forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revel.go
443 lines (373 loc) · 12.8 KB
/
revel.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"go/build"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/agtorre/gocolorize"
"github.com/netassist-ua/revel-config"
"sort"
)
const (
// RevelImportPath Revel framework import path
RevelImportPath = "github.com/netassist-ua/revel"
)
const (
// Called when templates are going to be refreshed (receivers are registered template engines added to the template.engine conf option)
TEMPLATE_REFRESH_REQUESTED = iota
// Called when templates are refreshed (receivers are registered template engines added to the template.engine conf option)
TEMPLATE_REFRESH_COMPLETED
)
type revelLogs struct {
c gocolorize.Colorize
w io.Writer
}
type EventHandler func(typeOf int, value interface{}) (responseOf int)
func (r *revelLogs) Write(p []byte) (n int, err error) {
return r.w.Write([]byte(r.c.Paint(string(p))))
}
// App details
var (
AppName string // e.g. "sample"
AppRoot string // e.g. "/app1"
BasePath string // e.g. "$GOPATH/src/corp/sample"
AppPath string // e.g. "$GOPATH/src/corp/sample/app"
ViewsPath string // e.g. "$GOPATH/src/corp/sample/app/views"
ImportPath string // e.g. "corp/sample"
SourcePath string // e.g. "$GOPATH/src"
Config *config.Context
RunMode string // Application-defined (by default, "dev" or "prod")
DevMode bool // if true, RunMode is a development mode.
// Revel installation details
RevelPath string // e.g. "$GOPATH/src/github.com/netassist-ua/revel"
// Where to look for templates
// Ordered by priority. (Earlier paths take precedence over later paths.)
CodePaths []string
TemplatePaths []string
// ConfPaths where to look for configurations
// Config load order
// 1. framework (revel/conf/*)
// 2. application (conf/*)
// 3. user supplied configs (...) - User configs can override/add any from above
ConfPaths []string
Modules []Module
// 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.
HTTPPort int // e.g. 9000
HTTPAddr string // e.g. "", "127.0.0.1"
HTTPSsl bool // e.g. true if using ssl
HTTPSslCert string // e.g. "/path/to/cert.pem"
HTTPSslKey string // e.g. "/path/to/key.pem"
// All cookies dropped by the framework begin with this prefix.
CookiePrefix string
// Cookie domain
CookieDomain string
// Cookie flags
CookieSecure bool
// Delimiters to use when rendering templates
TemplateDelims string
//Logger colors
colors = map[string]gocolorize.Colorize{
"trace": gocolorize.NewColor("magenta"),
"info": gocolorize.NewColor("white"),
"warn": gocolorize.NewColor("yellow"),
"error": gocolorize.NewColor("red"),
}
errorLog = revelLogs{c: colors["error"], w: os.Stderr}
// Loggers
TRACE = log.New(ioutil.Discard, "TRACE ", log.Ldate|log.Ltime|log.Lshortfile)
INFO = log.New(ioutil.Discard, "INFO ", log.Ldate|log.Ltime|log.Lshortfile)
WARN = log.New(ioutil.Discard, "WARN ", log.Ldate|log.Ltime|log.Lshortfile)
ERROR = log.New(&errorLog, "ERROR ", log.Ldate|log.Ltime|log.Lshortfile)
// Revel request access log, not exposed from package.
// However output settings can be controlled from app.conf
requestLog = log.New(ioutil.Discard, "", 0)
requestLogTimeFormat = "2006/01/02 15:04:05.000"
Initialized bool
// Private
secretKey []byte // Key used to sign cookies. An empty key disables signing.
packaged bool // If true, this is running from a pre-built package.
initEventList = []EventHandler{} // Event handler list for receiving events
)
// Init initializes Revel -- it provides paths for getting around the app.
//
// Params:
// mode - the run mode, which determines which app.conf settings are used.
// importPath - the Go import path of the application.
// srcPath - the path to the source directory, containing Revel and the app.
// If not specified (""), then a functioning Go installation is required.
func Init(mode, importPath, srcPath string) {
// Ignore trailing slashes.
ImportPath = strings.TrimRight(importPath, "/")
SourcePath = srcPath
RunMode = mode
if runtime.GOOS == "windows" {
gocolorize.SetPlain(true)
}
// If the SourcePath is not specified, find it using build.Import.
var revelSourcePath string // may be different from the app source path
if SourcePath == "" {
revelSourcePath, SourcePath = findSrcPaths(importPath)
} else {
// If the SourcePath was specified, assume both Revel and the app are within it.
SourcePath = filepath.Clean(SourcePath)
revelSourcePath = SourcePath
packaged = true
}
RevelPath = filepath.Join(revelSourcePath, filepath.FromSlash(RevelImportPath))
BasePath = filepath.Join(SourcePath, filepath.FromSlash(importPath))
AppPath = filepath.Join(BasePath, "app")
ViewsPath = filepath.Join(AppPath, "views")
CodePaths = []string{AppPath}
if ConfPaths == nil {
ConfPaths = []string{}
}
// Config load order
// 1. framework (revel/conf/*)
// 2. application (conf/*)
// 3. user supplied configs (...) - User configs can override/add any from above
ConfPaths = append(
[]string{
filepath.Join(RevelPath, "conf"),
filepath.Join(BasePath, "conf"),
},
ConfPaths...)
TemplatePaths = []string{
ViewsPath,
filepath.Join(RevelPath, "templates"),
}
// Load app.conf
var err error
Config, err = config.LoadContext("app.conf", ConfPaths)
if err != nil || Config == nil {
log.Fatalln("Failed to load app.conf:", err)
}
// 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.DefaultSection
}
if !Config.HasSection(mode) {
log.Fatalln("app.conf: No mode found:", mode)
}
Config.SetSection(mode)
// Configure properties from app.conf
DevMode = Config.BoolDefault("mode.dev", false)
HTTPPort = Config.IntDefault("http.port", 9000)
HTTPAddr = Config.StringDefault("http.addr", "")
HTTPSsl = Config.BoolDefault("http.ssl", false)
HTTPSslCert = Config.StringDefault("http.sslcert", "")
HTTPSslKey = Config.StringDefault("http.sslkey", "")
if HTTPSsl {
if HTTPSslCert == "" {
log.Fatalln("No http.sslcert provided.")
}
if HTTPSslKey == "" {
log.Fatalln("No http.sslkey provided.")
}
}
AppName = Config.StringDefault("app.name", "(not set)")
AppRoot = Config.StringDefault("app.root", "")
CookiePrefix = Config.StringDefault("cookie.prefix", "REVEL")
CookieDomain = Config.StringDefault("cookie.domain", "")
CookieSecure = Config.BoolDefault("cookie.secure", HTTPSsl)
TemplateDelims = Config.StringDefault("template.delimiters", "")
if secretStr := Config.StringDefault("app.secret", ""); secretStr != "" {
secretKey = []byte(secretStr)
}
// Configure logging
if !Config.BoolDefault("log.colorize", true) {
gocolorize.SetPlain(true)
}
TRACE = getLogger("trace")
INFO = getLogger("info")
WARN = getLogger("warn")
ERROR = getLogger("error")
// Revel request access logger, not exposed from package.
// However output settings can be controlled from app.conf
requestLog = getLogger("request")
loadModules()
Initialized = true
INFO.Printf("Initialized Revel v%s (%s) for %s", Version, BuildDate, MinimumGoVersion)
}
// Fires system events from revel
func fireEvent(key int, value interface{}) (response int) {
for _, handler := range initEventList {
response |= handler(key, value)
}
return
}
// Add event handler to listen for all system events
func AddInitEventHandler(handler EventHandler) {
initEventList = append(initEventList, handler)
return
}
func SetSecretKey(newKey []byte) error {
secretKey = newKey
return nil
}
// Create a logger using log.* directives in app.conf plus the current settings
// on the default logger.
func getLogger(name string) *log.Logger {
var logger *log.Logger
// Create a logger with the requested output. (default to stderr)
output := Config.StringDefault("log."+name+".output", "stderr")
var newlog revelLogs
switch output {
case "stdout":
newlog = revelLogs{c: colors[name], w: os.Stdout}
logger = newLogger(&newlog)
case "stderr":
newlog = revelLogs{c: colors[name], w: os.Stderr}
logger = newLogger(&newlog)
case "off":
return newLogger(ioutil.Discard)
default:
if !filepath.IsAbs(output) {
output = filepath.Join(BasePath, output)
}
logPath := filepath.Dir(output)
if err := createDir(logPath); err != nil {
log.Fatalln(err)
}
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)
}
if strings.EqualFold(name, "request") {
logger.SetFlags(0)
return logger
}
// 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, "", INFO.Flags())
}
// findSrcPaths uses the "go/build" package to find the source root for Revel
// and the app.
func findSrcPaths(importPath string) (revelSourcePath, appSourcePath string) {
var (
gopaths = filepath.SplitList(build.Default.GOPATH)
goroot = build.Default.GOROOT
)
if len(gopaths) == 0 {
ERROR.Fatalln("GOPATH environment variable is not set. ",
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
}
if ContainsString(gopaths, goroot) {
ERROR.Fatalf("GOPATH (%s) must not include your GOROOT (%s). "+
"Please refer to http://golang.org/doc/code.html to configure your Go environment.",
gopaths, goroot)
}
appPkg, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
ERROR.Fatalln("Failed to import", importPath, "with error:", err)
}
revelPkg, err := build.Import(RevelImportPath, "", build.FindOnly)
if err != nil {
ERROR.Fatalln("Failed to find Revel with error:", err)
}
return revelPkg.SrcRoot, appPkg.SrcRoot
}
type Module struct {
Name, ImportPath, Path string
}
func loadModules() {
keys := []string{}
for _, key := range Config.Options("module.") {
keys = append(keys, key)
}
// Reorder module order by key name, a poor mans sort but at least it is consistent
sort.Strings(keys)
for _, key := range keys {
println("Sorted keys", key)
}
for _, key := range keys {
moduleImportPath := Config.StringDefault(key, "")
if moduleImportPath == "" {
continue
}
modulePath, err := ResolveImportPath(moduleImportPath)
if err != nil {
log.Fatalln("Failed to load module. Import of", moduleImportPath, "failed:", err)
}
// Drop anything between module.???.<name of module>
subKey := key[len("module."):]
if index := strings.Index(subKey, "."); index > -1 {
subKey = subKey[index+1:]
}
addModule(subKey, moduleImportPath, modulePath)
}
}
// ResolveImportPath returns the filesystem path for the given import path.
// Returns an error if the import path could not be found.
func ResolveImportPath(importPath string) (string, error) {
if packaged {
return filepath.Join(SourcePath, importPath), nil
}
modPkg, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
return "", err
}
return modPkg.Dir, nil
}
func addModule(name, importPath, modulePath string) {
Modules = append(Modules, Module{Name: name, ImportPath: importPath, Path: modulePath})
if codePath := filepath.Join(modulePath, "app"); DirExists(codePath) {
CodePaths = append(CodePaths, codePath)
if viewsPath := filepath.Join(modulePath, "app", "views"); DirExists(viewsPath) {
TemplatePaths = append(TemplatePaths, viewsPath)
}
}
INFO.Print("Loaded module ", filepath.Base(modulePath))
// Hack: There is presently no way for the testrunner module to add the
// "test" subdirectory to the CodePaths. So this does it instead.
if importPath == Config.StringDefault("module.testrunner", "github.com/netassist-ua/revel-modules/testrunner") {
INFO.Print("Found testrunner module, adding `tests` path ", filepath.Join(BasePath, "tests"))
CodePaths = append(CodePaths, filepath.Join(BasePath, "tests"))
}
if testsPath := filepath.Join(modulePath, "tests"); DirExists(testsPath) {
INFO.Print("Found tests path ", testsPath)
CodePaths = append(CodePaths, testsPath)
}
}
// ModuleByName returns the module of the given name, if loaded.
func ModuleByName(name string) (m Module, found bool) {
for _, module := range Modules {
if module.Name == name {
return module, true
}
}
return Module{}, false
}
// CheckInit method checks `revel.Initialized` if not initialized it panics
func CheckInit() {
if !Initialized {
panic("Revel has not been initialized!")
}
}
func init() {
log.SetFlags(INFO.Flags())
}