forked from charmbracelet/catwalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (73 loc) · 2.22 KB
/
main.go
File metadata and controls
85 lines (73 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package main is the main entry point for the HTTP server that serves
// inference providers.
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/charmbracelet/catwalk/internal/deprecated"
"github.com/charmbracelet/catwalk/internal/providers"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var counter = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "catwalk",
Subsystem: "providers",
Name: "requests_total",
Help: "Total number of requests to the providers endpoint",
})
func providersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodHead {
return
}
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
counter.Inc()
allProviders := providers.GetAll()
if err := json.NewEncoder(w).Encode(allProviders); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
func providersHandlerDeprecated(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodHead {
return
}
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
counter.Inc()
allProviders := deprecated.GetAll()
if err := json.NewEncoder(w).Encode(allProviders); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/v2/providers", providersHandler)
mux.HandleFunc("/providers", providersHandlerDeprecated)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
mux.Handle("/metrics", promhttp.Handler())
server := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
log.Println("Server starting on :8080")
if err := server.ListenAndServe(); err != nil {
log.Fatal("Server failed to start:", err)
}
}