forked from roblillack/mars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mars.go
323 lines (268 loc) · 9.42 KB
/
mars.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package mars
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"time"
"github.com/agtorre/gocolorize"
"github.com/robfig/config"
"github.com/roblillack/mars/internal/watcher"
)
const (
MarsImportPath = "github.com/roblillack/mars"
defaultLoggerFlags = log.Ldate | log.Ltime | log.Lshortfile
)
type marsLogs struct {
c gocolorize.Colorize
w io.Writer
}
func (r *marsLogs) Write(p []byte) (n int, err error) {
return r.w.Write([]byte(r.c.Paint(string(p))))
}
var (
// ConfigFile specifies the path of the main configuration file relative to BasePath, e.g. "conf/app.conf"
ConfigFile = path.Join("conf", "app.conf")
// MimeTypesFile specifies the path of the optional MIME type configuration file relative to BasePath, e.g. "conf/mime-types.conf"
MimeTypesFile = path.Join("conf", "mime-types.conf")
// RoutesFile specified the path of the route configuration file relative to BasePath, e.g. "conf/routes"
RoutesFile = path.Join("conf", "routes")
// ViewPath specifies the name of directory where all the templates are located relative to BasePath, e.g. "views"
ViewsPath = "views"
Config = NewEmptyConfig()
MimeConfig = NewEmptyConfig()
// App details
AppName = "(not set)" // e.g. "sample"
AppRoot = "" // e.g. "/app1"
BasePath = "." // e.g. "/Users/robfig/gocode/src/corp/sample"
RunMode = "prod"
DevMode = false
// Server config.
//
// Alert: This is how the app is configured, which may be different from
// the current process reality. For example, if the app is configured for
// port 9000, HttpPort will always be 9000, even though in dev mode it is
// run on a random port and proxied.
HttpAddr = ":9000" // e.g. "", "127.0.0.1"
HttpSsl = false // e.g. true if using ssl
HttpSslCert = "" // e.g. "/path/to/cert.pem"
HttpSslKey = "" // e.g. "/path/to/key.pem"
DualStackHTTP = false
SSLAddr = ":https"
SelfSignedCert = false
SelfSignedOrganization = "ACME Inc."
SelfSignedDomains = "127.0.0.1"
// All cookies dropped by the framework begin with this prefix.
CookiePrefix = "MARS"
// Cookie domain
CookieDomain = ""
// Cookie flags
CookieHttpOnly = false
CookieSecure = false
// DisableCSRF disables CSRF checking altogether. See CSRFFilter for more information.
DisableCSRF = false
//Logger colors
colors = map[string]gocolorize.Colorize{
"trace": gocolorize.NewColor("magenta"),
"info": gocolorize.NewColor("green"),
"warn": gocolorize.NewColor("yellow"),
"error": gocolorize.NewColor("red"),
}
// Loggers
DisabledLogger = log.New(ioutil.Discard, "", 0)
TRACE = DisabledLogger
INFO = log.New(&marsLogs{c: colors["info"], w: os.Stderr}, "INFO ", defaultLoggerFlags)
WARN = log.New(&marsLogs{c: colors["warn"], w: os.Stderr}, "WARN ", defaultLoggerFlags)
ERROR = log.New(&marsLogs{c: colors["error"], w: os.Stderr}, "ERROR ", defaultLoggerFlags)
MaxAge = time.Hour * 24 // MaxAge specifies the time browsers shall cache static content served using Static.Serve
// Private
secretKey []byte // Key used to sign cookies. An empty key disables signing.
setupDone bool
)
func SetAppSecret(secret string) {
secretKey = []byte(secret)
}
func init() {
log.SetFlags(defaultLoggerFlags)
}
// InitDefaults initializes Mars based on runtime-loading of config files.
//
// Params:
// mode - the run mode, which determines which app.conf settings are used.
// basePath - the path to the configuration, messages, and view directories
func InitDefaults(mode, basePath string) {
RunMode = mode
if runtime.GOOS == "windows" {
gocolorize.SetPlain(true)
}
BasePath = filepath.FromSlash(basePath)
var cfgPath string
if filepath.IsAbs(ConfigFile) {
cfgPath = ConfigFile
} else {
cfgPath = filepath.Join(BasePath, ConfigFile)
}
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
var err error
Config, err = LoadConfig(cfgPath)
if err != nil || Config == nil {
log.Fatalln("Failed to load app.conf:", err)
}
}
MimeConfig, _ = LoadConfig(path.Join(BasePath, MimeTypesFile))
// Ensure that the selected runmode appears in app.conf.
// If empty string is passed as the mode, treat it as "DEFAULT"
if mode == "" {
mode = config.DEFAULT_SECTION
}
if Config.HasSection(mode) {
Config.SetSection(mode)
}
// Configure properties from app.conf
DevMode = Config.BoolDefault("mode.dev", DevMode)
HttpAddr = Config.StringDefault("http.addr", HttpAddr)
HttpSsl = Config.BoolDefault("https.enabled", Config.BoolDefault("http.ssl", HttpSsl))
HttpSslCert = Config.StringDefault("https.certfile", Config.StringDefault("http.sslcert", HttpSslCert))
HttpSslKey = Config.StringDefault("https.keyfile", Config.StringDefault("http.sslkey", HttpSslKey))
DualStackHTTP = Config.BoolDefault("http.dualstack", DualStackHTTP)
SSLAddr = Config.StringDefault("https.addr", "")
SelfSignedCert = Config.BoolDefault("https.selfsign", SelfSignedCert)
SelfSignedOrganization = Config.StringDefault("https.organization", SelfSignedOrganization)
SelfSignedDomains = Config.StringDefault("https.domains", SelfSignedDomains)
if (DualStackHTTP || HttpSsl) && !SelfSignedCert {
if HttpSslCert == "" {
log.Fatalln("No https.certfile provided and https.selfsign not true.")
}
if HttpSslKey == "" {
log.Fatalln("No https.keyfile provided and https.selfsign not true.")
}
}
tryAddingSSLPort := false
// Support legacy way of specifying HTTPS addr
if SSLAddr == "" {
if HttpSsl && !DualStackHTTP {
SSLAddr = HttpAddr
tryAddingSSLPort = true
} else {
SSLAddr = ":https"
}
}
// Support legacy way of specifying port number as config setting http.port
if p := Config.IntDefault("http.port", -1); p != -1 {
HttpAddr = fmt.Sprintf("%s:%d", HttpAddr, p)
if tryAddingSSLPort {
SSLAddr = fmt.Sprintf("%s:%d", SSLAddr, p)
}
}
AppName = Config.StringDefault("app.name", AppName)
AppRoot = Config.StringDefault("app.root", AppRoot)
CookiePrefix = Config.StringDefault("cookie.prefix", CookiePrefix)
CookieDomain = Config.StringDefault("cookie.domain", CookieDomain)
CookieHttpOnly = Config.BoolDefault("cookie.httponly", CookieHttpOnly)
CookieSecure = Config.BoolDefault("cookie.secure", CookieSecure)
if s := Config.StringDefault("app.secret", ""); s != "" {
SetAppSecret(s)
}
// Configure logging
if !Config.BoolDefault("log.colorize", true) {
gocolorize.SetPlain(true)
}
TRACE = getLogger("trace", TRACE)
INFO = getLogger("info", INFO)
WARN = getLogger("warn", WARN)
ERROR = getLogger("error", ERROR)
setup()
}
func setup() {
// The "watch" config variable can turn on and off all watching.
// (As a convenient way to control it all together.)
if Config.BoolDefault("watch", DevMode) {
mainWatcher = watcher.New()
Filters = append([]Filter{WatchFilter}, Filters...)
}
if MainTemplateLoader == nil {
SetupViews()
}
if MainRouter == nil {
SetupRouter()
}
runStartupHooks()
setupDone = true
}
// initializeFallbacks will setup all configuration options that are needed for serving results but might not have
// been initialized correctly by the consumer of the toolkit
func initializeFallbacks() {
if MainTemplateLoader == nil {
MainTemplateLoader = emptyTemplateLoader()
}
}
// SetupViews will create a template loader for all the templates provided in ViewsPath
func SetupViews() {
MainTemplateLoader = NewTemplateLoader([]string{path.Join(BasePath, ViewsPath)})
if err := MainTemplateLoader.Refresh(); err != nil {
ERROR.Fatalln(err)
}
// If desired (or by default), create a watcher for templates and routes.
// The watcher calls Refresh() on things on the first request.
if mainWatcher != nil && Config.BoolDefault("watch.templates", true) {
if err := mainWatcher.Listen(MainTemplateLoader, MainTemplateLoader.paths...); err != nil {
ERROR.Fatalln(err)
}
}
}
// SetupRouter will create the router of the application based on the information
// provided in RoutesFile and the controllers and actions which have been registered
// using RegisterController.
func SetupRouter() {
MainRouter = NewRouter(filepath.Join(BasePath, RoutesFile))
if err := MainRouter.Refresh(); err != nil {
ERROR.Fatalln(err)
}
// If desired (or by default), create a watcher for templates and routes.
// The watcher calls Refresh() on things on the first request.
if mainWatcher != nil && Config.BoolDefault("watch.routes", true) {
if err := mainWatcher.Listen(MainRouter, MainRouter.path); err != nil {
ERROR.Fatalln(err)
}
}
}
// Create a logger using log.* directives in app.conf plus the current settings
// on the default logger.
func getLogger(name string, original *log.Logger) *log.Logger {
var logger *log.Logger
// Create a logger with the requested output. (default to stderr)
output := Config.StringDefault("log."+name+".output", "")
switch output {
case "":
return original
case "stdout":
logger = newLogger(&marsLogs{c: colors[name], w: os.Stdout})
case "stderr":
logger = newLogger(&marsLogs{c: colors[name], w: os.Stderr})
case "off":
return DisabledLogger
default:
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file", output, ":", err)
}
logger = newLogger(file)
}
// Set the prefix / flags.
flags, found := Config.Int("log." + name + ".flags")
if found {
logger.SetFlags(flags)
}
prefix, found := Config.String("log." + name + ".prefix")
if found {
logger.SetPrefix(prefix)
}
return logger
}
func newLogger(wr io.Writer) *log.Logger {
return log.New(wr, "", defaultLoggerFlags)
}