Embed phoenix and serve it
So far Phoenix have been embedded into the binary and we can customize the required config based on flags, env variables and optionally via config file. For now I'm embedding the whole Phonix content, optherwise the all-in-one binary `ocis` will get pretty complicated until we add the generate commands to that repo as well and provide a mechanism to inject the embedding.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package debug
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/owncloud/ocis-phoenix/pkg/handler/metrics"
|
||||
"github.com/owncloud/ocis-phoenix/pkg/middleware/header"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// debug gets initialized by Router and configures the router.
|
||||
type debug struct {
|
||||
token string
|
||||
pprof bool
|
||||
}
|
||||
|
||||
// Router initializes a router for the debug server.
|
||||
func Router(opts ...Option) *chi.Mux {
|
||||
d := new(debug)
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(d)
|
||||
}
|
||||
|
||||
mux := chi.NewRouter()
|
||||
|
||||
mux.Use(hlog.NewHandler(log.Logger))
|
||||
mux.Use(hlog.RemoteAddrHandler("ip"))
|
||||
mux.Use(hlog.URLHandler("path"))
|
||||
mux.Use(hlog.MethodHandler("method"))
|
||||
mux.Use(hlog.RequestIDHandler("request_id", "Request-Id"))
|
||||
|
||||
mux.Use(middleware.RealIP)
|
||||
mux.Use(header.Version)
|
||||
mux.Use(header.Cache)
|
||||
mux.Use(header.Secure)
|
||||
mux.Use(header.Options)
|
||||
|
||||
mux.Route("/", func(root chi.Router) {
|
||||
if d.pprof {
|
||||
root.Mount(
|
||||
"/debug",
|
||||
middleware.Profiler(),
|
||||
)
|
||||
}
|
||||
|
||||
root.Mount(
|
||||
"/metrics",
|
||||
metrics.Handler(
|
||||
metrics.WithToken(d.token),
|
||||
),
|
||||
)
|
||||
|
||||
root.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
io.WriteString(w, http.StatusText(http.StatusOK))
|
||||
})
|
||||
|
||||
root.Get("/readyz", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
io.WriteString(w, http.StatusText(http.StatusOK))
|
||||
})
|
||||
})
|
||||
|
||||
return mux
|
||||
}
|
||||
Reference in New Issue
Block a user