From 4717b5725786f0eaea13b96245acfdf96caf9175 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 19 Apr 2026 10:51:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Update=20justfile=20with=20new?= =?UTF-8?q?=20run=20alias=20and=20modify=20main.go=20for=20config=20handli?= =?UTF-8?q?ng=20improvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- justfile | 5 ++++- main.go | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/justfile b/justfile index e35e16a..e6e8010 100644 --- a/justfile +++ b/justfile @@ -1,11 +1,14 @@ _: @just --list - alias b:=build +alias r:=run # Build llm-proxy build: go build +# Run llm-proxy +run: + te -X -k $(cat ~/.config/envs/LCOAL_DEFAULT_TINY_ENCRYPT_KEY_ID) ~/secrets/alibaba-cloud-ai-cn.env.tinyenc -- go run main.go diff --git a/main.go b/main.go index fbd1d73..900bb00 100644 --- a/main.go +++ b/main.go @@ -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 -} \ No newline at end of file +} + +func getEnv(keys ...string) string { + for _, key := range keys { + if val := os.Getenv(key); val != "" { + return val + } + } + return "" +}