forked from revel/revel
/
revel.go
254 lines (210 loc) · 6.68 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
package rev
import (
"github.com/robfig/goconfig/config"
"go/build"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
)
const (
REVEL_IMPORT_PATH = "github.com/robfig/revel"
)
var (
// App details
AppName string // e.g. "sample"
BasePath string // e.g. "/Users/robfig/gocode/src/corp/sample"
AppPath string // e.g. "/Users/robfig/gocode/src/corp/sample/app"
ViewsPath string // e.g. "/Users/robfig/gocode/src/corp/sample/app/views"
ImportPath string // e.g. "corp/sample"
SourcePath string // e.g. "/Users/robfig/gocode/src"
Config *MergedConfig
RunMode string // Application-defined (by default, "dev" or "prod")
// Revel installation details
RevelPath string // e.g. "/Users/robfig/gocode/src/revel"
// Where to look for templates and configuration.
// Ordered by priority. (Earlier paths take precedence over later paths.)
CodePaths []string
ConfPaths []string
TemplatePaths []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"
// All cookies dropped by the framework begin with this prefix.
CookiePrefix string
// Loggers
DEFAULT = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
TRACE = DEFAULT
INFO = DEFAULT
WARN = DEFAULT
ERROR = DEFAULT
// Revel runs every function in this array after init.
InitHooks []func()
// Private
revelInit bool
secretKey []byte
)
func init() {
log.SetFlags(DEFAULT.Flags())
}
// 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 = strings.TrimRight(srcPath, "/")
RunMode = mode
if SourcePath == "" {
SourcePath = findSrcPath(importPath)
}
RevelPath = path.Join(SourcePath, filepath.FromSlash(REVEL_IMPORT_PATH))
BasePath = path.Join(SourcePath, filepath.FromSlash(importPath))
AppPath = path.Join(BasePath, "app")
ViewsPath = path.Join(AppPath, "views")
CodePaths = []string{AppPath}
ConfPaths = []string{
path.Join(BasePath, "conf"),
path.Join(RevelPath, "conf"),
}
TemplatePaths = []string{
ViewsPath,
path.Join(RevelPath, "templates"),
}
// Load app.conf
var err error
Config, err = LoadConfig("app.conf")
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.DEFAULT_SECTION
}
if !Config.HasSection(mode) {
log.Fatalln("app.conf: No mode found:", mode)
}
Config.SetSection(mode)
// Configure properties from app.conf
HttpPort = Config.IntDefault("http.port", 9000)
HttpAddr = Config.StringDefault("http.addr", "")
AppName = Config.StringDefault("app.name", "(not set)")
CookiePrefix = Config.StringDefault("cookie.prefix", "REVEL")
secretStr := Config.StringDefault("app.secret", "")
if secretStr == "" {
log.Fatalln("No app.secret provided.")
}
secretKey = []byte(secretStr)
// Configure logging.
TRACE = getLogger("trace")
INFO = getLogger("info")
WARN = getLogger("warn")
ERROR = getLogger("error")
loadModules()
for _, hook := range InitHooks {
hook()
}
revelInit = true
}
// 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")
switch output {
case "stdout":
logger = newLogger(os.Stdout)
case "stderr":
logger = newLogger(os.Stderr)
default:
if output == "off" {
output = os.DevNull
}
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, DEFAULT.Prefix(), DEFAULT.Flags())
}
// findSrcPath uses the "go/build" package to find the source root.
func findSrcPath(importPath string) string {
appPkg, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
log.Fatalln("Failed to import", importPath, "with error:", err)
}
revelPkg, err := build.Import(REVEL_IMPORT_PATH, "", build.FindOnly)
if err != nil {
log.Fatalln("Failed to find Revel with error:", err)
}
// Find the source path from each, and ensure they are equal.
if revelPkg.SrcRoot != appPkg.SrcRoot {
log.Fatalln("Revel must be installed in the same GOPATH as your app."+
"\nRevel source root:", revelPkg.SrcRoot,
"\nApp source root:", appPkg.SrcRoot)
}
return appPkg.SrcRoot
}
type Module struct {
Name, ImportPath, Path string
}
func loadModules() {
for _, key := range Config.Options("module.") {
moduleImportPath := Config.StringDefault(key, "")
if moduleImportPath == "" {
continue
}
modPkg, err := build.Import(moduleImportPath, "", build.FindOnly)
if err != nil {
log.Fatalln("Failed to load module. Import of", moduleImportPath, "failed:", err)
}
addModule(key[len("module."):], moduleImportPath, modPkg.Dir)
}
}
func addModule(name, importPath, modulePath string) {
Modules = append(Modules, Module{Name: name, ImportPath: importPath, Path: modulePath})
if codePath := path.Join(modulePath, "app"); DirExists(codePath) {
CodePaths = append(CodePaths, codePath)
}
if viewsPath := path.Join(modulePath, "app", "views"); DirExists(viewsPath) {
TemplatePaths = append(TemplatePaths, viewsPath)
}
INFO.Print("Loaded module ", path.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 == "github.com/robfig/revel/modules/testrunner" {
CodePaths = append(CodePaths, path.Join(BasePath, "tests"))
}
}
func CheckInit() {
if !revelInit {
panic("Revel has not been initialized!")
}
}