main.go

 1// Package main is the main entry point for the HTTP server that serves
 2// inference providers.
 3package main
 4
 5import (
 6	"encoding/json"
 7	"log"
 8	"net/http"
 9	"time"
10
11	"github.com/charmbracelet/catwalk/internal/providers"
12	"github.com/prometheus/client_golang/prometheus"
13	"github.com/prometheus/client_golang/prometheus/promauto"
14	"github.com/prometheus/client_golang/prometheus/promhttp"
15)
16
17var counter = promauto.NewCounter(prometheus.CounterOpts{
18	Namespace: "catwalk",
19	Subsystem: "providers",
20	Name:      "requests_total",
21	Help:      "Total number of requests to the providers endpoint",
22})
23
24func providersHandler(w http.ResponseWriter, r *http.Request) {
25	if r.Method != http.MethodGet {
26		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
27		return
28	}
29
30	counter.Inc()
31	allProviders := providers.GetAll()
32	w.Header().Set("Content-Type", "application/json")
33	if err := json.NewEncoder(w).Encode(allProviders); err != nil {
34		http.Error(w, "Internal server error", http.StatusInternalServerError)
35		return
36	}
37}
38
39func main() {
40	mux := http.NewServeMux()
41	mux.HandleFunc("/providers", providersHandler)
42	mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
43		w.WriteHeader(http.StatusOK)
44		_, _ = w.Write([]byte("OK"))
45	})
46	mux.Handle("/metrics", promhttp.Handler())
47
48	server := &http.Server{
49		Addr:         ":8080",
50		Handler:      mux,
51		ReadTimeout:  15 * time.Second,
52		WriteTimeout: 15 * time.Second,
53		IdleTimeout:  60 * time.Second,
54	}
55
56	log.Println("Server starting on :8080")
57	if err := server.ListenAndServe(); err != nil {
58		log.Fatal("Server failed to start:", err)
59	}
60}