Skip to content

Commit d508af9

Browse files
ossrs-aiwinlinvip
authored andcommitted
AI: Refine project structure.
1 parent 154d073 commit d508af9

File tree

26 files changed

+1198
-779
lines changed

26 files changed

+1198
-779
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ all: build
44

55
build: fmt ./srs-proxy
66

7-
./srs-proxy: *.go
8-
go build -o srs-proxy .
7+
./srs-proxy: cmd/proxy-go/*.go internal/**/*.go
8+
go build -o srs-proxy ./cmd/proxy-go
99

1010
test:
1111
go test ./...
1212

1313
fmt: ./.go-formarted
1414

15-
./.go-formarted: *.go
15+
./.go-formarted: cmd/proxy-go/*.go internal/**/*.go
1616
touch .go-formarted
1717
go fmt ./...
1818

1919
clean:
2020
rm -f srs-proxy .go-formarted
2121

2222
run: fmt
23-
go run .
23+
go run ./cmd/proxy-go

main.go renamed to cmd/proxy-go/main.go

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ import (
77
"context"
88
"os"
99

10-
"srs-proxy/errors"
11-
"srs-proxy/logger"
10+
"srs-proxy/internal/debug"
11+
"srs-proxy/internal/env"
12+
"srs-proxy/internal/errors"
13+
"srs-proxy/internal/lb"
14+
"srs-proxy/internal/logger"
15+
"srs-proxy/internal/protocol"
16+
"srs-proxy/internal/signal"
17+
"srs-proxy/internal/utils"
18+
"srs-proxy/internal/version"
1219
)
1320

1421
func main() {
1522
ctx := logger.WithContext(context.Background())
16-
logger.Df(ctx, "%v/%v started", Signature(), Version())
23+
logger.Df(ctx, "%v/%v started", version.Signature(), version.Version())
1724

1825
// Install signals.
1926
ctx, cancel := context.WithCancel(ctx)
20-
installSignals(ctx, cancel)
27+
signal.InstallSignals(ctx, cancel)
2128

2229
// Start the main loop, ignore the user cancel error.
2330
err := doMain(ctx)
@@ -26,90 +33,102 @@ func main() {
2633
os.Exit(-1)
2734
}
2835

29-
logger.Df(ctx, "%v done", Signature())
36+
logger.Df(ctx, "%v done", version.Signature())
3037
}
3138

3239
func doMain(ctx context.Context) error {
3340
// Setup the environment variables.
34-
if err := loadEnvFile(ctx); err != nil {
41+
if err := env.LoadEnvFile(ctx); err != nil {
3542
return errors.Wrapf(err, "load env")
3643
}
3744

38-
buildDefaultEnvironmentVariables(ctx)
45+
env.BuildDefaultEnvironmentVariables(ctx)
3946

4047
// When cancelled, the program is forced to exit due to a timeout. Normally, this doesn't occur
4148
// because the main thread exits after the context is cancelled. However, sometimes the main thread
4249
// may be blocked for some reason, so a forced exit is necessary to ensure the program terminates.
43-
if err := installForceQuit(ctx); err != nil {
50+
if err := signal.InstallForceQuit(ctx); err != nil {
4451
return errors.Wrapf(err, "install force quit")
4552
}
4653

4754
// Start the Go pprof if enabled.
48-
handleGoPprof(ctx)
55+
debug.HandleGoPprof(ctx)
4956

50-
// Initialize SRS load balancers.
51-
switch lbType := envLoadBalancerType(); lbType {
52-
case "memory":
53-
srsLoadBalancer = NewMemoryLoadBalancer()
57+
// Initialize the load balancer.
58+
switch env.EnvLoadBalancerType() {
5459
case "redis":
55-
srsLoadBalancer = NewRedisLoadBalancer()
60+
lb.SrsLoadBalancer = lb.NewRedisLoadBalancer(
61+
env.EnvRedisHost,
62+
env.EnvRedisPort,
63+
env.EnvRedisPassword,
64+
env.EnvRedisDB,
65+
env.EnvDefaultBackendEnabled,
66+
env.EnvDefaultBackendIP,
67+
env.EnvDefaultBackendRTMP,
68+
env.EnvDefaultBackendHttp,
69+
env.EnvDefaultBackendAPI,
70+
env.EnvDefaultBackendRTC,
71+
env.EnvDefaultBackendSRT,
72+
)
5673
default:
57-
return errors.Errorf("invalid load balancer %v", lbType)
74+
lb.SrsLoadBalancer = lb.NewMemoryLoadBalancer(
75+
env.EnvDefaultBackendEnabled,
76+
env.EnvDefaultBackendIP,
77+
env.EnvDefaultBackendRTMP,
78+
env.EnvDefaultBackendHttp,
79+
env.EnvDefaultBackendAPI,
80+
env.EnvDefaultBackendRTC,
81+
env.EnvDefaultBackendSRT,
82+
)
5883
}
5984

60-
if err := srsLoadBalancer.Initialize(ctx); err != nil {
85+
if err := lb.SrsLoadBalancer.Initialize(ctx); err != nil {
6186
return errors.Wrapf(err, "initialize srs load balancer")
6287
}
6388

6489
// Parse the gracefully quit timeout.
65-
gracefulQuitTimeout, err := parseGracefullyQuitTimeout()
90+
gracefulQuitTimeout, err := utils.ParseGracefullyQuitTimeout()
6691
if err != nil {
6792
return errors.Wrapf(err, "parse gracefully quit timeout")
6893
}
6994

7095
// Start the RTMP server.
71-
srsRTMPServer := NewSRSRTMPServer()
96+
srsRTMPServer := protocol.NewSRSRTMPServer()
7297
defer srsRTMPServer.Close()
7398
if err := srsRTMPServer.Run(ctx); err != nil {
7499
return errors.Wrapf(err, "rtmp server")
75100
}
76101

77102
// Start the WebRTC server.
78-
srsWebRTCServer := NewSRSWebRTCServer()
103+
srsWebRTCServer := protocol.NewSRSWebRTCServer()
79104
defer srsWebRTCServer.Close()
80105
if err := srsWebRTCServer.Run(ctx); err != nil {
81106
return errors.Wrapf(err, "rtc server")
82107
}
83108

84109
// Start the HTTP API server.
85-
srsHTTPAPIServer := NewSRSHTTPAPIServer(func(server *srsHTTPAPIServer) {
86-
server.gracefulQuitTimeout, server.rtc = gracefulQuitTimeout, srsWebRTCServer
87-
})
110+
srsHTTPAPIServer := protocol.NewSRSHTTPAPIServer(gracefulQuitTimeout, srsWebRTCServer)
88111
defer srsHTTPAPIServer.Close()
89112
if err := srsHTTPAPIServer.Run(ctx); err != nil {
90113
return errors.Wrapf(err, "http api server")
91114
}
92115

93116
// Start the SRT server.
94-
srsSRTServer := NewSRSSRTServer()
117+
srsSRTServer := protocol.NewSRSSRTServer()
95118
defer srsSRTServer.Close()
96119
if err := srsSRTServer.Run(ctx); err != nil {
97120
return errors.Wrapf(err, "srt server")
98121
}
99122

100123
// Start the System API server.
101-
systemAPI := NewSystemAPI(func(server *systemAPI) {
102-
server.gracefulQuitTimeout = gracefulQuitTimeout
103-
})
124+
systemAPI := protocol.NewSystemAPI(gracefulQuitTimeout)
104125
defer systemAPI.Close()
105126
if err := systemAPI.Run(ctx); err != nil {
106127
return errors.Wrapf(err, "system api server")
107128
}
108129

109130
// Start the HTTP web server.
110-
srsHTTPStreamServer := NewSRSHTTPStreamServer(func(server *srsHTTPStreamServer) {
111-
server.gracefulQuitTimeout = gracefulQuitTimeout
112-
})
131+
srsHTTPStreamServer := protocol.NewSRSHTTPStreamServer(gracefulQuitTimeout)
113132
defer srsHTTPStreamServer.Close()
114133
if err := srsHTTPStreamServer.Run(ctx); err != nil {
115134
return errors.Wrapf(err, "http server")

doc/files.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Codebase Structure
2+
3+
This document provides an overview of the proxy-go codebase organization.
4+
5+
## Directory Structure
6+
7+
```
8+
proxy-go/
9+
├── cmd/proxy-go/
10+
│ └── main.go # Application entry point
11+
└── internal/
12+
├── debug/ # Go profiling support
13+
├── env/ # Configuration management
14+
├── errors/ # Error handling with stack traces
15+
├── lb/ # Load balancer (memory/Redis)
16+
├── logger/ # Logging and request tracing
17+
├── protocol/ # Protocol servers (RTMP, HTTP, WebRTC, SRT, API)
18+
├── rtmp/ # RTMP protocol implementation
19+
├── signal/ # Graceful shutdown handling
20+
├── sync/ # Concurrency utilities
21+
├── utils/ # Common utilities
22+
└── version/ # Version information
23+
```
24+
25+
## Internal Packages
26+
27+
### debug
28+
Go profiling support via pprof, controlled by `GO_PPROF` environment variable.
29+
30+
### env
31+
Configuration management using environment variables. Loads `.env` file and provides defaults for all server settings.
32+
33+
### errors
34+
Enhanced error handling with stack traces. Provides error wrapping and root cause extraction.
35+
36+
### lb
37+
Load balancer system supporting both single-proxy (memory-based) and multi-proxy (Redis-based) deployments.
38+
- `lb.go` - Core interfaces and types
39+
- `mem.go` - Memory-based load balancer
40+
- `redis.go` - Redis-based load balancer
41+
- `debug.go` - Default backend for testing
42+
43+
### logger
44+
Structured logging with context-based request tracing. Provides log levels: Verbose, Debug, Warning, Error.
45+
46+
### protocol
47+
Protocol server implementations for all supported streaming protocols:
48+
- `rtmp.go` - RTMP proxy server
49+
- `http.go` - HTTP streaming (HLS, HTTP-FLV, HTTP-TS)
50+
- `rtc.go` - WebRTC server (WHIP/WHEP)
51+
- `srt.go` - SRT server
52+
- `api.go` - HTTP API server
53+
54+
### rtmp
55+
Low-level RTMP protocol implementation including handshake and AMF0 serialization.
56+
57+
### signal
58+
Graceful shutdown coordination. Catches SIGINT/SIGTERM and implements timeout-based shutdown.
59+
60+
### sync
61+
Thread-safe generic Map wrapper around `sync.Map` for connection tracking and caching.
62+
63+
### utils
64+
Common utility functions for HTTP responses, JSON marshaling, and parsing.
65+
66+
### version
67+
Version information and server identification.

0 commit comments

Comments
 (0)