-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (48 loc) · 1.33 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
package main
import (
"bytes"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/gopher-lego/skeleton/route"
"github.com/gopher-lego/skeleton/config"
)
var AppConf = viper.New()
/**
* curl -X GET -d "s=a" http://localhost:8090/api/ping
*/
func main() {
if gin.Mode() == gin.ReleaseMode {
// https://github.com/go-bindata/go-bindata#accessing-an-asset
filename := "setting/app." + gin.Mode() + ".json"
bytesContent, err := Asset(filename)
if err != nil {
panic("Asset() can not found setting file")
}
// https://github.com/spf13/viper#reading-config-from-ioreader
AppConf.SetConfigType("json")
if err := AppConf.ReadConfig(bytes.NewBuffer(bytesContent)); err != nil {
panic("Something error:" + err.Error())
}
} else {
filenameWithoutExt := "app." + gin.Mode()
AppConf.SetConfigName(filenameWithoutExt)
AppConf.SetConfigType("json")
AppConf.AddConfigPath("./setting")
if err := AppConf.ReadInConfig(); err != nil {
panic("Using config file:" + AppConf.ConfigFileUsed())
}
}
// Assign config for outside using
config.Global(AppConf)
// Memory cache
config.NewFreeCache()
// Framework engine
engine := gin.Default()
// Routes
route.Set(engine)
// Run
err := engine.Run(config.Get("server.port")) // listen and serve on 0.0.0.0:port
if err != nil {
panic(err.Error())
}
}