-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
72 lines (54 loc) · 1.05 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"github.com/daqing/airway/config"
"github.com/daqing/airway/lib/orm"
"github.com/daqing/airway/lib/utils"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
appConfig := utils.AppConfig()
if appConfig.Env == "" {
log.Println("AIRWAY_ENV is not set")
os.Exit(1)
}
if 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)
}
orm.Setup()
app := NewApp()
r := app.Router()
r.Use(cors.Default())
config.Routes(r)
port := utils.GetEnvMust("AIRWAY_PORT")
fmt.Printf("Airway running at: http://127.0.0.1:%s\n", port)
app.Run(":" + port)
}
type App struct {
r *gin.Engine
}
func NewApp() *App {
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
return &App{
r: router,
}
}
func (a *App) Router() *gin.Engine {
return a.r
}
func (a *App) Run(addr string) {
a.r.Run(addr)
}