mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
Redirect frontend routes to setup if setup is not configured. (#721)
Closes #617
This commit is contained in:
parent
448e806c36
commit
a13042e11b
@ -139,14 +139,16 @@ the way that the kolide server works.
|
||||
|
||||
httpLogger := kitlog.NewContext(logger).With("component", "http")
|
||||
|
||||
var apiHandler http.Handler
|
||||
var apiHandler, frontendHandler http.Handler
|
||||
{
|
||||
frontendHandler = prometheus.InstrumentHandler("get_frontend", service.ServeFrontend())
|
||||
apiHandler = service.MakeHandler(ctx, svc, config.Auth.JwtKey, httpLogger)
|
||||
// WithSetup will check if first time setup is required
|
||||
// By performing the same check inside main, we can make server startups
|
||||
// more efficient after the first startup.
|
||||
if service.RequireSetup(svc, logger) {
|
||||
apiHandler = service.WithSetup(svc, logger, apiHandler)
|
||||
frontendHandler = service.RedirectLoginToSetup(svc, logger, frontendHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,7 +163,7 @@ the way that the kolide server works.
|
||||
http.Handle("/assets/", prometheus.InstrumentHandler("static_assets", service.ServeStaticAssets("/assets/")))
|
||||
http.Handle("/metrics", prometheus.InstrumentHandler("metrics", promhttp.Handler()))
|
||||
http.Handle("/api/", apiHandler)
|
||||
http.Handle("/", prometheus.InstrumentHandler("get_frontend", service.ServeFrontend()))
|
||||
http.Handle("/", frontendHandler)
|
||||
|
||||
errs := make(chan error, 2)
|
||||
go func() {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
@ -369,7 +368,6 @@ func attachKolideAPIRoutes(r *mux.Router, h *kolideHandlers) {
|
||||
func WithSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
configRouter := http.NewServeMux()
|
||||
configRouter.Handle("/api/v1/kolide/config", http.HandlerFunc(forceSetup))
|
||||
configRouter.Handle("/api/v1/setup", kithttp.NewServer(
|
||||
context.Background(),
|
||||
makeSetupEndpoint(svc),
|
||||
@ -384,12 +382,24 @@ func WithSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http
|
||||
}
|
||||
}
|
||||
|
||||
func forceSetup(w http.ResponseWriter, r *http.Request) {
|
||||
response := map[string]bool{
|
||||
"require_setup": true,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(&response); err != nil {
|
||||
encodeError(context.Background(), err, w)
|
||||
// RedirectLoginToSetup detects if the setup endpoint should be used. If setup is required it redirect all
|
||||
// frontend urls to /setup, otherwise the frontend router is used.
|
||||
func RedirectLoginToSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
redirect := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/setup" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
newURL := r.URL
|
||||
newURL.Path = "/setup"
|
||||
http.Redirect(w, r, newURL.String(), http.StatusTemporaryRedirect)
|
||||
})
|
||||
if RequireSetup(svc, logger) {
|
||||
redirect.ServeHTTP(w, r)
|
||||
} else {
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user