-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.54 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
package main
import (
"fmt"
"log"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/mdanialr/api-mock/api"
"github.com/spf13/viper"
)
func main() {
vApp := initAppConfig()
if err := vApp.ReadInConfig(); err != nil {
log.Fatalln("failed to init app config:", err)
}
responsePath := vApp.GetString("response_dir")
responseName := vApp.GetString("response_name")
vResp := initResponseConfig(responsePath, responseName)
if err := vResp.ReadInConfig(); err != nil {
log.Fatalln("failed to read response config:", err)
}
vResp.WatchConfig() // enable live reload config on changes
handler := api.NewMainHandler(vResp)
e := echo.New()
e.Use(middleware.Logger())
e.Any("/*", handler.Entry)
host := vApp.GetString("host")
port := vApp.GetInt("port")
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", host, port)))
}
// initAppConfig read app config and set default value as necessary.
func initAppConfig() *viper.Viper {
vApp := viper.New()
vApp.SetConfigName("app")
vApp.SetConfigType("yaml")
vApp.AddConfigPath(".")
vApp.SetDefault("host", "127.0.0.1")
vApp.SetDefault("port", 9595)
vApp.SetDefault("response", ".")
return vApp
}
// initResponseConfig read response data set config.
func initResponseConfig(dir, name string) *viper.Viper {
if name == "" {
name = "response" // default config name is response
}
if dir == "" {
dir = "." // default config path is current directory
}
vResp := viper.New()
vResp.SetConfigName(name)
vResp.SetConfigType("yaml")
vResp.AddConfigPath(dir)
return vResp
}