Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,20 @@ 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
// 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.
func (ctx Context) AppIsConfigured(name string) bool {
if _, ok := ctx.cfg.apps[name]; ok {
return true
func (ctx Context) AppIfConfigured(name string) (any, error) {
app, ok := ctx.cfg.apps[name]
if !ok || app == nil {
return nil, nil
}
appRaw := ctx.cfg.AppsRaw[name]
return appRaw != nil

appModule, err := ctx.App(name)
if err != nil {
return nil, err
}
return appModule, nil
}

// Storage returns the configured Caddy storage implementation.
Expand Down
16 changes: 5 additions & 11 deletions modules/caddypki/adminapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,14 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
a.ctx = ctx
a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)

// First check if the PKI app was configured, because
// a.ctx.App() has the side effect of instantiating
// and provisioning an app even if it wasn't configured.
pkiAppConfigured := a.ctx.AppIsConfigured("pki")
if !pkiAppConfigured {
return nil
}

// Load the PKI app, so we can query it for information.
appModule, err := a.ctx.App("pki")
// Avoid initializing PKI if it wasn't configured
pkiApp, err := a.ctx.AppIfConfigured("pki")
if err != nil {
return err
}
a.pkiApp = appModule.(*PKI)
if pkiApp != nil {
a.pkiApp = pkiApp.(*PKI)
}

return nil
}
Expand Down