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
98 lines (83 loc) · 2.64 KB
/
Copy pathmain.go
File metadata and controls
98 lines (83 loc) · 2.64 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
package main
import (
"fmt"
"log/slog"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"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)
}
}
// 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 graph name map
internal.SetGraphNameMap()
// Process property.json
if err = processProperty(internal.PropertyJsonFile); err != nil {
slog.Error("process property.json failed", "err", err)
os.Exit(1)
}
// Start server
httpServerConfig := &internal.HttpServerConfig{
AppId: agoraAppId,
AppCertificate: os.Getenv("AGORA_APP_CERTIFICATE"),
LogPath: logPath,
Port: os.Getenv("SERVER_PORT"),
WorkersMax: workersMax,
WorkerQuitTimeoutSeconds: workerQuitTimeoutSeconds,
}
httpServer := internal.NewHttpServer(httpServerConfig)
httpServer.Start()
}
func processProperty(propertyJsonFile string) (err error) {
content, err := os.ReadFile(propertyJsonFile)
if err != nil {
slog.Error("read property.json failed", "err", err, "propertyJsonFile", propertyJsonFile)
return
}
propertyJson := string(content)
for i := range gjson.Get(propertyJson, "rte.predefined_graphs").Array() {
graph := fmt.Sprintf("rte.predefined_graphs.%d", i)
// Shut down all auto-starting Graphs
propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.auto_start`, graph), false)
// Set environment variable values to property.json
for envKey, envProps := range internal.EnvPropMap {
if envVal := os.Getenv(envKey); envVal != "" {
for _, envProp := range envProps {
propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, envProp.ExtensionName, envProp.Property), envVal)
}
}
}
}
err = os.WriteFile(propertyJsonFile, []byte(propertyJson), 0644)
return
}