Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 05:30 UTC
context: Rename func to `AppIfConfigured` (#5397)
Francis Lavoie committed
Feb 27, 2023 at 13:58 UTC
f6bab8ba85b231ea0930282e684c0040001059e6
2 files changed
+17
-18
context.go
+12
-7
@@ -426,15 +426,20 @@ func (ctx Context) App(name string) (any, error) {
426
return modVal, nil
427
}
428
429
-// AppIsConfigured returns whether an app named name has been
430
-// configured. Can be called before calling App() to avoid
429
+// AppIfConfigured returns an app by its name if it has been
430
+// configured. Can be called instead of App() to avoid
431
// instantiating an empty app when that's not desirable.
432
-func (ctx Context) AppIsConfigured(name string) bool {
433
- if _, ok := ctx.cfg.apps[name]; ok {
434
- return true
432
+func (ctx Context) AppIfConfigured(name string) (any, error) {
433
+ app, ok := ctx.cfg.apps[name]
434
+ if !ok || app == nil {
435
+ return nil, nil
436
}
436
- appRaw := ctx.cfg.AppsRaw[name]
437
- return appRaw != nil
437
+
438
+ appModule, err := ctx.App(name)
439
+ if err != nil {
440
+ return nil, err
441
+ }
442
+ return appModule, nil
443
}
444
445
// Storage returns the configured Caddy storage implementation.
modules/caddypki/adminapi.go
+5
-11
@@ -49,20 +49,14 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
49
a.ctx = ctx
50
a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)
51
52
- // First check if the PKI app was configured, because
53
- // a.ctx.App() has the side effect of instantiating
54
- // and provisioning an app even if it wasn't configured.
55
- pkiAppConfigured := a.ctx.AppIsConfigured("pki")
56
- if !pkiAppConfigured {
57
- return nil
58
- }
59
-
60
- // Load the PKI app, so we can query it for information.
61
- appModule, err := a.ctx.App("pki")
52
+ // Avoid initializing PKI if it wasn't configured
53
+ pkiApp, err := a.ctx.AppIfConfigured("pki")
54
if err != nil {
55
return err
56
}
65
- a.pkiApp = appModule.(*PKI)
57
+ if pkiApp != nil {
58
+ a.pkiApp = pkiApp.(*PKI)
59
+ }
60
61
return nil
62
}