summaryrefslogtreecommitdiff
path: root/context.go
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2023-11-01 17:57:48 +0100
committerTom Barrett <tom@tombarrett.xyz>2023-11-01 18:11:33 +0100
commit240c3d1338415e5d82ef7ca0e52c4284be6441bd (patch)
tree4b0ee5d208c2cdffa78d65f1b0abe0ec85f15652 /context.go
parent73e78ab226f21e6c6c68961af88c4ab9c746f4f4 (diff)
parent0e204b730aa2b1fa0835336b1117eff8c420f713 (diff)
vbump to v2.7.5HEADcaddy-cgi
Diffstat (limited to 'context.go')
-rw-r--r--context.go34
1 files changed, 25 insertions, 9 deletions
diff --git a/context.go b/context.go
index 330bfa6..85978d4 100644
--- a/context.go
+++ b/context.go
@@ -326,7 +326,7 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
// fill in its config only if there is a config to fill in
if len(rawMsg) > 0 {
- err := strictUnmarshalJSON(rawMsg, &val)
+ err := StrictUnmarshalJSON(rawMsg, &val)
if err != nil {
return nil, fmt.Errorf("decoding module config: %s: %v", modInfo, err)
}
@@ -410,6 +410,11 @@ func (ctx Context) loadModuleInline(moduleNameKey, moduleScope string, raw json.
// called during the Provision/Validate phase to reference a
// module's own host app (since the parent app module is still
// in the process of being provisioned, it is not yet ready).
+//
+// We return any type instead of the App type because it is NOT
+// intended for the caller of this method to be the one to start
+// or stop App modules. The caller is expected to assert to the
+// concrete type.
func (ctx Context) App(name string) (any, error) {
if app, ok := ctx.cfg.apps[name]; ok {
return app, nil
@@ -426,15 +431,23 @@ func (ctx Context) App(name string) (any, error) {
return modVal, nil
}
-// AppIsConfigured returns whether an app named name has been
-// configured. Can be called before calling App() to avoid
-// instantiating an empty app when that's not desirable.
-func (ctx Context) AppIsConfigured(name string) bool {
- if _, ok := ctx.cfg.apps[name]; ok {
- return true
+// AppIfConfigured returns an app by its name if it has been
+// configured. Can be called instead of App() to avoid
+// instantiating an empty app when that's not desirable. If
+// the app has not been loaded, nil is returned.
+//
+// We return any type instead of the App type because it is not
+// intended for the caller of this method to be the one to start
+// or stop App modules. The caller is expected to assert to the
+// concrete type.
+func (ctx Context) AppIfConfigured(name string) any {
+ if ctx.cfg == nil {
+ // this can happen if the currently-active context
+ // is being accessed, but no config has successfully
+ // been loaded yet
+ return nil
}
- appRaw := ctx.cfg.AppsRaw[name]
- return appRaw != nil
+ return ctx.cfg.apps[name]
}
// Storage returns the configured Caddy storage implementation.
@@ -475,6 +488,9 @@ func (ctx Context) Logger(module ...Module) *zap.Logger {
if len(module) > 0 {
mod = module[0]
}
+ if mod == nil {
+ return Log()
+ }
return ctx.cfg.Logging.Logger(mod)
}