Skip to content

Commit

Permalink
Add custom http root
Browse files Browse the repository at this point in the history
Now we are able to let this service run on a different root path.
  • Loading branch information
tboerger committed Dec 9, 2019
1 parent 34dcbee commit c100102
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"os/signal"
"strings"
"time"

"contrib.go.opencensus.io/exporter/jaeger"
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
9 changes: 7 additions & 2 deletions pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)))
}

0 comments on commit c100102

Please sign in to comment.