From 2ae971be818f7a83467b3d76ce1fb7fb7f67b8ca Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sat, 04 Jul 2026 01:40:56 +0000 Subject: [PATCH] add /health endpoint --- README.md | 3 ++- internal/api/health.go | 11 +++++++++++ internal/api/server.go | 2 ++ internal/store/store.go | 5 +++++ 4 file(s) changed, 20 insertion(s)(+), 1 deletion(s)(-) diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -175,13 +175,14 @@ - [ ] Account deactivation handling - [x] Graceful shutdown and Firehose reconnect - [x] CI + Dockerfile +- [x] Add health endpoint **Medium term** - [ ] Robust automatic backfill with checkpoint/resume (survive restarts mid-backfill) - [ ] Exponential backoff for getRepo requests - [ ] Backfill progress reporting -- [ ] Metrics and health endpoints +- [ ] Prometheus metrics endpoint **Longer term** diff --git a/internal/api/health.go b/internal/api/health.go new file mode 100644 --- /dev/null +++ b/internal/api/health.go @@ -0,0 +1,11 @@ +package api + +import "net/http" + +func (s *Server) Health(w http.ResponseWriter, r *http.Request) { + if err := s.Store.Ping(r.Context()); err != nil { + http.Error(w, "unhealthy", http.StatusServiceUnavailable) + return + } + w.WriteHeader(http.StatusOK) +} diff --git a/internal/api/server.go b/internal/api/server.go --- a/internal/api/server.go +++ b/internal/api/server.go @@ -12,6 +12,8 @@ func (s *Server) Run(addr string) error { mux := http.NewServeMux() + mux.HandleFunc("GET /health", s.Health) + mux.HandleFunc("GET /xrpc/blue.microcosm.links.getBacklinksCount", s.GetBacklinksCount) mux.HandleFunc("GET /xrpc/blue.microcosm.links.getBacklinkDids", s.GetBacklinkDids) mux.HandleFunc("GET /xrpc/blue.microcosm.links.getBacklinks", s.GetBacklinks) diff --git a/internal/store/store.go b/internal/store/store.go --- a/internal/store/store.go +++ b/internal/store/store.go @@ -1,6 +1,7 @@ package store import ( + "context" "database/sql" "fmt" @@ -59,4 +60,8 @@ } return &Store{writeDB: writeDB, readDB: readDB}, nil +} + +func (s *Store) Ping(ctx context.Context) error { + return s.readDB.PingContext(ctx) } -- tangled.sh