start working on context root configuration

This commit is contained in:
Michael Engelhardt
2020-11-21 01:44:05 +01:00
parent 76d45d7eb8
commit f9706a98ed
6 changed files with 126 additions and 9 deletions

18
main.go
View File

@ -29,13 +29,16 @@ func main() {
if cfg.Security != nil && cfg.Security.IsValid() {
resultsHandler = security.Handler(serviceResultsHandler, cfg.Security)
}
http.HandleFunc("/api/v1/results", resultsHandler)
http.HandleFunc("/health", healthHandler)
http.Handle("/", GzipHandler(http.FileServer(http.Dir("./static"))))
// favicon needs to be always served from the root
http.HandleFunc("/favicon.ico", favIconHandler)
http.HandleFunc(cfg.Web.AppendToCtxRoot("/api/v1/results"), resultsHandler)
http.HandleFunc(cfg.Web.AppendToCtxRoot("/health"), healthHandler)
http.Handle(cfg.Web.CtxRoot(), GzipHandler(http.StripPrefix(cfg.Web.CtxRoot(), http.FileServer(http.Dir("./static")))))
if cfg.Metrics {
http.Handle("/metrics", promhttp.Handler())
http.Handle(cfg.Web.AppendToCtxRoot("/metrics"), promhttp.Handler())
}
log.Printf("[main][main] Listening on %s\n", cfg.Web.SocketAddress())
log.Printf("[main][main] Listening on %s%s\n", cfg.Web.SocketAddress(), cfg.Web.CtxRoot())
go watchdog.Monitor(cfg)
log.Fatal(http.ListenAndServe(cfg.Web.SocketAddress(), nil))
}
@ -88,3 +91,8 @@ func healthHandler(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte("{\"status\":\"UP\"}"))
}
// favIconHanlder responds to /favicon.ico requests
func favIconHandler(writer http.ResponseWriter, request *http.Request) {
http.ServeFile(writer, request, "./static/favicon.ico")
}