package main import ( "log/slog" "net/http" "os" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) func main() { port := os.Getenv("PORT") if port == "" { port = "7860" // Default port for Hugging Face Spaces } addr := "0.0.0.0:" + port slog.Info("Starting OpenMeter Native Mock Server", "address", addr) r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.RealIP) r.Use(middleware.Logger) r.Use(middleware.Recoverer) // Redirect root to api-docs r.Get("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/api-docs", http.StatusFound) }) // /health r.Get("/health", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("OK")) }) // /api/openapi.yaml r.Get("/api/openapi.yaml", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/yaml") // We'll read the openapi.yaml copied to the container http.ServeFile(w, r, "/usr/local/bin/openapi.yaml") }) // /api-docs r.Get("/api-docs", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(` OpenMeter API Documentation (Native Mock Server)
`)) }) // GET /api/v1/meters r.Get("/api/v1/meters", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`[ { "id": "api_requests_total", "slug": "api_requests_total", "description": "API Requests", "eventType": "request", "aggregation": "COUNT" }, { "id": "tokens_total", "slug": "tokens_total", "description": "AI Token Usage", "eventType": "prompt", "aggregation": "SUM" } ]`)) }) // POST /api/v1/events r.Post("/api/v1/events", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) _, _ = w.Write([]byte(`{"status": "accepted"}`)) }) // GET /api/v1/meters/{meterIdOrSlug}/query r.Get("/api/v1/meters/{meterIdOrSlug}/query", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`{ "data": [ { "value": 150, "windowStart": "2026-07-07T00:00:00Z", "windowEnd": "2026-07-07T01:00:00Z" } ] }`)) }) err := http.ListenAndServe(addr, r) if err != nil { slog.Error("Mock server stopped with error", "error", err) os.Exit(1) } }