Documentation
¶
Overview ¶
Package running 提供进程级运行态信息与全局 CLI flag 定义。
包含环境名(dev/test/stage/prod)、debug 开关、HTTP/gRPC 端口、 实例 ID、主机名、K8s namespace 等,供日志、debug 端点和各命令共享。
Index ¶
Constants ¶
const ( EnvDev = "dev" EnvTest = "test" EnvStage = "stage" // staging EnvProd = "prod" )
Well-known runtime environment names (see EnvFlag / --runenv).
const (
// DefaultDebugPort is the default local bind port for /debug endpoints.
DefaultDebugPort = 6060
)
Variables ¶
var ( // Env 是当前运行环境,默认 EnvDev(dev/test/stage/prod,见 EnvNames)。 Env = redant.StringOf(lo.ToPtr(EnvDev)) // Debug 是否启用 debug 模式。 Debug = redant.BoolOf(lo.ToPtr(false)) // HttpPort 是默认 HTTP 监听端口。 HttpPort = redant.Int64Of(lo.ToPtr(int64(8080))) // GrpcPort 是默认 gRPC 监听端口。 GrpcPort = redant.Int64Of(lo.ToPtr(int64(50051))) // Project 返回项目名称(来自 buildinfo)。 Project = version.Project // InstanceID 是当前进程实例的唯一 ID,进程启动时生成。 InstanceID = xid.New().String() // DeviceID 是设备级唯一标识,优先使用 machineid,失败时回退为 InstanceID。 DeviceID = InstanceID Version = version.Version CommitID = version.CommitID // Pwd 是进程启动时的工作目录。 Pwd = assert.Exit1(os.Getwd()) LocalIP = netutil.GetLocalIP() Hostname = strutil.FirstFnNotEmpty( func() string { return env.Get("HOSTNAME") }, func() string { return assert.Exit1(os.Hostname()) }, ) // Namespace 是 K8s 命名空间,依次从环境变量或 serviceaccount 文件读取。 Namespace = strutil.FirstFnNotEmpty( func() string { return env.Get("NAMESPACE") }, func() string { return env.Get("POD_NAMESPACE") }, func() string { file := "/var/run/secrets/kubernetes.io/serviceaccount/namespace" if pathutil.IsNotExist(file) { return "" } return strings.TrimSpace(string(assert.Exit1(os.ReadFile(file)))) }, ) Domain = version.Domain DebugFlag = redant.Option{ Flag: "debug", Description: "enable debug mode", Value: Debug, Default: Debug.String(), Envs: []string{env.Key("enable_debug"), env.Key("debug")}, Action: func(val pflag.Value) error { env.Set("enable_debug", val.String()) env.Set("debug", val.String()) return debugs.Enabled.Set(val.String()) }, } EnvFlag = redant.Option{ Flag: "runenv", Description: "running env: dev, test, stage (staging), prod", Value: Env, Default: Env.String(), Envs: []string{env.Key("env"), env.Key("runenv")}, Action: func(val pflag.Value) error { env.Set("env", val.String()) env.Set("runenv", val.String()) return nil }, } GrpcPortFlag = redant.Option{ Flag: "grpc-port", Description: "service grpc port", Value: GrpcPort, Default: GrpcPort.String(), Envs: []string{env.Key("server_grpc_port")}, Action: func(val pflag.Value) error { env.Set("server_grpc_port", val.String()) return nil }, } HttpPortFlag = redant.Option{ Flag: "http-port", Description: "service http port", Value: HttpPort, Default: HttpPort.String(), Envs: []string{env.Key("server_http_port")}, Action: func(val pflag.Value) error { env.Set("server_http_port", val.String()) return nil }, } )
EnvNames lists all supported environment values in promotion order.
Functions ¶
func DebugListenAddr ¶ added in v2.2.0
func DebugListenAddr() string
DebugListenAddr returns the local bind address for /debug endpoints. DEBUG_ADDR overrides; otherwise DEBUG_PORT or DefaultDebugPort is used.
func EnvName ¶ added in v2.2.0
func EnvName() string
EnvName returns the current runtime environment.
func GetSysInfo ¶
GetSysInfo 返回当前进程的系统与构建信息快照,供 debug/version 命令使用。
func HttpListenAddr ¶ added in v2.2.0
func HttpListenAddr() string
HttpListenAddr returns the local HTTP bind address. HTTP_ADDR overrides; otherwise the global --http-port flag is used.
func IsDev ¶ added in v2.2.0
func IsDev() bool
IsDev reports whether the process runs in the development environment.
func IsNonProd ¶ added in v2.2.0
func IsNonProd() bool
IsNonProd reports whether the process runs outside production.
func IsProd ¶ added in v2.2.0
func IsProd() bool
IsProd reports whether the process runs in the production environment.
Types ¶
This section is empty.