use gin instead of standard http lib in main

This commit is contained in:
Benson Wong
2024-11-18 15:58:28 -08:00
parent a33ac6f8fb
commit c9233d2c9a
2 changed files with 13 additions and 5 deletions

View File

@@ -3,9 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"net/http"
"os" "os"
"github.com/gin-gonic/gin"
"github.com/mostlygeek/llama-swap/proxy" "github.com/mostlygeek/llama-swap/proxy"
) )
@@ -22,12 +22,16 @@ func main() {
os.Exit(1) os.Exit(1)
} }
proxyManager := proxy.New(config) if mode := os.Getenv("GIN_MODE"); mode != "" {
http.HandleFunc("/", proxyManager.HandlerFunc) gin.SetMode(mode)
} else {
gin.SetMode(gin.ReleaseMode)
}
proxyManager := proxy.New(config)
fmt.Println("llama-swap listening on " + *listenStr) fmt.Println("llama-swap listening on " + *listenStr)
if err := http.ListenAndServe(*listenStr, nil); err != nil { if err := proxyManager.Run(*listenStr); err != nil {
fmt.Printf("Error starting server: %v\n", err) fmt.Printf("Server error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} }

View File

@@ -46,6 +46,10 @@ func New(config *Config) *ProxyManager {
return pm return pm
} }
func (pm *ProxyManager) Run(addr ...string) error {
return pm.ginEngine.Run(addr...)
}
func (pm *ProxyManager) HandlerFunc(w http.ResponseWriter, r *http.Request) { func (pm *ProxyManager) HandlerFunc(w http.ResponseWriter, r *http.Request) {
pm.ginEngine.ServeHTTP(w, r) pm.ginEngine.ServeHTTP(w, r)
} }