Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/cortezaproject/corteza/extra/server-discovery/pkg/auth"
|
|
"github.com/cortezaproject/corteza/extra/server-discovery/pkg/healthcheck"
|
|
"github.com/cortezaproject/corteza/extra/server-discovery/pkg/options"
|
|
"github.com/cortezaproject/corteza/server/pkg/api"
|
|
"github.com/cortezaproject/corteza/server/pkg/version"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/zap"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
// routes used when server is in waiting mode
|
|
func waitingRoutes(log *zap.Logger, httpOpt options.HttpServerOpt) (r chi.Router) {
|
|
r = chi.NewRouter()
|
|
r.Use(handleCORS)
|
|
|
|
mountServiceHandlers(r, log, httpOpt, waiting)
|
|
|
|
return
|
|
}
|
|
|
|
// routes used when server in shutdown mode
|
|
func shutdownRoutes() (r chi.Router) {
|
|
r = chi.NewRouter()
|
|
|
|
return
|
|
}
|
|
|
|
// routes used when in active mode
|
|
func activeRoutes(log *zap.Logger, mountable []func(r chi.Router), envOpt options.EnvironmentOpt, httpOpt options.HttpServerOpt, searcherOpt options.SearcherOpt) (r chi.Router) {
|
|
r = chi.NewRouter()
|
|
r.Use(handleCORS)
|
|
|
|
r.Route("/"+strings.TrimPrefix(httpOpt.BaseUrl, "/"), func(r chi.Router) {
|
|
// Base middleware, CORS, RealIP, RequestID, context-logger
|
|
r.Use(BaseMiddleware(envOpt.IsProduction(), log)...)
|
|
|
|
// Verifies JWT in headers, cookies, ...
|
|
r.Use(auth.HttpTokenVerifier)
|
|
|
|
for _, mount := range mountable {
|
|
mount(r)
|
|
}
|
|
|
|
})
|
|
|
|
if httpOpt.BaseUrl != "/" {
|
|
r.Handle("/", http.RedirectHandler(httpOpt.BaseUrl, http.StatusTemporaryRedirect))
|
|
}
|
|
|
|
mountServiceHandlers(r, log, httpOpt, active)
|
|
|
|
return
|
|
}
|
|
|
|
func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerOpt, state uint32) {
|
|
if opt.EnableVersionRoute {
|
|
mountVersionHandler(r, log, opt.BaseUrl)
|
|
}
|
|
|
|
if opt.EnableHealthcheckRoute {
|
|
mountHealthCheckHandler(r, log, opt.BaseUrl)
|
|
}
|
|
}
|
|
|
|
func mountVersionHandler(r chi.Router, log *zap.Logger, basePath string) {
|
|
var (
|
|
dPath = "/version"
|
|
sPath = path.Join(basePath, dPath)
|
|
handler = func(w http.ResponseWriter, r *http.Request) {
|
|
api.Send(w, r, struct {
|
|
BuildTime string `json:"buildTime"`
|
|
Version string `json:"version"`
|
|
}{version.BuildTime, version.Version})
|
|
}
|
|
)
|
|
|
|
r.Get(dPath, handler)
|
|
log.Debug("version route enabled: " + dPath)
|
|
|
|
if dPath != sPath {
|
|
r.Get(sPath, handler)
|
|
log.Debug("version route enabled: " + sPath)
|
|
}
|
|
|
|
}
|
|
|
|
func mountHealthCheckHandler(r chi.Router, log *zap.Logger, basePath string) {
|
|
// default & sub path for health-check endpoint
|
|
var (
|
|
dPath = "/healthcheck"
|
|
sPath = path.Join(basePath, dPath)
|
|
)
|
|
|
|
r.Get(dPath, healthcheck.HttpHandler())
|
|
log.Debug("health check route enabled: " + sPath)
|
|
|
|
if dPath != sPath {
|
|
r.Get(sPath, healthcheck.HttpHandler())
|
|
log.Debug("health check route enabled: " + sPath)
|
|
}
|
|
}
|