Skip to content

Navigation Menu

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 c370e23

Browse filesBrowse files
committed
Added RegisterModuleInit so modules can receive their own instance of the *Module object
1 parent 02f2390 commit c370e23
Copy full SHA for c370e23

File tree

2 files changed

+55
-13
lines changed
Filter options

2 files changed

+55
-13
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+
}

‎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:RootLog.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
}

0 commit comments

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