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:
Thomas Boerger
2019-09-06 13:40:18 +02:00
parent 1405272e98
commit b6d43661a8
26 changed files with 1880 additions and 29 deletions
+74
View File
@@ -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
}