Add custom http root
Now we are able to let this service run on a different root path.
This commit is contained in:
@@ -71,6 +71,9 @@ OCS_DEBUG_ZPAGES
|
|||||||
OCS_HTTP_ADDR
|
OCS_HTTP_ADDR
|
||||||
: Address to bind http server, defaults to `0.0.0.0:9110`
|
: Address to bind http server, defaults to `0.0.0.0:9110`
|
||||||
|
|
||||||
|
OCS_HTTP_ROOT
|
||||||
|
: Root path of http server, defaults to `/`
|
||||||
|
|
||||||
##### Health
|
##### Health
|
||||||
|
|
||||||
OCS_DEBUG_ADDR
|
OCS_DEBUG_ADDR
|
||||||
@@ -126,6 +129,9 @@ If you prefer to configure the service with commandline flags you can see the av
|
|||||||
--http-addr
|
--http-addr
|
||||||
: Address to bind http server, defaults to `0.0.0.0:9110`
|
: Address to bind http server, defaults to `0.0.0.0:9110`
|
||||||
|
|
||||||
|
--http-root
|
||||||
|
: Root path of http server, defaults to `/`
|
||||||
|
|
||||||
##### Health
|
##### Health
|
||||||
|
|
||||||
--debug-addr
|
--debug-addr
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"contrib.go.opencensus.io/exporter/jaeger"
|
"contrib.go.opencensus.io/exporter/jaeger"
|
||||||
@@ -28,6 +29,13 @@ func Server(cfg *config.Config) cli.Command {
|
|||||||
Name: "server",
|
Name: "server",
|
||||||
Usage: "Start integrated server",
|
Usage: "Start integrated server",
|
||||||
Flags: flagset.ServerWithConfig(cfg),
|
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 {
|
Action: func(c *cli.Context) error {
|
||||||
logger := NewLogger(cfg)
|
logger := NewLogger(cfg)
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type Debug struct {
|
|||||||
// HTTP defines the available http configuration.
|
// HTTP defines the available http configuration.
|
||||||
type HTTP struct {
|
type HTTP struct {
|
||||||
Addr string
|
Addr string
|
||||||
|
Root string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tracing defines the available tracing configuration.
|
// Tracing defines the available tracing configuration.
|
||||||
|
|||||||
@@ -120,5 +120,12 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
|||||||
EnvVar: "OCS_HTTP_ADDR",
|
EnvVar: "OCS_HTTP_ADDR",
|
||||||
Destination: &cfg.HTTP.Addr,
|
Destination: &cfg.HTTP.Addr,
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "http-root",
|
||||||
|
Value: "/",
|
||||||
|
Usage: "Root path of http server",
|
||||||
|
EnvVar: "OCS_HTTP_ROOT",
|
||||||
|
Destination: &cfg.HTTP.Root,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,9 @@ func NewService(opts ...Option) Service {
|
|||||||
mux: m,
|
mux: m,
|
||||||
}
|
}
|
||||||
|
|
||||||
m.HandleFunc("/", svc.Dummy)
|
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
||||||
|
r.Get("/", svc.Dummy)
|
||||||
|
})
|
||||||
|
|
||||||
return svc
|
return svc
|
||||||
}
|
}
|
||||||
@@ -43,5 +45,8 @@ func (g Ocs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Dummy implements the Service interface.
|
// Dummy implements the Service interface.
|
||||||
func (g Ocs) Dummy(w http.ResponseWriter, r *http.Request) {
|
func (g Ocs) 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)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user