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/fur/internal/providers"
12)
13
14func providersHandler(w http.ResponseWriter, r *http.Request) {
15	if r.Method != http.MethodGet {
16		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
17		return
18	}
19
20	allProviders := providers.GetAll()
21
22	w.Header().Set("Content-Type", "application/json")
23	if err := json.NewEncoder(w).Encode(allProviders); err != nil {
24		http.Error(w, "Internal server error", http.StatusInternalServerError)
25		return
26	}
27}
28
29func main() {
30	mux := http.NewServeMux()
31	mux.HandleFunc("/providers", providersHandler)
32	mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
33		w.WriteHeader(http.StatusOK)
34		_, _ = w.Write([]byte("OK"))
35	})
36
37	server := &http.Server{
38		Addr:         ":8080",
39		Handler:      mux,
40		ReadTimeout:  15 * time.Second,
41		WriteTimeout: 15 * time.Second,
42		IdleTimeout:  60 * time.Second,
43	}
44
45	log.Println("Server starting on :8080")
46	if err := server.ListenAndServe(); err != nil {
47		log.Fatal("Server failed to start:", err)
48	}
49}