Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5c52331

Browse filesBrowse files
authored
Merge pull request revel#1230 from notzippy/pre-release
Cleanup, Added RegisterModuleInit
2 parents a572ac3 + c370e23 commit 5c52331
Copy full SHA for 5c52331

File tree

5 files changed

+62
-19
lines changed
Filter options

5 files changed

+62
-19
lines changed

‎.codebeatsettings

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"GOLANG": {
3+
"TOO_MANY_IVARS": [15, 18, 20, 25],
4+
"TOO_MANY_FUNCTIONS": [20, 30, 40, 50],
5+
"TOTAL_COMPLEXITY": [150, 250, 400, 500],
6+
"LOC": [50, 75, 90, 120]
7+
}
8+
}

‎controller.go

Copy file name to clipboardExpand all lines: controller.go
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type Controller struct {
2828
AppController interface{} // The controller that was instantiated. embeds revel.Controller
2929
Action string // The fully qualified action name, e.g. "App.Index"
3030
ClientIP string // holds IP address of request came from
31-
module *Module // The module for the parent controller (if available)
3231

3332
Request *Request
3433
Response *Response

‎module.go

Copy file name to clipboardExpand all lines: module.go
+47-13Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
package revel
22

33
import (
4+
"fmt"
5+
"github.com/go-stack/stack"
6+
"github.com/revel/revel/logger"
47
"go/build"
58
"path/filepath"
69
"sort"
710
"strings"
8-
"github.com/revel/revel/logger"
911
)
1012

1113
// Module specific functions
1214
type Module struct {
1315
Name, ImportPath, Path string
1416
ControllerTypeList []*ControllerType
15-
Log logger.MultiLogger
17+
Log logger.MultiLogger
18+
initializedModules map[string]ModuleCallbackInterface
1619
}
1720

21+
// Modules can be called back after they are loaded in revel by using this interface.
22+
type ModuleCallbackInterface func(*Module)
23+
1824
// The namespace separator constant
1925
const namespaceSeperator = `\` // (note cannot be . or : as this is already used for routes)
2026

2127
var (
22-
Modules []*Module // The list of modules in use
23-
anyModule = &Module{} // Wildcard search for controllers for a module (for backward compatible lookups)
24-
appModule = &Module{Name: "App"} // The app module
25-
moduleLogger = RevelLog.New("section", "module")
28+
Modules []*Module // The list of modules in use
29+
anyModule = &Module{} // Wildcard search for controllers for a module (for backward compatible lookups)
30+
appModule = &Module{Name: "App", initializedModules: map[string]ModuleCallbackInterface{}, Log: AppLog} // The app module
31+
moduleLog = RevelLog.New("section", "module")
2632
)
2733

34+
// Called by a module init() function, caller will receive the *Module object created for that module
35+
// This would be useful for assigning a logger for logging information in the module (since the module context would be correct)
36+
func RegisterModuleInit(callback ModuleCallbackInterface) {
37+
// Store the module that called this so we can do a callback when the app is initialized
38+
// The format %+k is from go-stack/Call.Format and returns the package path
39+
key := fmt.Sprintf("%+k", stack.Caller(1))
40+
appModule.initializedModules[key] = callback
41+
if Initialized {
42+
RevelLog.Error("Application already initialized, initializing using app module", "key", key)
43+
callback(appModule)
44+
}
45+
46+
}
2847
func init() {
2948
AddInitEventHandler(func(typeOf int, value interface{}) (responseOf int) {
3049
if typeOf == REVEL_BEFORE_MODULES_LOADED {
@@ -117,7 +136,7 @@ func loadModules() {
117136
// Reorder module order by key name, a poor mans sort but at least it is consistent
118137
sort.Strings(keys)
119138
for _, key := range keys {
120-
moduleLogger.Debug("Sorted keys", "keys", key)
139+
moduleLog.Debug("Sorted keys", "keys", key)
121140

122141
}
123142
for _, key := range keys {
@@ -128,7 +147,7 @@ func loadModules() {
128147

129148
modulePath, err := ResolveImportPath(moduleImportPath)
130149
if err != nil {
131-
moduleLogger.Error("Failed to load module. Import of path failed", "modulePath", moduleImportPath, "error", err)
150+
moduleLog.Error("Failed to load module. Import of path failed", "modulePath", moduleImportPath, "error", err)
132151
}
133152
// Drop anything between module.???.<name of module>
134153
subKey := key[len("module."):]
@@ -137,32 +156,47 @@ func loadModules() {
137156
}
138157
addModule(subKey, moduleImportPath, modulePath)
139158
}
159+
160+
// Modules loaded, now show module path
161+
for key, callback := range appModule.initializedModules {
162+
found := false
163+
for _, m := range Modules {
164+
if strings.HasPrefix(key, m.ImportPath) {
165+
moduleLog.Debug("Module called callback", "moduleKey", m.ImportPath, "callbackKey", key)
166+
callback(m)
167+
}
168+
}
169+
if !found {
170+
RevelLog.Error("Callback for non registered module initializing with application module","modulePath",key)
171+
callback(appModule)
172+
}
173+
}
140174
}
141175

142176
//
143177
func addModule(name, importPath, modulePath string) {
144178
if _, found := ModuleByName(name); found {
145-
moduleLogger.Panic("Attempt to import duplicate module %s path %s aborting startup", "name", name, "path", modulePath)
179+
moduleLog.Panic("Attempt to import duplicate module %s path %s aborting startup", "name", name, "path", modulePath)
146180
}
147-
Modules = append(Modules, &Module{Name: name, ImportPath: importPath, Path: modulePath, Log:AppLog.New("module", name)})
181+
Modules = append(Modules, &Module{Name: name, ImportPath: importPath, Path: modulePath, Log: RootLog.New("module", name)})
148182
if codePath := filepath.Join(modulePath, "app"); DirExists(codePath) {
149183
CodePaths = append(CodePaths, codePath)
150184
if viewsPath := filepath.Join(modulePath, "app", "views"); DirExists(viewsPath) {
151185
TemplatePaths = append(TemplatePaths, viewsPath)
152186
}
153187
}
154188

155-
moduleLogger.Debug("Loaded module ", "module", filepath.Base(modulePath))
189+
moduleLog.Debug("Loaded module ", "module", filepath.Base(modulePath))
156190

157191
// Hack: There is presently no way for the testrunner module to add the
158192
// "test" subdirectory to the CodePaths. So this does it instead.
159193
if importPath == Config.StringDefault("module.testrunner", "github.com/revel/modules/testrunner") {
160194
joinedPath := filepath.Join(BasePath, "tests")
161-
moduleLogger.Debug("Found testrunner module, adding `tests` path ", "path", joinedPath)
195+
moduleLog.Debug("Found testrunner module, adding `tests` path ", "path", joinedPath)
162196
CodePaths = append(CodePaths, joinedPath)
163197
}
164198
if testsPath := filepath.Join(modulePath, "tests"); DirExists(testsPath) {
165-
moduleLogger.Debug("Found tests path ", "path", testsPath)
199+
moduleLog.Debug("Found tests path ", "path", testsPath)
166200
CodePaths = append(CodePaths, testsPath)
167201
}
168202
}

‎router.go

Copy file name to clipboardExpand all lines: router.go
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,12 @@ func RouterFilter(c *Controller, fc []Filter) {
749749

750750
// Add the route and fixed params to the Request Params.
751751
c.Params.Route = route.Params
752+
// Assign logger if from module
753+
if c.Type.ModuleSource!=nil && c.Type.ModuleSource!=appModule {
754+
c.Log = c.Type.ModuleSource.Log.New("ip", c.ClientIP,
755+
"path", c.Request.URL.Path, "method", c.Request.Method)
756+
}
757+
752758

753759
// Add the fixed parameters mapped by name.
754760
// TODO: Pre-calculate this mapping.

‎server-engine.go

Copy file name to clipboardExpand all lines: server-engine.go
+1-5Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ func handleInternal(ctx ServerContext) {
139139
}()
140140

141141
c.ClientIP = clientIP
142-
controllerLog := AppLog
143-
if c.module!=nil {
144-
controllerLog = c.module.Log
145-
}
146-
c.Log = controllerLog.New("ip", clientIP,
142+
c.Log = AppLog.New("ip", clientIP,
147143
"path", req.GetPath(), "method", req.Method)
148144
// Call the first filter, this will process the request
149145
Filters[0](c, Filters[1:])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.