From 385a99d3a3710fb5622694567d117babf492a038 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Mon, 9 Dec 2019 14:47:38 +0100 Subject: [PATCH] Add custom http root Now we are able to let this service run on a different root path. --- docs/content/getting-started.md | 6 ++++++ pkg/command/server.go | 8 ++++++++ pkg/config/config.go | 1 + pkg/flagset/flagset.go | 7 +++++++ pkg/service/v0/service.go | 15 ++++++++------- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/content/getting-started.md b/docs/content/getting-started.md index 6df7a718ee..c81ddf3f11 100644 --- a/docs/content/getting-started.md +++ b/docs/content/getting-started.md @@ -71,6 +71,9 @@ GRAPH_DEBUG_ZPAGES GRAPH_HTTP_ADDR : Address to bind http server, defaults to `0.0.0.0:9120` +GRAPH_HTTP_ROOT +: Root path of http server, defaults to `/` + ##### Health GRAPH_DEBUG_ADDR @@ -126,6 +129,9 @@ If you prefer to configure the service with commandline flags you can see the av --http-addr : Address to bind http server, defaults to `0.0.0.0:9120` +--http-root +: Root path of http server, defaults to `/` + ##### Health --debug-addr diff --git a/pkg/command/server.go b/pkg/command/server.go index a6ca7affbe..2c402a8a4b 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -4,6 +4,7 @@ import ( "context" "os" "os/signal" + "strings" "time" "contrib.go.opencensus.io/exporter/jaeger" @@ -28,6 +29,13 @@ func Server(cfg *config.Config) cli.Command { Name: "server", Usage: "Start integrated server", Flags: flagset.ServerWithConfig(cfg), + Before: func(c *cli.Context) error { + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil + }, Action: func(c *cli.Context) error { logger := NewLogger(cfg) diff --git a/pkg/config/config.go b/pkg/config/config.go index 0f56402e21..8067a011dc 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,7 @@ type Debug struct { // HTTP defines the available http configuration. type HTTP struct { Addr string + Root string } // Tracing defines the available tracing configuration. diff --git a/pkg/flagset/flagset.go b/pkg/flagset/flagset.go index c74d97de4a..786bec82bd 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -120,5 +120,12 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { EnvVar: "GRAPH_HTTP_ADDR", Destination: &cfg.HTTP.Addr, }, + &cli.StringFlag{ + Name: "http-root", + Value: "/", + Usage: "Root path of http server", + EnvVar: "GRAPH_HTTP_ROOT", + Destination: &cfg.HTTP.Root, + }, } } diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index a2137ff6e3..fac8f516f4 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -27,13 +27,14 @@ func NewService(opts ...Option) Service { logger: &options.Logger, } - m.Route("/v1.0", func(r chi.Router) { - r.Get("/me", svc.GetMe) - r.Route("/users", func(r chi.Router) { - r.Get("/", svc.GetUsers) - r.Route("/{userID}", func(r chi.Router) { - r.Use(svc.UserCtx) - r.Get("/", svc.GetUser) + m.Route(options.Config.HTTP.Root, func(r chi.Router) { + r.Route("/v1.0", func(r chi.Router) { + r.Route("/users", func(r chi.Router) { + r.Get("/", svc.GetUsers) + r.Route("/{userID}", func(r chi.Router) { + r.Use(svc.UserCtx) + r.Get("/", svc.GetUser) + }) }) }) })