🔧 Update justfile with new run alias and modify main.go for config handling improvements

This commit is contained in:
2026-04-19 10:51:22 +08:00
parent 5a4afd3f0c
commit 4717b57257
2 changed files with 28 additions and 24 deletions
+24 -23
View File
@@ -20,9 +20,9 @@ import (
type Config struct {
UpstreamURL string
ListenAddr string
APIKey string
Insecure bool
ListenAddr string
APIKey string
Insecure bool
}
func main() {
@@ -55,8 +55,7 @@ func main() {
}
go func() {
fmt.Printf("LLM Proxy listening on %s\n", cfg.ListenAddr)
fmt.Printf("Upstream: %s\n", cfg.UpstreamURL)
fmt.Printf("LLM Proxy listening on %s proxy to upstream: %s\n", cfg.ListenAddr, cfg.UpstreamURL)
srv.ListenAndServe()
}()
@@ -170,13 +169,6 @@ func processChunk(w io.Writer, line string, cfg Config) {
fmt.Fprintln(w, line)
}
func getEnv(key, def string) string {
if val := os.Getenv(key); val != "" {
return val
}
return def
}
func printHelp() {
fmt.Println(`LLM Proxy - HTTP proxy for LLM APIs
@@ -211,15 +203,15 @@ func printConfig(cfg Config) {
func loadConfig() Config {
cfg := Config{
UpstreamURL: "https://api.openai.com/v1/chat/completions",
ListenAddr: ":8080",
ListenAddr: "127.0.0.1:8080",
}
if data, err := os.ReadFile("llm-proxy.toml"); err == nil {
var tomlCfg struct {
UpstreamURL string `toml:"upstream_url"`
ListenAddr string `toml:"listen_addr"`
APIKey string `toml:"api_key"`
Insecure bool `toml:"insecure"`
ListenAddr string `toml:"listen_addr"`
APIKey string `toml:"api_key"`
Insecure bool `toml:"insecure"`
}
if err := toml.Unmarshal(data, &tomlCfg); err == nil {
cfg.UpstreamURL = tomlCfg.UpstreamURL
@@ -230,18 +222,27 @@ func loadConfig() Config {
}
}
if val := os.Getenv("UPSTREAM_URL"); val != "" {
if val := getEnv("UPSTREAM_URL", "OPENAI_API_BASE"); val != "" {
cfg.UpstreamURL = val
}
if val := os.Getenv("LISTEN_ADDR"); val != "" {
cfg.ListenAddr = val
}
if val := os.Getenv("API_KEY"); val != "" {
if val := getEnv("API_KEY", "OPENAI_API_KEY"); val != "" {
cfg.APIKey = val
}
if val := os.Getenv("INSECURE"); val != "" {
if val := getEnv("LISTEN_ADDR"); val != "" {
cfg.ListenAddr = val
}
if val := getEnv("INSECURE"); val != "" {
cfg.Insecure = val == "true"
}
return cfg
}
}
func getEnv(keys ...string) string {
for _, key := range keys {
if val := os.Getenv(key); val != "" {
return val
}
}
return ""
}