mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(http): add healthcheck endpoints liveness and readiness (#240)
* feat(http): add liveness and readiness endpoints * feat(http): improve unhealthy msg
This commit is contained in:
parent
4b74a006c8
commit
2a3b5ce448
4 changed files with 61 additions and 2 deletions
|
@ -100,7 +100,7 @@ func main() {
|
||||||
errorChannel := make(chan error)
|
errorChannel := make(chan error)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
httpServer := http.NewServer(cfg, serverEvents, version, commit, date, actionService, authService, downloadClientService, filterService, indexerService, ircService, notificationService, releaseService)
|
httpServer := http.NewServer(cfg, serverEvents, db, version, commit, date, actionService, authService, downloadClientService, filterService, indexerService, ircService, notificationService, releaseService)
|
||||||
errorChannel <- httpServer.Open()
|
errorChannel <- httpServer.Open()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,10 @@ func (db *DB) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) Ping() error {
|
||||||
|
return db.handler.Ping()
|
||||||
|
}
|
||||||
|
|
||||||
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
||||||
tx, err := db.handler.BeginTx(ctx, opts)
|
tx, err := db.handler.BeginTx(ctx, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
51
internal/http/health.go
Normal file
51
internal/http/health.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/autobrr/autobrr/internal/database"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type healthHandler struct {
|
||||||
|
encoder encoder
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHealthHandler(encoder encoder, db *database.DB) *healthHandler {
|
||||||
|
return &healthHandler{
|
||||||
|
encoder: encoder,
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h healthHandler) Routes(r chi.Router) {
|
||||||
|
r.Get("/liveness", h.handleLiveness)
|
||||||
|
r.Get("/readiness", h.handleReadiness)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h healthHandler) handleLiveness(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
writeHealthy(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h healthHandler) handleReadiness(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
if err := h.db.Ping(); err != nil {
|
||||||
|
writeUnhealthy(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeHealthy(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHealthy(w http.ResponseWriter) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeUnhealthy(w http.ResponseWriter) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte("Unhealthy. Database unreachable"))
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/autobrr/autobrr/internal/database"
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/web"
|
"github.com/autobrr/autobrr/web"
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
sse *sse.Server
|
sse *sse.Server
|
||||||
|
db *database.DB
|
||||||
|
|
||||||
config domain.Config
|
config domain.Config
|
||||||
cookieStore *sessions.CookieStore
|
cookieStore *sessions.CookieStore
|
||||||
|
@ -35,10 +37,11 @@ type Server struct {
|
||||||
releaseService releaseService
|
releaseService releaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(config domain.Config, sse *sse.Server, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, notificationSvc notificationService, releaseSvc releaseService) Server {
|
func NewServer(config domain.Config, sse *sse.Server, db *database.DB, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, notificationSvc notificationService, releaseSvc releaseService) Server {
|
||||||
return Server{
|
return Server{
|
||||||
config: config,
|
config: config,
|
||||||
sse: sse,
|
sse: sse,
|
||||||
|
db: db,
|
||||||
version: version,
|
version: version,
|
||||||
commit: commit,
|
commit: commit,
|
||||||
date: date,
|
date: date,
|
||||||
|
@ -98,6 +101,7 @@ func (s Server) Handler() http.Handler {
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/api/auth", newAuthHandler(encoder, s.config, s.cookieStore, s.authService).Routes)
|
r.Route("/api/auth", newAuthHandler(encoder, s.config, s.cookieStore, s.authService).Routes)
|
||||||
|
r.Route("/api/healthz", newHealthHandler(encoder, s.db).Routes)
|
||||||
|
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(s.IsAuthenticated)
|
r.Use(s.IsAuthenticated)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue