Skip to content

Commit f07e306

Browse files
author
mirkobrombin
committed
feat: implement global configuration loading and plugin enablement checks
1 parent e0314ba commit f07e306

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

cmd/goup/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/mirkobrombin/goup/internal/cli"
8+
"github.com/mirkobrombin/goup/internal/config"
59
"github.com/mirkobrombin/goup/internal/plugin"
610
"github.com/mirkobrombin/goup/plugins"
711
)
812

913
func main() {
14+
// Load global configuration
15+
if err := config.LoadGlobalConfig(); err != nil {
16+
fmt.Printf("Error loading global config: %v\n", err)
17+
os.Exit(1)
18+
}
19+
1020
pluginManager := plugin.NewPluginManager()
1121
plugin.SetDefaultPluginManager(pluginManager)
1222

internal/config/global_config.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
// GlobalConfig contains the global settings for GoUP.
10+
type GlobalConfig struct {
11+
EnableAPI bool `json:"enable_api"`
12+
DashboardPort int `json:"dashboard_port"`
13+
EnabledPlugins []string `json:"enabled_plugins"` // empty means all enabled
14+
}
15+
16+
// GlobalConf is the global configuration in memory.
17+
var GlobalConf *GlobalConfig
18+
var globalConfName = "conf.global.json"
19+
20+
// LoadGlobalConfig loads the global configuration file.
21+
func LoadGlobalConfig() error {
22+
configDir := GetConfigDir()
23+
configFile := filepath.Join(configDir, globalConfName)
24+
if _, err := os.Stat(configFile); os.IsNotExist(err) {
25+
// Valori di default
26+
GlobalConf = &GlobalConfig{
27+
EnableAPI: true,
28+
DashboardPort: 6008,
29+
EnabledPlugins: []string{},
30+
}
31+
return nil
32+
}
33+
34+
data, err := os.ReadFile(configFile)
35+
if err != nil {
36+
return err
37+
}
38+
var conf GlobalConfig
39+
if err := json.Unmarshal(data, &conf); err != nil {
40+
return err
41+
}
42+
GlobalConf = &conf
43+
return nil
44+
}
45+
46+
// SaveGlobalConfig saves the global configuration file.
47+
func SaveGlobalConfig() error {
48+
configDir := GetConfigDir()
49+
configFile := filepath.Join(configDir, globalConfName)
50+
data, err := json.MarshalIndent(GlobalConf, "", " ")
51+
if err != nil {
52+
return err
53+
}
54+
return os.WriteFile(configFile, data, 0644)
55+
}

internal/plugin/plugin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func (pm *PluginManager) InitPlugins() error {
7777
defer pm.mu.Unlock()
7878

7979
for _, plugin := range pm.plugins {
80+
if !isPluginEnabled(plugin.Name()) {
81+
fmt.Printf("Plugin %s is disabled\n", plugin.Name())
82+
continue
83+
}
8084
if err := plugin.OnInit(); err != nil {
8185
return err
8286
}
@@ -90,6 +94,9 @@ func (pm *PluginManager) InitPluginsForSite(conf config.SiteConfig, l *logger.Lo
9094
defer pm.mu.Unlock()
9195

9296
for _, plugin := range pm.plugins {
97+
if !isPluginEnabled(plugin.Name()) {
98+
continue
99+
}
93100
if err := plugin.OnInitForSite(conf, l); err != nil {
94101
return err
95102
}
@@ -109,6 +116,18 @@ func (pm *PluginManager) GetRegisteredPlugins() []string {
109116
return names
110117
}
111118

119+
func isPluginEnabled(name string) bool {
120+
if config.GlobalConf == nil || len(config.GlobalConf.EnabledPlugins) == 0 {
121+
return true
122+
}
123+
for _, pName := range config.GlobalConf.EnabledPlugins {
124+
if pName == name {
125+
return true
126+
}
127+
}
128+
return false
129+
}
130+
112131
// PluginMiddleware applies the plugin hooks around each HTTP request.
113132
func PluginMiddleware(pm *PluginManager) middleware.MiddlewareFunc {
114133
return func(next http.Handler) http.Handler {

0 commit comments

Comments
 (0)