-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (62 loc) · 1.34 KB
/
main.go
File metadata and controls
76 lines (62 loc) · 1.34 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
package main
import (
"errors"
"log"
"os"
"github.com/daqing/airway/cmd"
"github.com/daqing/airway/lib/redis_client"
"github.com/daqing/airway/lib/repo"
"github.com/daqing/airway/lib/utils"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
args := os.Args[1:]
isCLICommand := len(args) > 0 && args[0] == "cli"
if isCLICommand {
loadCLIEnv()
cmd.Run(args)
return
}
appConfig := utils.AppConfig()
if !isCLICommand && appConfig.Env == "" {
log.Println("AIRWAY_ENV is not set")
os.Exit(1)
}
if !isCLICommand && appConfig.IsLocal {
envFile := ".env"
err := godotenv.Load(envFile)
if err != nil {
log.Printf("Loading env file: %s failed", envFile)
os.Exit(2)
}
} else {
gin.SetMode(gin.ReleaseMode)
}
dsn, err := utils.GetEnv("AIRWAY_DB_DSN")
if err == nil {
if _, setupErr := repo.SetupDB(dsn); setupErr != nil {
log.Printf("database setup failed: %v", setupErr)
os.Exit(3)
}
}
redisURL, err := utils.GetEnv("AIRWAY_REDIS")
if err == nil {
redis_client.Setup(redisURL)
}
if len(args) > 0 {
cmd.Run(args)
return
}
runApp()
}
func loadCLIEnv() {
err := godotenv.Load(".env")
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("Loading env file: .env failed: %v", err)
}
}
func runApp() {
app := NewApp("Airway", utils.GetEnvMust("AIRWAY_PORT"))
app.Run()
}