forked from TEN-framework/ten-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (68 loc) · 1.92 KB
/
Copy pathmain.go
File metadata and controls
80 lines (68 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"log/slog"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/joho/godotenv"
"app/internal"
)
func main() {
// Load .env
err := godotenv.Load()
if err != nil {
slog.Warn("load .env file failed", "err", err)
}
// Check if the directory exists
logPath := os.Getenv("LOG_PATH")
if _, err := os.Stat(logPath); os.IsNotExist(err) {
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
slog.Error("create log directory failed", "err", err)
os.Exit(1)
}
}
log2Stdout, err := strconv.ParseBool(os.Getenv("LOG_STDOUT"))
if err != nil {
slog.Error("environment LOG_STDOUT invalid")
log2Stdout = false
}
// Check environment
agoraAppId := os.Getenv("AGORA_APP_ID")
if len(agoraAppId) != 32 {
slog.Error("environment AGORA_APP_ID invalid")
os.Exit(1)
}
workersMax, err := strconv.Atoi(os.Getenv("WORKERS_MAX"))
if err != nil || workersMax <= 0 {
slog.Error("environment WORKERS_MAX invalid")
os.Exit(1)
}
workerQuitTimeoutSeconds, err := strconv.Atoi(os.Getenv("WORKER_QUIT_TIMEOUT_SECONDES"))
if err != nil || workerQuitTimeoutSeconds <= 0 {
slog.Error("environment WORKER_QUIT_TIMEOUT_SECONDES invalid")
os.Exit(1)
}
// Set up signal handler to clean up all workers on Ctrl+C
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
fmt.Println("Received interrupt signal, cleaning up workers...")
internal.CleanWorkers()
os.Exit(0)
}()
// Start server
httpServerConfig := &internal.HttpServerConfig{
AppId: agoraAppId,
AppCertificate: os.Getenv("AGORA_APP_CERTIFICATE"),
LogPath: logPath,
Port: os.Getenv("SERVER_PORT"),
WorkersMax: workersMax,
WorkerQuitTimeoutSeconds: workerQuitTimeoutSeconds,
Log2Stdout: log2Stdout,
}
httpServer := internal.NewHttpServer(httpServerConfig)
httpServer.Start()
}