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

View File

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