mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-11-29 15:31:53 +00:00
feat: public /api/health served by the agentbbs process (#143)
Some checks failed
CI / build (push) Has been cancelled
deploy / deploy (push) Has been cancelled
test / test (push) Has been cancelled
Some checks failed
CI / build (push) Has been cancelled
deploy / deploy (push) Has been cancelled
test / test (push) Has been cancelled
https://bbs.profullstack.com/ is Caddy's file_server, so a 200 there says nothing about the BBS. /api/health is now answered by agentbbs itself on its loopback HTTP listener (Caddy proxies it, like /verify) after a `select 1` on the SQLite store with a 3s cap: 200 {"status":"ok","db":"ok"} or 503 {"status":"error","db":"down"}, Cache-Control: no-store. Adds Store.Ping. For status.profullstack.com. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e95f2a30c3
commit
6cdc352d97
4 changed files with 65 additions and 0 deletions
34
cmd/agentbbs/health_test.go
Normal file
34
cmd/agentbbs/health_test.go
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/profullstack/agentbbs/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleHealth(t *testing.T) {
|
||||||
|
st, err := store.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
a := &app{st: st}
|
||||||
|
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
a.handleHealth(rec, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
||||||
|
if rec.Code != http.StatusOK || rec.Body.String() != `{"status":"ok","db":"ok"}` {
|
||||||
|
t.Fatalf("healthy: %d %s", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
if cc := rec.Header().Get("Cache-Control"); cc != "no-store" {
|
||||||
|
t.Fatalf("Cache-Control = %q", cc)
|
||||||
|
}
|
||||||
|
|
||||||
|
st.Close()
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
a.handleHealth(rec, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
||||||
|
if rec.Code != http.StatusServiceUnavailable || rec.Body.String() != `{"status":"error","db":"down"}` {
|
||||||
|
t.Fatalf("closed store: %d %s", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -272,6 +272,7 @@ func main() {
|
||||||
mux.HandleFunc("/verify", a.handleVerify)
|
mux.HandleFunc("/verify", a.handleVerify)
|
||||||
mux.HandleFunc("/irc-auth", a.handleIRCAuth) // Ergo auth-script: members-only gate
|
mux.HandleFunc("/irc-auth", a.handleIRCAuth) // Ergo auth-script: members-only gate
|
||||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok")) })
|
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok")) })
|
||||||
|
mux.HandleFunc("/api/health", a.handleHealth) // Caddy proxies it: status.profullstack.com
|
||||||
// OpenAccess descriptor for the BBS host itself (Caddy proxies the
|
// OpenAccess descriptor for the BBS host itself (Caddy proxies the
|
||||||
// well-known path here); the files host serves the same one.
|
// well-known path here); the files host serves the same one.
|
||||||
mux.Handle(files.OpenAccessPath, files.OpenAccessHandler())
|
mux.Handle(files.OpenAccessPath, files.OpenAccessHandler())
|
||||||
|
|
@ -2395,3 +2396,19 @@ func remoteIP(s ssh.Session) string {
|
||||||
}
|
}
|
||||||
return s.RemoteAddr().String()
|
return s.RemoteAddr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleHealth is the public health check status.profullstack.com polls via
|
||||||
|
// Caddy: it proves this process (the one serving SSH :22) is up and its SQLite
|
||||||
|
// store answers a query within 3s. Never returns error details.
|
||||||
|
func (a *app) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("Cache-Control", "no-store")
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := a.st.Ping(ctx); err != nil {
|
||||||
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
|
_, _ = w.Write([]byte(`{"status":"error","db":"down"}`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, _ = w.Write([]byte(`{"status":"ok","db":"ok"}`))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -235,6 +236,8 @@ type Store interface {
|
||||||
// SetFilesSetting writes a Files service setting. Idempotent upsert.
|
// SetFilesSetting writes a Files service setting. Idempotent upsert.
|
||||||
SetFilesSetting(key, value string) error
|
SetFilesSetting(key, value string) error
|
||||||
|
|
||||||
|
// Ping runs a trivial query, for the public health check.
|
||||||
|
Ping(ctx context.Context) error
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1175,4 +1178,9 @@ func (s *sqliteStore) SetFilesSetting(key, value string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sqliteStore) Ping(ctx context.Context) error {
|
||||||
|
var one int
|
||||||
|
return s.db.QueryRowContext(ctx, "select 1").Scan(&one)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sqliteStore) Close() error { return s.db.Close() }
|
func (s *sqliteStore) Close() error { return s.db.Close() }
|
||||||
|
|
|
||||||
6
setup.sh
6
setup.sh
|
|
@ -807,6 +807,12 @@ ${DOMAIN} {
|
||||||
reverse_proxy http://${HTTP_ADDR}
|
reverse_proxy http://${HTTP_ADDR}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Public health check (status.profullstack.com): answered by the agentbbs
|
||||||
|
# process itself after a SQLite query, so a 200 proves the BBS is up.
|
||||||
|
handle /api/health {
|
||||||
|
reverse_proxy http://${HTTP_ADDR}
|
||||||
|
}
|
||||||
|
|
||||||
# IRC over WebSocket: Caddy terminates TLS and proxies to Ergo's loopback
|
# IRC over WebSocket: Caddy terminates TLS and proxies to Ergo's loopback
|
||||||
# WebSocket listener, so web clients hit wss://${DOMAIN}/irc and agents get a
|
# WebSocket listener, so web clients hit wss://${DOMAIN}/irc and agents get a
|
||||||
# WebSocket transport without exposing another public port. (No-op if IRC=0;
|
# WebSocket transport without exposing another public port. (No-op if IRC=0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue