Refactor application configuration
- Rename StringsYML to LocYML in App struct for better clarity. - Modify LoadConfig() to accept AppConfig as a parameter for more flexible configuration loading. - Add debug output for configuration in NewApp(). - Update CreateContainerPOST to pass App instance to FromInputsToCmdParams. - Change FromInputsToCmdParams to use the network configuration from App's Config.ContainerEngine.Network. Update getLocalizations to use the new LocYML field for YAML unmarshalling.
- commit
- cc06cda7423193b25f8db61c89194ea0d4ce2bff
- author
- bnema · · 2023-10-09 13:51 UTC
- parent
- 79543aa
- signature
- unsigned
4 files · +10 -9
M internal/app/app.go +5 -5
| @@ -17,13 +17,12 @@ import ( | |||
| 17 | 17 | ||
| 18 | 18 | var ( | |
| 19 | 19 | OauthCallbackURL string | |
| 20 | - | config AppConfig | |
| 21 | 20 | ) | |
| 22 | 21 | ||
| 23 | 22 | type App struct { | |
| 24 | 23 | TemplateFS fs.FS | |
| 25 | 24 | PublicFS fs.FS | |
| 26 | - | StringsYML []byte // strings.yml contains the strings for the current language | |
| 25 | + | LocYML []byte // strings.yml contains the strings for the current language | |
| 27 | 26 | DBDir string | |
| 28 | 27 | DBFilename string | |
| 29 | 28 | DBPath string | |
| @@ -62,7 +61,7 @@ type ContainerEngineConfig struct { | |||
| 62 | 61 | Network string `yaml:"network"` | |
| 63 | 62 | } | |
| 64 | 63 | ||
| 65 | - | func LoadConfig() (AppConfig, error) { | |
| 64 | + | func LoadConfig(config AppConfig) (AppConfig, error) { | |
| 66 | 65 | // Load env elements | |
| 67 | 66 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | |
| 68 | 67 | config.General.RunEnv = os.Getenv("RUN_ENV") | |
| @@ -94,7 +93,8 @@ func LoadConfig() (AppConfig, error) { | |||
| 94 | 93 | ||
| 95 | 94 | func NewApp() *App { | |
| 96 | 95 | // Initialize AppConfig | |
| 97 | - | config, err := LoadConfig() | |
| 96 | + | config, err := LoadConfig(AppConfig{}) | |
| 97 | + | fmt.Printf("Config: %+v\n", config) | |
| 98 | 98 | if err != nil { | |
| 99 | 99 | log.Fatalf("Failed to load configuration: %v", err) | |
| 100 | 100 | } | |
| @@ -117,7 +117,7 @@ func NewApp() *App { | |||
| 117 | 117 | a := &App{ | |
| 118 | 118 | TemplateFS: templates.TemplateFS, | |
| 119 | 119 | PublicFS: webui.PublicFS, | |
| 120 | - | StringsYML: bytes, | |
| 120 | + | LocYML: bytes, | |
| 121 | 121 | DBDir: DBDir, | |
| 122 | 122 | DBFilename: DBFilename, | |
| 123 | 123 | Config: config, | |
| @@ -17,13 +17,12 @@ import ( | |||||
| 17 | 17 | ||||
| 18 | var ( | 18 | var ( | ||
| 19 | OauthCallbackURL string | 19 | OauthCallbackURL string | ||
| 20 | - | config AppConfig | |||
| 21 | ) | 20 | ) | ||
| 22 | 21 | ||||
| 23 | type App struct { | 22 | type App struct { | ||
| 24 | TemplateFS fs.FS | 23 | TemplateFS fs.FS | ||
| 25 | PublicFS fs.FS | 24 | PublicFS fs.FS | ||
| 26 | - | StringsYML []byte // strings.yml contains the strings for the current language | 25 | + | LocYML []byte // strings.yml contains the strings for the current language |
| 27 | DBDir string | 26 | DBDir string | ||
| 28 | DBFilename string | 27 | DBFilename string | ||
| 29 | DBPath string | 28 | DBPath string | ||
| @@ -62,7 +61,7 @@ type ContainerEngineConfig struct { | |||||
| 62 | Network string `yaml:"network"` | 61 | Network string `yaml:"network"` | ||
| 63 | } | 62 | } | ||
| 64 | 63 | ||||
| 65 | - | func LoadConfig() (AppConfig, error) { | 64 | + | func LoadConfig(config AppConfig) (AppConfig, error) { |
| 66 | // Load env elements | 65 | // Load env elements | ||
| 67 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | 66 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | ||
| 68 | config.General.RunEnv = os.Getenv("RUN_ENV") | 67 | config.General.RunEnv = os.Getenv("RUN_ENV") | ||
| @@ -94,7 +93,8 @@ func LoadConfig() (AppConfig, error) { | |||||
| 94 | 93 | ||||
| 95 | func NewApp() *App { | 94 | func NewApp() *App { | ||
| 96 | // Initialize AppConfig | 95 | // Initialize AppConfig | ||
| 97 | - | config, err := LoadConfig() | 96 | + | config, err := LoadConfig(AppConfig{}) |
| 97 | + | fmt.Printf("Config: %+v\n", config) | |||
| 98 | if err != nil { | 98 | if err != nil { | ||
| 99 | log.Fatalf("Failed to load configuration: %v", err) | 99 | log.Fatalf("Failed to load configuration: %v", err) | ||
| 100 | } | 100 | } | ||
| @@ -117,7 +117,7 @@ func NewApp() *App { | |||||
| 117 | a := &App{ | 117 | a := &App{ | ||
| 118 | TemplateFS: templates.TemplateFS, | 118 | TemplateFS: templates.TemplateFS, | ||
| 119 | PublicFS: webui.PublicFS, | 119 | PublicFS: webui.PublicFS, | ||
| 120 | - | StringsYML: bytes, | 120 | + | LocYML: bytes, |
| 121 | DBDir: DBDir, | 121 | DBDir: DBDir, | ||
| 122 | DBFilename: DBFilename, | 122 | DBFilename: DBFilename, | ||
| 123 | Config: config, | 123 | Config: config, | ||
M internal/httpserve/handler/createcontainer.go +1 -1
| @@ -106,7 +106,7 @@ func CreateContainerPOST(c echo.Context, a *app.App) error { | |||
| 106 | 106 | } | |
| 107 | 107 | } | |
| 108 | 108 | ||
| 109 | - | cmdParams, err := render.FromInputsToCmdParams(sanitizedInputs) | |
| 109 | + | cmdParams, err := render.FromInputsToCmdParams(sanitizedInputs, a) | |
| 110 | 110 | if err != nil { | |
| 111 | 111 | return sendError(c, err) | |
| 112 | 112 | } | |
| @@ -106,7 +106,7 @@ func CreateContainerPOST(c echo.Context, a *app.App) error { | |||||
| 106 | } | 106 | } | ||
| 107 | } | 107 | } | ||
| 108 | 108 | ||||
| 109 | - | cmdParams, err := render.FromInputsToCmdParams(sanitizedInputs) | 109 | + | cmdParams, err := render.FromInputsToCmdParams(sanitizedInputs, a) |
| 110 | if err != nil { | 110 | if err != nil { | ||
| 111 | return sendError(c, err) | 111 | return sendError(c, err) | ||
| 112 | } | 112 | } | ||
M internal/templates/render/cmdparams.go +3 -2
| @@ -4,11 +4,12 @@ import ( | |||
| 4 | 4 | "fmt" | |
| 5 | 5 | "strings" | |
| 6 | 6 | ||
| 7 | + | "github.com/bnema/gordon/internal/app" | |
| 7 | 8 | "github.com/bnema/gordon/pkg/docker" | |
| 8 | 9 | ) | |
| 9 | 10 | ||
| 10 | 11 | // FromInputsToCmdParams transforms the inputs map into a ContainerCommandParams struct | |
| 11 | - | func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandParams, error) { | |
| 12 | + | func FromInputsToCmdParams(inputs map[string]string, a *app.App) (docker.ContainerCommandParams, error) { | |
| 12 | 13 | ||
| 13 | 14 | volumeStr := inputs["volumes"] | |
| 14 | 15 | volumeSlice := strings.Split(volumeStr, ",") | |
| @@ -45,7 +46,7 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar | |||
| 45 | 46 | Restart: inputs["restart"], | |
| 46 | 47 | Volumes: volumeSlice, | |
| 47 | 48 | Environment: environmentSlice, | |
| 48 | - | Network: ("traefik"), | |
| 49 | + | Network: a.Config.ContainerEngine.Network, | |
| 49 | 50 | PortMappings: portMappings, | |
| 50 | 51 | } | |
| 51 | 52 | ||
| @@ -4,11 +4,12 @@ import ( | |||||
| 4 | "fmt" | 4 | "fmt" | ||
| 5 | "strings" | 5 | "strings" | ||
| 6 | 6 | ||||
| 7 | + | "github.com/bnema/gordon/internal/app" | |||
| 7 | "github.com/bnema/gordon/pkg/docker" | 8 | "github.com/bnema/gordon/pkg/docker" | ||
| 8 | ) | 9 | ) | ||
| 9 | 10 | ||||
| 10 | // FromInputsToCmdParams transforms the inputs map into a ContainerCommandParams struct | 11 | // FromInputsToCmdParams transforms the inputs map into a ContainerCommandParams struct | ||
| 11 | - | func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandParams, error) { | 12 | + | func FromInputsToCmdParams(inputs map[string]string, a *app.App) (docker.ContainerCommandParams, error) { |
| 12 | 13 | ||||
| 13 | volumeStr := inputs["volumes"] | 14 | volumeStr := inputs["volumes"] | ||
| 14 | volumeSlice := strings.Split(volumeStr, ",") | 15 | volumeSlice := strings.Split(volumeStr, ",") | ||
| @@ -45,7 +46,7 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar | |||||
| 45 | Restart: inputs["restart"], | 46 | Restart: inputs["restart"], | ||
| 46 | Volumes: volumeSlice, | 47 | Volumes: volumeSlice, | ||
| 47 | Environment: environmentSlice, | 48 | Environment: environmentSlice, | ||
| 48 | - | Network: ("traefik"), | 49 | + | Network: a.Config.ContainerEngine.Network, |
| 49 | PortMappings: portMappings, | 50 | PortMappings: portMappings, | ||
| 50 | } | 51 | } | ||
| 51 | 52 | ||||
M internal/templates/render/localization.go +1 -1
| @@ -41,7 +41,7 @@ func GetLocalization(lang string, a *app.App) (map[string]interface{}, error) { | |||
| 41 | 41 | func getLocalizations(a *app.App) (Localizations, error) { | |
| 42 | 42 | var loc Localizations | |
| 43 | 43 | ||
| 44 | - | err := yaml.Unmarshal(a.StringsYML, &loc) | |
| 44 | + | err := yaml.Unmarshal(a.LocYML, &loc) | |
| 45 | 45 | if err != nil { | |
| 46 | 46 | return loc, fmt.Errorf("failed to unmarshal strings.yml: %w", err) | |
| 47 | 47 | } | |
| @@ -41,7 +41,7 @@ func GetLocalization(lang string, a *app.App) (map[string]interface{}, error) { | |||||
| 41 | func getLocalizations(a *app.App) (Localizations, error) { | 41 | func getLocalizations(a *app.App) (Localizations, error) { | ||
| 42 | var loc Localizations | 42 | var loc Localizations | ||
| 43 | 43 | ||||
| 44 | - | err := yaml.Unmarshal(a.StringsYML, &loc) | 44 | + | err := yaml.Unmarshal(a.LocYML, &loc) |
| 45 | if err != nil { | 45 | if err != nil { | ||
| 46 | return loc, fmt.Errorf("failed to unmarshal strings.yml: %w", err) | 46 | return loc, fmt.Errorf("failed to unmarshal strings.yml: %w", err) | ||
| 47 | } | 47 | } | ||