From 5b67b66611e8e976b3cc55414bf3151d5c6b6465 Mon Sep 17 00:00:00 2001 From: Sakurasan <26715255+Sakurasan@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E7=BD=B2:=20=E6=94=AF=E6=8C=81=20.env?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E6=B3=A8=E5=85=A5=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F(OT=5F=20=E5=89=8D=E7=BC=80),=20nginx-public?= =?UTF-8?q?=20=E5=85=AC=E7=BD=91=E5=8D=95=E7=AB=AF=E5=8F=A3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - config: loadDotEnv() 读取 .env 注入 os env, AutomaticEnv 统一映射 OT_ 前缀 - deploy/nginx-public.conf: 宿主 8088 单端口, 前端静态 + /api /v1 反代 loopback (SSE 关缓冲), host 网络避开 iptables DNAT --- deploy/nginx-public.conf | 45 ++++++++++++++++++++++++++++++++ server/internal/config/config.go | 26 ++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 deploy/nginx-public.conf diff --git a/deploy/nginx-public.conf b/deploy/nginx-public.conf new file mode 100644 index 0000000..37e953f --- /dev/null +++ b/deploy/nginx-public.conf @@ -0,0 +1,45 @@ +# openteam 公网部署(IP+端口方式):宿主 8088 单端口入口 +# nginx 容器使用 host 网络,反代走 loopback 避开 DNAT 干扰 +server { + listen 8088; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + # SPA 路由回退 + location / { + try_files $uri $uri/ /index.html; + } + + # 管理 API + 代理端点 → 本机 openteam(127.0.0.1 loopback 不经 iptables DNAT) + location /api/ { + proxy_pass http://127.0.0.1:8080; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + location /v1/ { + proxy_pass http://127.0.0.1:8080; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + # SSE 流式透传:关闭缓冲 + proxy_buffering off; + proxy_cache off; + proxy_read_timeout 300s; + } + + # 静态资源缓存 + location /assets/ { + expires 30d; + add_header Cache-Control "public, immutable"; + } + + gzip on; + gzip_types text/plain text/css application/json application/javascript image/svg+xml; +} diff --git a/server/internal/config/config.go b/server/internal/config/config.go index 3ca3b2b..f6e9a3f 100644 --- a/server/internal/config/config.go +++ b/server/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( + "os" "strings" "time" @@ -50,7 +51,32 @@ type ProxyConfig struct { Timeout time.Duration } +// loadDotEnv 读取 .env 文件并把 KEY=VALUE 注入环境变量(AutomaticEnv 会自动映射 OT_ 前缀)。 +func loadDotEnv() { + data, err := os.ReadFile(".env") + if err != nil { + return + } + for _, line := range strings.Split(string(data), "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + k, v, ok := strings.Cut(line, "=") + if !ok { + continue + } + k = strings.TrimSpace(k) + v = strings.Trim(strings.TrimSpace(v), `"'`) + if k != "" && os.Getenv(k) == "" { + _ = os.Setenv(k, v) + } + } +} + func Load() (*Config, error) { + loadDotEnv() + v := viper.New() v.SetEnvPrefix("OT") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))