From c100102253a5fae3612a3b187d3eb3ac45b5f4f9 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Mon, 9 Dec 2019 14:48:37 +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 | 9 +++++++-- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/content/getting-started.md b/docs/content/getting-started.md index fe7837ea95b..4282ad17b84 100644 --- a/docs/content/getting-started.md +++ b/docs/content/getting-started.md @@ -71,6 +71,9 @@ WEBDAV_DEBUG_ZPAGES WEBDAV_HTTP_ADDR : Address to bind http server, defaults to `0.0.0.0:9115` +WEBDAV_HTTP_ROOT +: Root path of http server, defaults to `/` + ##### Health WEBDAV_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:9115` +--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 d33af6950d7..4f1ec5566e3 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 0f56402e211..8067a011dc8 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 9528eae80fb..a414d238a9b 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -120,5 +120,12 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { EnvVar: "WEBDAV_HTTP_ADDR", Destination: &cfg.HTTP.Addr, }, + &cli.StringFlag{ + Name: "http-root", + Value: "/", + Usage: "Root path of http server", + EnvVar: "WEBDAV_HTTP_ROOT", + Destination: &cfg.HTTP.Root, + }, } } diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 4a180b5f3ef..1ea39f58942 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -25,7 +25,9 @@ func NewService(opts ...Option) Service { mux: m, } - m.HandleFunc("/", svc.Dummy) + m.Route(options.Config.HTTP.Root, func(r chi.Router) { + r.Get("/", svc.Dummy) + }) return svc } @@ -43,5 +45,8 @@ func (g Webdav) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Dummy implements the Service interface. func (g Webdav) Dummy(w http.ResponseWriter, r *http.Request) { - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + + w.Write([]byte(http.StatusText(http.StatusOK))) }