Reorganize the SRS (Simple Realtime Server) repository to follow a conventional Go project structure, setting the stage for a progressive transition from a C++ project to a Go project. The proxy, which was once contained within its own `proxy/` subdirectory, will now be converted into the initial Go module located at the root of the repository, serving as a template for subsequent Go modules. - **Go module at repo root:** `go.mod` moved to repo root, module renamed from `proxy` to `srsx`. The repo is now a proper Go project with `cmd/` and `internal/` at the top level. - **Elevation of Proxy Code:** Move the proxy code from `proxy/cmd/proxy-go/` to `cmd/proxy/`, and from `proxy/internal/` to `internal/`. The proxy serves as the inaugural application; subsequent modules (for instance, `cmd/origin`) will mimic this arrangement. - **Documentation Restructured:** Transfer the documentation from `proxy/docs/` to `docs/proxy/`, revise the main README to endorse OpenClaw as the preferred AI tool, and update `proxy/README.md` to point to the new documentation locations. - **Build and config:** `Makefile` moved to root, `PROXY_STATIC_FILES` default path corrected for the new layout, `.gitignore` consolidated. - **Cleanup:** removed standalone `proxy/LICENSE` (repo-level license applies), all internal imports updated to `srsx/internal/...`. - **OpenClaw workspace:** added community bot info, git workflow conventions, and support group behavior guidance. This restructuring was performed by OpenClaw orchestrating Claude Code and Codex via ACP. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
254 lines
7.5 KiB
Go
254 lines
7.5 KiB
Go
// Copyright (c) 2025 Winlin
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
package env
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"srsx/internal/errors"
|
|
"srsx/internal/logger"
|
|
)
|
|
|
|
// Environment provides access to environment variables.
|
|
type Environment interface {
|
|
// Go pprof profiling
|
|
GoPprof() string
|
|
// Graceful quit timeout
|
|
GraceQuitTimeout() string
|
|
// Force quit timeout
|
|
ForceQuitTimeout() string
|
|
// HTTP API server port
|
|
HttpAPI() string
|
|
// HTTP web server port
|
|
HttpServer() string
|
|
// RTMP media server port
|
|
RtmpServer() string
|
|
// WebRTC media server port (UDP)
|
|
WebRTCServer() string
|
|
// SRT media server port (UDP)
|
|
SRTServer() string
|
|
// System API server port
|
|
SystemAPI() string
|
|
// Static files directory
|
|
StaticFiles() string
|
|
// Load balancer type (memory or redis)
|
|
LoadBalancerType() string
|
|
// Redis host
|
|
RedisHost() string
|
|
// Redis port
|
|
RedisPort() string
|
|
// Redis password
|
|
RedisPassword() string
|
|
// Redis database
|
|
RedisDB() string
|
|
// Default backend enabled
|
|
DefaultBackendEnabled() string
|
|
// Default backend IP
|
|
DefaultBackendIP() string
|
|
// Default backend RTMP port
|
|
DefaultBackendRTMP() string
|
|
// Default backend HTTP port
|
|
DefaultBackendHttp() string
|
|
// Default backend API port
|
|
DefaultBackendAPI() string
|
|
// Default backend RTC port (UDP)
|
|
DefaultBackendRTC() string
|
|
// Default backend SRT port (UDP)
|
|
DefaultBackendSRT() string
|
|
}
|
|
|
|
type environment struct{}
|
|
|
|
// NewEnvironment creates a new Environment instance, loading and building default environment variables.
|
|
func NewEnvironment(ctx context.Context) (Environment, error) {
|
|
if err := loadEnvFile(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
buildDefaultEnvironmentVariables(ctx)
|
|
return &environment{}, nil
|
|
}
|
|
|
|
func (e *environment) GoPprof() string {
|
|
return os.Getenv("GO_PPROF")
|
|
}
|
|
|
|
func (e *environment) GraceQuitTimeout() string {
|
|
return os.Getenv("PROXY_GRACE_QUIT_TIMEOUT")
|
|
}
|
|
|
|
func (e *environment) ForceQuitTimeout() string {
|
|
return os.Getenv("PROXY_FORCE_QUIT_TIMEOUT")
|
|
}
|
|
|
|
func (e *environment) HttpAPI() string {
|
|
return os.Getenv("PROXY_HTTP_API")
|
|
}
|
|
|
|
func (e *environment) HttpServer() string {
|
|
return os.Getenv("PROXY_HTTP_SERVER")
|
|
}
|
|
|
|
func (e *environment) RtmpServer() string {
|
|
return os.Getenv("PROXY_RTMP_SERVER")
|
|
}
|
|
|
|
func (e *environment) WebRTCServer() string {
|
|
return os.Getenv("PROXY_WEBRTC_SERVER")
|
|
}
|
|
|
|
func (e *environment) SRTServer() string {
|
|
return os.Getenv("PROXY_SRT_SERVER")
|
|
}
|
|
|
|
func (e *environment) SystemAPI() string {
|
|
return os.Getenv("PROXY_SYSTEM_API")
|
|
}
|
|
|
|
func (e *environment) StaticFiles() string {
|
|
return os.Getenv("PROXY_STATIC_FILES")
|
|
}
|
|
|
|
func (e *environment) LoadBalancerType() string {
|
|
return os.Getenv("PROXY_LOAD_BALANCER_TYPE")
|
|
}
|
|
|
|
func (e *environment) RedisHost() string {
|
|
return os.Getenv("PROXY_REDIS_HOST")
|
|
}
|
|
|
|
func (e *environment) RedisPort() string {
|
|
return os.Getenv("PROXY_REDIS_PORT")
|
|
}
|
|
|
|
func (e *environment) RedisPassword() string {
|
|
return os.Getenv("PROXY_REDIS_PASSWORD")
|
|
}
|
|
|
|
func (e *environment) RedisDB() string {
|
|
return os.Getenv("PROXY_REDIS_DB")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendEnabled() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_ENABLED")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendIP() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_IP")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendRTMP() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_RTMP")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendHttp() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_HTTP")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendAPI() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_API")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendRTC() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_RTC")
|
|
}
|
|
|
|
func (e *environment) DefaultBackendSRT() string {
|
|
return os.Getenv("PROXY_DEFAULT_BACKEND_SRT")
|
|
}
|
|
|
|
// loadEnvFile loads the environment variables from .env file.
|
|
func loadEnvFile(ctx context.Context) error {
|
|
if err := godotenv.Load(); err != nil {
|
|
// If .env file doesn't exist, that's okay, just log and continue
|
|
if os.IsNotExist(err) {
|
|
logger.Df(ctx, "no .env file found, skipping")
|
|
return nil
|
|
}
|
|
return errors.Wrapf(err, "load .env file")
|
|
}
|
|
logger.Df(ctx, "successfully loaded .env file")
|
|
return nil
|
|
}
|
|
|
|
// buildDefaultEnvironmentVariables setups the default environment variables.
|
|
func buildDefaultEnvironmentVariables(ctx context.Context) {
|
|
// Whether enable the Go pprof.
|
|
setEnvDefault("GO_PPROF", "")
|
|
// Force shutdown timeout.
|
|
setEnvDefault("PROXY_FORCE_QUIT_TIMEOUT", "30s")
|
|
// Graceful quit timeout.
|
|
setEnvDefault("PROXY_GRACE_QUIT_TIMEOUT", "20s")
|
|
|
|
// The HTTP API server.
|
|
setEnvDefault("PROXY_HTTP_API", "11985")
|
|
// The HTTP web server.
|
|
setEnvDefault("PROXY_HTTP_SERVER", "18080")
|
|
// The RTMP media server.
|
|
setEnvDefault("PROXY_RTMP_SERVER", "11935")
|
|
// The WebRTC media server, via UDP protocol.
|
|
setEnvDefault("PROXY_WEBRTC_SERVER", "18000")
|
|
// The SRT media server, via UDP protocol.
|
|
setEnvDefault("PROXY_SRT_SERVER", "20080")
|
|
// The API server of proxy itself.
|
|
setEnvDefault("PROXY_SYSTEM_API", "12025")
|
|
// The static directory for web server, optional.
|
|
setEnvDefault("PROXY_STATIC_FILES", "./trunk/research")
|
|
|
|
// The load balancer, use redis or memory.
|
|
setEnvDefault("PROXY_LOAD_BALANCER_TYPE", "memory")
|
|
// The redis server host.
|
|
setEnvDefault("PROXY_REDIS_HOST", "127.0.0.1")
|
|
// The redis server port.
|
|
setEnvDefault("PROXY_REDIS_PORT", "6379")
|
|
// The redis server password.
|
|
setEnvDefault("PROXY_REDIS_PASSWORD", "")
|
|
// The redis server db.
|
|
setEnvDefault("PROXY_REDIS_DB", "0")
|
|
|
|
// Whether enable the default backend server, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_ENABLED", "off")
|
|
// Default backend server IP, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_IP", "127.0.0.1")
|
|
// Default backend server port, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_RTMP", "1935")
|
|
// Default backend api port, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_API", "1985")
|
|
// Default backend udp rtc port, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_RTC", "8000")
|
|
// Default backend udp srt port, for debugging.
|
|
setEnvDefault("PROXY_DEFAULT_BACKEND_SRT", "10080")
|
|
|
|
logger.Df(ctx, "load .env as GO_PPROF=%v, "+
|
|
"PROXY_FORCE_QUIT_TIMEOUT=%v, PROXY_GRACE_QUIT_TIMEOUT=%v, "+
|
|
"PROXY_HTTP_API=%v, PROXY_HTTP_SERVER=%v, PROXY_RTMP_SERVER=%v, "+
|
|
"PROXY_WEBRTC_SERVER=%v, PROXY_SRT_SERVER=%v, "+
|
|
"PROXY_SYSTEM_API=%v, PROXY_STATIC_FILES=%v, PROXY_DEFAULT_BACKEND_ENABLED=%v, "+
|
|
"PROXY_DEFAULT_BACKEND_IP=%v, PROXY_DEFAULT_BACKEND_RTMP=%v, "+
|
|
"PROXY_DEFAULT_BACKEND_HTTP=%v, PROXY_DEFAULT_BACKEND_API=%v, "+
|
|
"PROXY_DEFAULT_BACKEND_RTC=%v, PROXY_DEFAULT_BACKEND_SRT=%v, "+
|
|
"PROXY_LOAD_BALANCER_TYPE=%v, PROXY_REDIS_HOST=%v, PROXY_REDIS_PORT=%v, "+
|
|
"PROXY_REDIS_PASSWORD=%v, PROXY_REDIS_DB=%v",
|
|
os.Getenv("GO_PPROF"),
|
|
os.Getenv("PROXY_FORCE_QUIT_TIMEOUT"), os.Getenv("PROXY_GRACE_QUIT_TIMEOUT"),
|
|
os.Getenv("PROXY_HTTP_API"), os.Getenv("PROXY_HTTP_SERVER"), os.Getenv("PROXY_RTMP_SERVER"),
|
|
os.Getenv("PROXY_WEBRTC_SERVER"), os.Getenv("PROXY_SRT_SERVER"),
|
|
os.Getenv("PROXY_SYSTEM_API"), os.Getenv("PROXY_STATIC_FILES"), os.Getenv("PROXY_DEFAULT_BACKEND_ENABLED"),
|
|
os.Getenv("PROXY_DEFAULT_BACKEND_IP"), os.Getenv("PROXY_DEFAULT_BACKEND_RTMP"),
|
|
os.Getenv("PROXY_DEFAULT_BACKEND_HTTP"), os.Getenv("PROXY_DEFAULT_BACKEND_API"),
|
|
os.Getenv("PROXY_DEFAULT_BACKEND_RTC"), os.Getenv("PROXY_DEFAULT_BACKEND_SRT"),
|
|
os.Getenv("PROXY_LOAD_BALANCER_TYPE"), os.Getenv("PROXY_REDIS_HOST"), os.Getenv("PROXY_REDIS_PORT"),
|
|
os.Getenv("PROXY_REDIS_PASSWORD"), os.Getenv("PROXY_REDIS_DB"),
|
|
)
|
|
}
|
|
|
|
// setEnvDefault set env key=value if not set.
|
|
func setEnvDefault(key, value string) {
|
|
if os.Getenv(key) == "" {
|
|
os.Setenv(key, value)
|
|
}
|
|
}
|