mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-22 02:46:42 +02:00
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>
69 lines
2 KiB
Go
69 lines
2 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// HealthHandler handles health and metrics endpoints.
|
|
type HealthHandler struct {
|
|
pool *pgxpool.Pool
|
|
redis *redis.Client
|
|
startTime time.Time
|
|
}
|
|
|
|
// NewHealthHandler creates a new health handler.
|
|
func NewHealthHandler(pool *pgxpool.Pool, rdb *redis.Client) *HealthHandler {
|
|
return &HealthHandler{pool: pool, redis: rdb, startTime: time.Now()}
|
|
}
|
|
|
|
// Health handles GET /health
|
|
func (h *HealthHandler) Health(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
dbOK := "ok"
|
|
if err := h.pool.Ping(ctx); err != nil {
|
|
dbOK = "error"
|
|
}
|
|
|
|
redisOK := "ok"
|
|
if err := h.redis.Ping(ctx).Err(); err != nil {
|
|
redisOK = "error"
|
|
}
|
|
|
|
status := "ok"
|
|
if dbOK != "ok" || redisOK != "ok" {
|
|
status = "degraded"
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"status": status,
|
|
"service": "mana-api-gateway",
|
|
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
|
"uptime": time.Since(h.startTime).Seconds(),
|
|
"database": dbOK,
|
|
"redis": redisOK,
|
|
})
|
|
}
|
|
|
|
// Metrics handles GET /metrics (Prometheus format)
|
|
func (h *HealthHandler) Metrics(w http.ResponseWriter, r *http.Request) {
|
|
uptime := time.Since(h.startTime).Seconds()
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
fmt.Fprintf(w, "# HELP mana_api_gateway_uptime_seconds Gateway uptime\n")
|
|
fmt.Fprintf(w, "# TYPE mana_api_gateway_uptime_seconds gauge\n")
|
|
fmt.Fprintf(w, "mana_api_gateway_uptime_seconds %.0f\n", uptime)
|
|
|
|
// DB pool stats
|
|
stats := h.pool.Stat()
|
|
fmt.Fprintf(w, "# HELP mana_api_gateway_db_connections Database connection pool\n")
|
|
fmt.Fprintf(w, "# TYPE mana_api_gateway_db_connections gauge\n")
|
|
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"total\"} %d\n", stats.TotalConns())
|
|
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"idle\"} %d\n", stats.IdleConns())
|
|
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"acquired\"} %d\n", stats.AcquiredConns())
|
|
}
|