/
app.go
57 lines (48 loc) · 1.43 KB
/
app.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
package app
import (
"fmt"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/lyquocnam/zalora_icecream/database"
"github.com/lyquocnam/zalora_icecream/lib"
"github.com/tylerb/graceful"
"time"
)
// Start app
func Run() {
lib.LoadConfig("config.yaml")
lib.ConnectDatabase()
database.Migrate()
router := echo.New()
router.Use(middleware.Gzip())
router.Use(middleware.Recover())
router.Use(middleware.RemoveTrailingSlash())
router.Static("/", "public")
router.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &lib.CustomContext{Context: c}
return h(cc)
}
})
// load all routes
loadRoutes(router)
// print app info
printInfo()
router.Server.Addr = fmt.Sprintf(`:%v`, lib.Config.AppPort)
router.Logger.Fatal(graceful.ListenAndServe(router.Server, 5*time.Second))
}
func printInfo() {
mode := "PRODUCTION"
if lib.Config.Environment == "development" {
mode = "DEVELOPMENT"
} else if lib.Config.Environment == "staging" {
mode = "STAGING"
}
fmt.Println("----------------------------------------")
fmt.Println(fmt.Sprintf(`- %v`, lib.Config.AppName))
fmt.Println(fmt.Sprintf(`- Version: %v`, lib.Config.AppVersion))
fmt.Println(fmt.Sprintf(`- Environment: %v`, mode))
fmt.Println(fmt.Sprintf(`- Author: %v`, lib.Config.Author))
fmt.Println(fmt.Sprintf(`- Port: %v`, lib.Config.AppPort))
fmt.Println("----------------------------------------")
}