-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (87 loc) · 2.68 KB
/
main.go
File metadata and controls
103 lines (87 loc) · 2.68 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/joho/godotenv"
"app/internal"
)
func main() {
// Parse command line arguments
var tenappDir string
flag.StringVar(&tenappDir, "tenapp_dir", "", "The base folder path for tman run start command (required)")
flag.Parse()
// Validate required tenapp_dir parameter
if tenappDir == "" {
slog.Error("tenapp_dir parameter is required")
fmt.Println("Usage: ./api -tenapp_dir=<path>")
fmt.Println(" -tenapp_dir: The base folder path for tman run start command (required)")
os.Exit(1)
}
// 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)
}
// Read worker quit timeout with support for legacy misspelled var
workerQuitTimeoutEnv := os.Getenv("WORKER_QUIT_TIMEOUT_SECONDS")
if workerQuitTimeoutEnv == "" {
workerQuitTimeoutEnv = os.Getenv("WORKER_QUIT_TIMEOUT_SECONDES")
}
workerQuitTimeoutSeconds, err := strconv.Atoi(workerQuitTimeoutEnv)
if err != nil || workerQuitTimeoutSeconds <= 0 {
slog.Error("environment WORKER_QUIT_TIMEOUT_SECONDS 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,
TenappDir: tenappDir,
}
slog.Info("Server configured with tenapp_dir", "tenappDir", tenappDir)
httpServer := internal.NewHttpServer(httpServerConfig)
httpServer.Start()
}