Prefixing paths in Vert.x Web Api service/contract - vert.x

It seems that the vertx-web-api-service/vertx-web-api-contract does not respect the OpenAPI 3 top-level node called "servers" - under which is a list of - url: that can be used by the client to know where the API is located (and prefixed).
I want to be able to prefix the paths: with /v1 but how is that possible?
My initial though was to put it in the top-level servers: node but it does not work.

It's not possible for the RouterFactory object to automatically determine the base path to use, because the servers is an array and you might define more than once (https://github.com/vert-x3/vertx-web/issues/1655).
If you need to specify a subpath, mount the generated router as subrouter in another router specifying the subpath:
Router generated = routerFactory.getRouter();
Router global = Router.router(vertx);
global.mountSubRouter("/v1", generated);

Related

Config server with Vault backend - fetch secrets from multiple paths

We are using config server with Vault backend to fetch application secrets.
Config server project is using spring-vault-core dependency and spring-vault-dependencies dependency management for Vault.
Vault related config in application yml file is as follows:
spring:
cloud:
config:
server:
vault:
order: 0
uri: <complete URI>
connection-timeout: 5000
read-timeout: 15000
kvVersion: 2
backend: secret
defaultKey: config
This works fine and fetches me the Vault secrets in secret/config.
I am unable to add secret fetching from multiple paths in Vault (secret/config + secret/customFolder). I have tried adding comma separated application-name etc as suggested across various posts but does not work. Has anyone tried something similar?
You can take a look to the composite profile.
There are a lot of additional questions - what exactly you are trying to do, and why do you want to have this?
For us, for example, it was important to split infra services configurations and also split, actually, microservices configurations by itself. And, important requirement, to be able to "overwrite" it (in case of migrations, for instance).
We have achieve that with two things:
on config server side we are using composite configuration (with exactly the same type and uri, but little bit different backend and keys),
on config client's side we are specifying several values for spring.cloud.config.name property (coma separated list).

Traefik redirect from one host to another

We decided to move from the subdomain structure to one root domain with path prefixes, but we got many old URLs on the internet. So is there any way to add a redirect from the old URL to the new one?
For example,
We got subdomain test.example.com switched to example.com/test, I can access correctly site with the string in docker-swarm YAML file
traefik.frontend.rule: Host:example.com;PathPrefixStrip:/test
but when I'm trying to add to Traefik config redirects like:
[http.middlewares]
[http.middlewares.test-redirectregex.redirectRegex]
regex = "^https://(*).example.com/)"
replacement = "^https://example.com/${1}"
Traefik says that it doesn't know where to forward this request
If I'm trying to add:
traefik.frontend.rule: Host:test.example.com,example.com;PathPrefixStrip:/test
Traefik adds a prefix to both hosts. Is there any way to resolve this without adding a second reverse proxy?
Assuming that you are using Traefik 2.1, you can use the below middleware for Traefik
[http.middlewares]
[http.middlewares.blog-redirect.redirectRegex]
regex = "^(https?://)(.*).example.com/(.*)$"
replacement = "${1}example.com/${2}/${3}"
permanent = true
The important step to activate the above middleware is to add the below label on the corresponding router and service. For instance, if you a a blog service and you defined a blog router for it, then you need to add the below table to the services
traefik.http.routers.blog.middlewares=blog-redirect
In addition, your route rule should look like the below rule to be able to handle both domains (or you define multiple routes per service)
- traefik.http.routers.blog.rule=Host(`example.com`) && Path(`/test`) || Host(`api.example.com``)
in this post, you can find more info about traffic and redirection

Spring Web: Mapping URL to a standalone Spring app

I have two standalone Spring Boot/Web web app .jars, A and B, both made by third-parties, with their own URL mappings which I do not know about in advance.
I would like to create a setup where "localhost:8080/A" maps (passes through) to A's "/" mapping. Similarly, "localhost:8080/B" should pass through to B's "/" mapping.
Additional mappings, which again I don't necessarily know about in advance, should also pass through respectively - so "localhost:8080/A/items" should pass through to A's "/items", etc.
I do not have the source code for A or B, only .jar files.
For security, A and B should have separate scopes with no knowledge of each other or ability to interact with each other. The whole setup should behave as if A and B were separate "inner" servers within my "outer" localhost server.
Can this be achieved via a Spring Boot/Web wrapper or gateway app, or some other way?
You could use spring boot with Zuul as a third app, running on 8080, which has routes setup for A and B similar to what you described.
zuul:
routes:
app-A:
path: /A/**
url: http://localhost:8081/
app-B:
path: /B/**
url: http://localhost:8082/
That configuration (application.yml) of your third app would point "A" traffic to port 8081 and "B" traffic to port 8082.
This is quick and dirty, but should get you started.
Alternatively, you could use spring cloud gateway, to get a similar type of setup with the additional ability to do sockets/reactive.

Routing to different ports based on environment variable stored in path of request

This is kind of a weird complicated question.
Context:
I have a bunch of docker containers that need to be routed to from haproxy dynamically. They are each running on different ports on the machine, and are stored in environment variables like this:
a=9873
b=9874
c=9875
These are available to the haproxy server. The request path that comes in will be in the form like this example:
/api/a/action
From that, the taks is as follows:
The /api needs to be removed from the path.
The /a refers to the service, so the environment variable for a needs to be retrieved to get the port of the server
The request needs to be routed to localhost:9873/a/action where the port, 9873, is the environment variable that is the value from the path in the beginning (after removing /api) and then the path is simply appended onto the request (which is the /a/action that remains after removing the /api.
My current config looks like this:
backend api
reqrep ^([^\ ]*\ /)api[/]?(.*) \1\2
server api_server localhost:9871
All this config is doing is removing the /api from the path of the request and sending it to a static port, 9871. *I need this port to be the value held by the environment variable of the same name as the first element in the path (the /a above) and the rest (passing the remaining path) is already working.*
I also would like to be able to get the environment variable of the name prefix_a, where the path will have the name /a, but I need to prepend one common prefix prefix_ to get the environment variable. This can be a separate question or search though, unless it's simple to just put that into the solution.
Please let me know if I can clarify or give more information that might help solve the problem.
(I've done a heck a lot of googling. Here are some related urls but not quite the answer I need:
https://gist.github.com/meineerde/8bea63e64fc47f9a67c0
Dynamic routing to backend based on context path in HAProxy
How can I set up HAProxy to a backend based on a value in the url?
Haproxy route and rewrite based on URI path
haproxy: get the host name
https://serverfault.com/questions/818937/haproxy-is-giving-me-problems-with-regex-replace-is-this-a-bug-or-am-i-doing-so
https://serverfault.com/questions/668025/how-to-use-environment-variable-in-haproxy
http://cbonte.github.io/haproxy-dconv/1.9/configuration.html#7.2
How do I set a dynamic variable in HAProxy?
use environment variables in haproxy

Context routing for two different apps

I deploy two different apps to CF and I want to be able to use the context path routing for those two apps
e.g.
lets say I've two apps that deployed and I was able to consume it with the following URL.
1. app1.domain.com
2. app2.domain.com
Now I want somehow to use the context path routing of CF
to be able to use this apps like following
1. something.domain.com/app1
2. something.domain.com/app2
My question are:
I missing the "something", what should I put in the apps manifest to be able to use it like above ?
How should I define the routes in the mainfest.yml file?
what should I put in the path?
Example will be very helpful
https://www.cloudfoundry.org/context-path-routing/
Lucky that I recently prepared a blog post and a tutorial on context path routing. Here is a sample manifest.yml taken from the tutorial that shows two apps with different routes on the same domain:
# This manifest deploys two applications.
#
# Both use the same host and domain name as defined
# by their respective route(s) property. The first app
# uses the root path, the second the "sub" and
# "lower" paths.
applications:
# The Python app starts here
- name: yourname-myapp
memory: 256M
command: python myapp.py
routes:
- route: yourname-myapp.mybluemix.net
path: ./top/
# The Node.js app starts here
- name: yourname-myapp-node
routes:
- route: yourname-myapp.mybluemix.net/lower
- route: yourname-myapp.mybluemix.net/sub
path: ./lower/
You can even define multiple routes for a single app, all in a single manifest file. The routes property is the place for the routing information. Note that the path points to the source code for the app (if done this way) and that you need a recent version of cf CLI to deploy it. See the tutorial for more information and additional links.
You can also find a good example in the map route documentation per below
https://docs.cloudfoundry.org/devguide/deploy-apps/routes-domains.html#map-route