-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
197 lines (165 loc) · 5.46 KB
/
options.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cucumber
import (
"html/template"
"github.com/AjdinHalac/cucumber/log"
"github.com/AjdinHalac/cucumber/render/view"
"github.com/AjdinHalac/cucumber/sessions"
"google.golang.org/grpc"
)
const (
defaultEnv = "development"
defaultName = "cucumberApp"
defaultVersion = "v0.0.0"
defaultLogLevel = "debug"
defaultRedirectTrailingSlash = true
defaultRedirectFixedPath = false
defaultHandleMethodNotAllowed = false
defaultMaxMultipartMemory = 32 << 20 // 32 MB
default404Body = "404 page not found"
default405Body = "405 method not allowed"
defaultUseSession = false
defaultSessionName = "_cucumber_app_session"
defaultUseTranslator = false
defaultTranslatorLocalesRoot = "locales"
defaultTranslatorDefaultLang = "en-US"
defaultUseRequestLogger = true
defaultUsePanicRecovery = true
defaultUseViewEngine = false
defaultViewsRoot = "views"
defaultViewsExt = ".tpl"
defaultViewsMasterLayout = "layouts/master"
defaultViewsPartialsRoot = "partials"
defaultViewsDisableCache = false
defaultServeStatic = false
defaultStaticPath = "/static"
defaultStaticDir = "./public"
// ControllerPackage holds package name in which controllers can be registered
defaultControllerPackage = "controllers"
// ControllerIndex holds controller Index name
defaultControllerIndex = "Index"
// ControllerSuffix holds controller naming convention
defaultControllerSuffix = "Controller"
)
// Options holds cucumber configuration options
type Options struct {
Env string
Name string
HTTPAddr string
GRPCAddr string
Version string
LogLevel string
RedirectTrailingSlash bool
RedirectFixedPath bool
HandleMethodNotAllowed bool
MaxMultipartMemory int64
Body404 string
Body500 string
UseSession bool
SessionName string
SessionSecret string
UseTranslator bool
TranslatorLocalesRoot string
TranslatorDefaultLang string
UseRequestLogger bool
UsePanicRecovery bool
UseViewEngine bool
ViewsRoot string
ViewsExt string
ViewsMasterLayout string
ViewsPartialsRoot string
ViewsDisableCache bool
ServeStatic bool
StaticPath string
StaticDir string
Logger log.Logger
SessionStore sessions.Store
ViewEngine view.Engine
Translator *Translator
UnaryInterceptors []grpc.UnaryServerInterceptor
// ControllerPackage holds package name in which controllers can be registered
ControllerPackage string
// ControllerIndex holds controller Index name
ControllerIndex string
// ControllerSuffix holds controller naming convention
ControllerSuffix string
RequestLoggerIgnore []string
UnaryRequestLoggerIgnore []string
AppConfig interface{}
}
// NewOptions returns a new Options instance with default configuration
func NewOptions() Options {
opts := Options{
Env: defaultEnv,
Name: defaultName,
Version: defaultVersion,
LogLevel: defaultLogLevel,
RedirectTrailingSlash: defaultRedirectTrailingSlash,
RedirectFixedPath: defaultRedirectFixedPath,
HandleMethodNotAllowed: defaultHandleMethodNotAllowed,
MaxMultipartMemory: defaultMaxMultipartMemory,
Body404: default404Body,
Body500: default405Body,
UseSession: defaultUseSession,
SessionName: defaultSessionName,
UseTranslator: defaultUseTranslator,
TranslatorLocalesRoot: defaultTranslatorLocalesRoot,
TranslatorDefaultLang: defaultTranslatorDefaultLang,
UseRequestLogger: defaultUseRequestLogger,
UsePanicRecovery: defaultUsePanicRecovery,
UseViewEngine: defaultUseViewEngine,
ViewsRoot: defaultViewsRoot,
ViewsExt: defaultViewsExt,
ViewsMasterLayout: defaultViewsMasterLayout,
ViewsPartialsRoot: defaultViewsPartialsRoot,
ViewsDisableCache: defaultViewsDisableCache,
ServeStatic: defaultServeStatic,
StaticPath: defaultStaticPath,
StaticDir: defaultStaticDir,
ControllerPackage: defaultControllerPackage,
ControllerIndex: defaultControllerIndex,
ControllerSuffix: defaultControllerSuffix,
}
return opts
}
func optionsWithDefault(opts Options) Options {
//configure logger
if opts.Logger == nil {
opts.Logger = log.New(log.Configuration{
EnableConsole: true,
ConsoleJSONFormat: true,
ConsoleLevel: opts.LogLevel,
})
}
//configure session store
if opts.UseSession && opts.SessionStore == nil {
if opts.SessionSecret == "" {
opts.Logger.Warn("SessionSecret configuration key is not set. Your sessions are not safe!")
}
opts.SessionStore = sessions.NewCookieStore([]byte(opts.SessionSecret))
}
//configure ViewEngine
if opts.UseViewEngine && opts.ViewEngine == nil {
partials, err := loadPartials(opts.ViewsRoot, opts.ViewsPartialsRoot, opts.ViewsExt)
if err != nil {
opts.Logger.Fatal(err.Error())
}
opts.ViewEngine = view.NewHTMLEngine(view.Config{
Root: opts.ViewsRoot,
Ext: opts.ViewsExt,
Master: opts.ViewsMasterLayout,
Partials: partials,
Funcs: make(template.FuncMap),
DisableCache: opts.ViewsDisableCache,
Delims: view.Delims{Left: "{{", Right: "}}"},
})
}
// configure translator
if opts.UseTranslator && opts.Translator == nil {
t, err := NewTranslator(opts.TranslatorLocalesRoot, opts.TranslatorDefaultLang)
if err != nil {
opts.Logger.Fatal(err.Error())
}
opts.Translator = t
}
return opts
}