Skip to content

Commit 67e3b3f

Browse files
author
mirkobrombin
committed
feat: implement a convenient IsEnabled method to figure out if a plugin must be enabled or not
1 parent b63615b commit 67e3b3f

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

internal/plugin/plugin_base.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ func (bp *BasePlugin) SetupLoggers(conf config.SiteConfig, pluginName string, do
2929

3030
return nil
3131
}
32+
33+
// IsEnabled returns true if the plugin is enabled for the given site.
34+
func (bp *BasePlugin) IsEnabled(conf interface{}) bool {
35+
if conf == nil {
36+
return false
37+
}
38+
39+
// We assume that the plugin config has an "Enable" field.
40+
enabled, ok := conf.(map[string]interface{})["enable"].(bool)
41+
if !ok {
42+
return false
43+
}
44+
45+
return enabled
46+
}

plugins/auth.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ func (p *AuthPlugin) OnInitForSite(conf config.SiteConfig, domainLogger *logger.
8080
}
8181

8282
if rawMap, ok := pluginConfigRaw.(map[string]interface{}); ok {
83-
// ProtectedPaths
84-
if en, ok := rawMap["enable"].(bool); ok {
85-
authConfig.Enable = en
86-
}
83+
authConfig.Enable = p.IsEnabled(rawMap)
8784

85+
// ProtectedPaths
8886
if paths, ok := rawMap["protected_paths"].([]interface{}); ok {
8987
for _, path := range paths {
9088
if pStr, ok := path.(string); ok {

plugins/custom_header.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func (p *CustomHeaderPlugin) OnInitForSite(conf config.SiteConfig, domainLogger
2929
if err := p.SetupLoggers(conf, p.Name(), domainLogger); err != nil {
3030
return err
3131
}
32+
33+
pluginConfigRaw, ok := conf.PluginConfigs[p.Name()]
34+
if !ok || !p.IsEnabled(pluginConfigRaw) {
35+
return nil
36+
}
37+
3238
p.siteConfigs[conf.Domain] = conf
3339
return nil
3440
}

plugins/docker_base.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ func (d *DockerBasePlugin) OnInitForSite(conf config.SiteConfig, domainLogger *l
5353
raw, ok := conf.PluginConfigs[d.Name()]
5454
if ok {
5555
if rawMap, ok := raw.(map[string]interface{}); ok {
56-
if v, ok := rawMap["enable"].(bool); ok {
57-
cfg.Enable = v
58-
}
56+
cfg.Enable = d.IsEnabled(rawMap)
5957
if v, ok := rawMap["mode"].(string); ok {
6058
cfg.Mode = v
6159
}

plugins/docker_standard.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ func (d *DockerStandardPlugin) OnInitForSite(conf config.SiteConfig, domainLogge
5959
raw, ok := conf.PluginConfigs[d.Name()]
6060
if ok {
6161
if rawMap, ok := raw.(map[string]interface{}); ok {
62-
if v, ok := rawMap["enable"].(bool); ok {
63-
cfg.Enable = v
64-
}
62+
cfg.Enable = d.IsEnabled(rawMap)
6563
if v, ok := rawMap["dockerfile_path"].(string); ok {
6664
cfg.DockerfilePath = v
6765
}

plugins/nodejs.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ type NodeJSPluginConfig struct {
3131
type NodeJSPlugin struct {
3232
plugin.BasePlugin
3333

34-
mu sync.Mutex
35-
process *os.Process
36-
34+
mu sync.Mutex
35+
process *os.Process
3736
siteConfigs map[string]NodeJSPluginConfig
3837
}
3938

@@ -58,9 +57,8 @@ func (p *NodeJSPlugin) OnInitForSite(conf config.SiteConfig, domainLogger *logge
5857
}
5958
cfg := NodeJSPluginConfig{}
6059
if rawMap, ok := pluginConfigRaw.(map[string]interface{}); ok {
61-
if enable, ok := rawMap["enable"].(bool); ok {
62-
cfg.Enable = enable
63-
}
60+
// Use BasePlugin's IsEnabled method to determine if the plugin is enabled.
61+
cfg.Enable = p.IsEnabled(rawMap)
6462
if port, ok := rawMap["port"].(string); ok {
6563
cfg.Port = port
6664
}

plugins/php.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ func (p *PHPPlugin) OnInitForSite(conf config.SiteConfig, domainLogger *logger.L
4747

4848
cfg := PHPPluginConfig{}
4949
if rawMap, ok := pluginConfigRaw.(map[string]interface{}); ok {
50-
if enable, ok := rawMap["enable"].(bool); ok {
51-
cfg.Enable = enable
52-
}
50+
// Use BasePlugin's IsEnabled method to determine if the plugin is enabled.
51+
cfg.Enable = p.IsEnabled(rawMap)
5352
if fpmAddr, ok := rawMap["fpm_addr"].(string); ok {
5453
cfg.FPMAddr = fpmAddr
5554
}

plugins/python.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ func (p *PythonPlugin) OnInitForSite(conf config.SiteConfig, baseLogger *logger.
6363

6464
cfg := PythonPluginConfig{}
6565
if rawMap, ok := raw.(map[string]interface{}); ok {
66-
if v, ok := rawMap["enable"].(bool); ok {
67-
cfg.Enable = v
68-
}
66+
// Use BasePlugin's IsEnabled method to determine if the plugin is enabled.
67+
cfg.Enable = p.IsEnabled(rawMap)
6968
if v, ok := rawMap["port"].(string); ok {
7069
cfg.Port = v
7170
}

0 commit comments

Comments
 (0)