mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
feat: add liveness and readiness probes
- Added new routes: /liveness, /healthz, /readiness, /readyz - These routes do not take the base path into consideration and are always available at the root of the server
This commit is contained in:
parent
09fb05aaaf
commit
1bb0fde14d
1 changed files with 33 additions and 18 deletions
|
@ -173,43 +173,58 @@ func startHTTPServer(server *http.Server) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupHandler(store *storage.Storage, pool *worker.Pool) *mux.Router {
|
func setupHandler(store *storage.Storage, pool *worker.Pool) *mux.Router {
|
||||||
|
livenessProbe := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
readinessProbe := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := store.Ping(); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Database Connection Error: %q", err), http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
|
||||||
|
// These routes do not take the base path into consideration and are always available at the root of the server.
|
||||||
|
router.HandleFunc("/liveness", livenessProbe).Name("liveness")
|
||||||
|
router.HandleFunc("/healthz", livenessProbe).Name("healthz")
|
||||||
|
router.HandleFunc("/readiness", readinessProbe).Name("readiness")
|
||||||
|
router.HandleFunc("/readyz", readinessProbe).Name("readyz")
|
||||||
|
|
||||||
|
var subrouter *mux.Router
|
||||||
if config.Opts.BasePath() != "" {
|
if config.Opts.BasePath() != "" {
|
||||||
router = router.PathPrefix(config.Opts.BasePath()).Subrouter()
|
subrouter = router.PathPrefix(config.Opts.BasePath()).Subrouter()
|
||||||
|
} else {
|
||||||
|
subrouter = router.NewRoute().Subrouter()
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Opts.HasMaintenanceMode() {
|
if config.Opts.HasMaintenanceMode() {
|
||||||
router.Use(func(next http.Handler) http.Handler {
|
subrouter.Use(func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte(config.Opts.MaintenanceMessage()))
|
w.Write([]byte(config.Opts.MaintenanceMessage()))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
router.Use(middleware)
|
subrouter.Use(middleware)
|
||||||
|
|
||||||
fever.Serve(router, store)
|
fever.Serve(subrouter, store)
|
||||||
googlereader.Serve(router, store)
|
googlereader.Serve(subrouter, store)
|
||||||
api.Serve(router, store, pool)
|
api.Serve(subrouter, store, pool)
|
||||||
ui.Serve(router, store, pool)
|
ui.Serve(subrouter, store, pool)
|
||||||
|
|
||||||
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
subrouter.HandleFunc("/healthcheck", readinessProbe).Name("healthcheck")
|
||||||
if err := store.Ping(); err != nil {
|
|
||||||
http.Error(w, "Database Connection Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write([]byte("OK"))
|
subrouter.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
|
||||||
}).Name("healthcheck")
|
|
||||||
|
|
||||||
router.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write([]byte(version.Version))
|
w.Write([]byte(version.Version))
|
||||||
}).Name("version")
|
}).Name("version")
|
||||||
|
|
||||||
if config.Opts.HasMetricsCollector() {
|
if config.Opts.HasMetricsCollector() {
|
||||||
router.Handle("/metrics", promhttp.Handler()).Name("metrics")
|
subrouter.Handle("/metrics", promhttp.Handler()).Name("metrics")
|
||||||
router.Use(func(next http.Handler) http.Handler {
|
subrouter.Use(func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
route := mux.CurrentRoute(r)
|
route := mux.CurrentRoute(r)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue