feat: add Domain/host based request routing

This commit is contained in:
Jonas Kaninda
2024-10-28 04:10:24 +01:00
parent b7700ee0de
commit ed4587ed1a
4 changed files with 13 additions and 2 deletions

View File

@@ -140,6 +140,8 @@ gateway:
routes: routes:
# Example of a route | 1 # Example of a route | 1
- name: Store - name: Store
# host Domain/host based request routing
host: localhost
path: /store path: /store
## Rewrite a request path ## Rewrite a request path
# e.g rewrite: /store to / # e.g rewrite: /store to /

View File

@@ -34,6 +34,8 @@ gateway:
routes: routes:
# Example of a route | 1 # Example of a route | 1
- name: Store - name: Store
# host Domain/host based request routing
host: localhost
path: /store path: /store
## Rewrite a request path ## Rewrite a request path
# e.g rewrite: /store to / # e.g rewrite: /store to /

View File

@@ -105,6 +105,8 @@ type Route struct {
Name string `yaml:"name"` Name string `yaml:"name"`
// Path defines route path // Path defines route path
Path string `yaml:"path"` Path string `yaml:"path"`
//Host Domain/host based request routing
Host string `yaml:"host"`
// Rewrite rewrites route path to desired path // Rewrite rewrites route path to desired path
// //
// E.g. /cart to / => It will rewrite /cart path to / // E.g. /cart to / => It will rewrite /cart path to /
@@ -247,6 +249,7 @@ func initConfig(configFile string) {
Routes: []Route{ Routes: []Route{
{ {
Name: "HealthCheck", Name: "HealthCheck",
Host: "localhost",
Path: "/public", Path: "/public",
Destination: "http://localhost:80", Destination: "http://localhost:80",
Rewrite: "/healthz", Rewrite: "/healthz",

View File

@@ -120,10 +120,14 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
disableXForward: route.DisableHeaderXForward, disableXForward: route.DisableHeaderXForward,
cors: route.Cors, cors: route.Cors,
} }
router := r.PathPrefix(route.Path).Subrouter() router := r.PathPrefix(route.Path).Subrouter()
router.Use(CORSHandler(route.Cors)) router.Use(CORSHandler(route.Cors))
router.PathPrefix("").Handler(proxyRoute.ProxyHandler()) //Domain/host based request routing
if route.Host != "" {
router.Host(route.Host).PathPrefix("").Handler(proxyRoute.ProxyHandler())
} else {
router.PathPrefix("").Handler(proxyRoute.ProxyHandler())
}
} else { } else {
logger.Error("Error, path is empty in route %s", route.Name) logger.Error("Error, path is empty in route %s", route.Name)
logger.Info("Route path ignored: %s", route.Path) logger.Info("Route path ignored: %s", route.Path)