35 lines
1.3 KiB
Go
35 lines
1.3 KiB
Go
// Package httpserver exposes bootstrap health endpoints only. Task, workflow,
|
|
// authentication and WebSocket APIs will be implemented in subsequent slices.
|
|
package httpserver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Checker func(context.Context) error
|
|
|
|
func New(addr string, ready Checker) *http.Server {
|
|
mux := http.NewServeMux()
|
|
write := func(w http.ResponseWriter, status int, body map[string]string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(body)
|
|
}
|
|
mux.HandleFunc("GET /health/live", func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, http.StatusOK, map[string]string{"status": "alive"})
|
|
})
|
|
mux.HandleFunc("GET /health/ready", func(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
|
defer cancel()
|
|
if ready == nil || ready(ctx) != nil {
|
|
write(w, http.StatusServiceUnavailable, map[string]string{"status": "not_ready"})
|
|
return
|
|
}
|
|
write(w, http.StatusOK, map[string]string{"status": "ready", "scope": "database_and_schema"})
|
|
})
|
|
return &http.Server{Addr: addr, Handler: mux, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 << 10}
|
|
}
|