onesvm-browser-server/server/internal/config/config.go
chii eb972dfa93 feat: 落地 browser-server 控制面并打通 mgr1 海外订阅
单二进制三角色 + Dock 适配器 + Swarm stack 达到可部署态;mgr1 实测订阅经 central-proxy bootstrap,探活 alive=41/52。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-02 15:05:12 +08:00

64 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package config 提供环境变量读取辅助与全局时区常量。
//
// 密钥纪律(service-secret-protocol §2):
// - MustEnv:密钥类配置缺省即 panic(fail-closed),禁止任何字面量默认值;
// - EnvDefault:仅允许用于非密钥配置(端口、阈值、路径等)。
package config
import (
"fmt"
"os"
"strconv"
"time"
)
// Timezone 组织规范东八区(development-standards §7)。
const Timezone = "Asia/Shanghai"
// TZ 全局 Location;包加载时初始化,失败 panic(时区数据属基础环境,不可降级)。
var TZ = mustLoadTZ()
func mustLoadTZ() *time.Location {
loc, err := time.LoadLocation(Timezone)
if err != nil {
panic(fmt.Sprintf("config: 加载时区 %s 失败: %v", Timezone, err))
}
return loc
}
// Now 返回东八区当前时间。全项目时间字段统一经此取值。
func Now() time.Time {
return time.Now().In(TZ)
}
// MustEnv 读取必填环境变量;缺失或空串直接 panic(fail-closed,无默认值兜底)。
// 仅用于密钥/凭据类配置。
func MustEnv(name string) string {
v := os.Getenv(name)
if v == "" {
panic(fmt.Sprintf("config: 必填环境变量 %s 未设置(fail-closed,禁默认值)", name))
}
return v
}
// EnvDefault 读取非密钥环境变量,缺省给默认值。
// 禁止传入任何密钥名(密钥必须走 MustEnv)。
func EnvDefault(name, def string) string {
if v := os.Getenv(name); v != "" {
return v
}
return def
}
// EnvDefaultInt 读取非密钥整型环境变量,解析失败给默认值。
func EnvDefaultInt(name string, def int) int {
v := os.Getenv(name)
if v == "" {
return def
}
n, err := strconv.Atoi(v)
if err != nil {
return def
}
return n
}