managarten/services/mana-matrix-bot/internal/runtime/health.go
Till JS 819568c3df feat(infra): consolidate 21 Matrix bots into Go binary + add Go API gateway
Replace 21 separate NestJS Matrix bot processes (~2.1 GB RAM, ~4.2 GB Docker images)
with a single Go binary using plugin architecture (8.6 MB binary, ~30 MB RAM).

New services:
- services/mana-matrix-bot/ — Go Matrix bot with 21 plugins (mautrix-go, Redis sessions)
- services/mana-api-gateway-go/ — Go API gateway (rate limiting, API keys, credit billing)

Deleted:
- 21 services/matrix-*-bot/ directories
- packages/bot-services/ and packages/matrix-bot-common/
- Legacy deploy scripts and CI build jobs

Updated:
- docker-compose.macmini.yml: new Go services, legacy bots removed
- CI/CD: change detection + build jobs for Go services
- Root package.json: new dev:matrix, build:matrix, test:matrix scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 21:03:00 +01:00

66 lines
1.7 KiB
Go

package runtime
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"time"
)
// HealthServer serves health and metrics endpoints.
type HealthServer struct {
runtime *Runtime
port int
}
// NewHealthServer creates a new health server.
func NewHealthServer(rt *Runtime, port int) *HealthServer {
return &HealthServer{runtime: rt, port: port}
}
// Start starts the HTTP health server.
func (h *HealthServer) Start() *http.Server {
mux := http.NewServeMux()
mux.HandleFunc("GET /health", h.handleHealth)
mux.HandleFunc("GET /metrics", h.handleMetrics)
server := &http.Server{
Addr: fmt.Sprintf(":%d", h.port),
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
go func() {
slog.Info("health server starting", "port", h.port)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
slog.Error("health server error", "error", err)
}
}()
return server
}
func (h *HealthServer) handleHealth(w http.ResponseWriter, r *http.Request) {
plugins := h.runtime.ActivePlugins()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"status": "ok",
"service": "mana-matrix-bot",
"timestamp": time.Now().UTC().Format(time.RFC3339),
"plugins": plugins,
"count": len(plugins),
})
}
func (h *HealthServer) handleMetrics(w http.ResponseWriter, r *http.Request) {
plugins := h.runtime.ActivePlugins()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "# HELP mana_matrix_bot_plugins_active Number of active plugins\n")
fmt.Fprintf(w, "# TYPE mana_matrix_bot_plugins_active gauge\n")
fmt.Fprintf(w, "mana_matrix_bot_plugins_active %d\n", len(plugins))
}