-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgogo.go
148 lines (116 loc) · 3.6 KB
/
gogo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gogo
import (
"log"
"net/http"
"os"
"path"
"github.com/dolab/gogo/internal/params"
)
var (
// FindModeConfigFile returns config file for specified run mode.
// You could custom your own resolver by overwriting it.
FindModeConfigFile = func(mode, cfgfile string) string {
// adjust cfgfile
cfgfile = path.Clean(cfgfile)
if len(cfgfile) == 0 || cfgfile == "." || cfgfile == ".." {
return GogoSchema
}
// is cfgfile exist?
finfo, ferr := os.Stat(cfgfile)
if ferr != nil {
return GogoSchema
}
// is cfgfile a regular file?
if finfo.Mode()&os.ModeType == 0 {
return cfgfile
}
filename := "application.yml"
switch RunMode(mode) {
case Development:
// try application.development.yml
filename = "application.development.yml"
case Test:
// try application.test.yml
filename = "application.test.yml"
case Production:
// skip
}
filepath := path.Join(cfgfile, "config", filename)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
filepath = path.Join(cfgfile, "config", "application.yml")
}
return filepath
}
// FindInterceptorConfigFile returns config file for middlewares.
FindInterceptorConfigFile = func(mode, cfgfile string) string {
// adjust cfgfile
cfgfile = path.Clean(cfgfile)
if len(cfgfile) == 0 || cfgfile == "." || cfgfile == ".." {
return GogoSchema
}
// is cfgfile exist?
finfo, ferr := os.Stat(cfgfile)
if ferr != nil {
return GogoSchema
}
// is cfgfile a regular file?
if finfo.Mode()&os.ModeType == 0 {
return cfgfile
}
// resolve cfgfile with run mode
filename := "interceptors.yml"
switch RunMode(mode) {
case Development:
filename = "interceptors.development.yml"
case Test:
filename = "interceptors.test.yml"
case Production:
// skip
}
filepath := path.Join(cfgfile, "config", filename)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
filepath = path.Join(cfgfile, "config", "interceptors.yml")
}
return filepath
}
)
// New creates application server with config resolved
// from file <cfgPath>/config/application[.<runMode>].yml.
//
// NOTE: You can custom resolver by overwriting FindModeConfigFile.
func New(runMode, cfgPath string) *AppServer {
// resolve config from application.yml
config, err := NewAppConfig(FindModeConfigFile(runMode, cfgPath))
if err != nil {
log.Fatalf("[GOGO] NewAppConfig(%s): %v", FindModeConfigFile(runMode, cfgPath), err)
}
return NewWithConfiger(config)
}
// NewDefaults creates application server with defaults.
func NewDefaults() *AppServer {
return New("development", "")
}
// NewWithConfiger creates application server with custom Configer and
// default Logger, see Configer for implements a new config provider.
func NewWithConfiger(config Configer) *AppServer {
// init default logger
logger := NewAppLogger(config.Section().Logger.Output, config.RunMode().String())
return NewWithLogger(config, logger)
}
// NewWithLogger creates application server with custom Configer and Logger
func NewWithLogger(config Configer, logger Logger) *AppServer {
// overwrite logger level and colorful
logger.SetLevelByName(config.Section().Logger.LevelName)
logger.SetColor(!config.RunMode().IsProduction())
logger.Printf("Initialized %s in %s mode", config.RunName(), config.RunMode())
// try load config of middlewares
// NOTE: ignore returned error is ok!
if err := config.LoadInterceptors(); err != nil {
logger.Errorf("config.LoadInterceptors(): %v", err)
}
return NewAppServer(config, logger)
}
// NewParams returns *params.Params related with http request
func NewParams(r *http.Request) *params.Params {
return params.New(r)
}