Refactor Config structure, remove debug logs, update dev dependencies, and adjust affected functions
- commit
- e31f60c9d922901acee3d0d651acc13e7aed9fec
- author
- bnema · · 2023-10-12 12:49 UTC
- parent
- ab63437
- signature
- unsigned
4 files · +999 -16
M internal/app/app.go +12 -14
| @@ -28,9 +28,9 @@ type App struct { | |||
| 28 | 28 | DBPath string | |
| 29 | 29 | InitialChecksum string | |
| 30 | 30 | DB *sql.DB | |
| 31 | - | Config AppConfig | |
| 31 | + | Config Config | |
| 32 | 32 | } | |
| 33 | - | type AppConfig struct { | |
| 33 | + | type Config struct { | |
| 34 | 34 | General GeneralConfig `yaml:"General"` | |
| 35 | 35 | Http HttpConfig `yaml:"Http"` | |
| 36 | 36 | Admin AdminConfig `yaml:"Admin"` | |
| @@ -38,10 +38,11 @@ type AppConfig struct { | |||
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | 40 | type GeneralConfig struct { | |
| 41 | - | RunEnv string | |
| 42 | - | BuildDir string | |
| 43 | - | BuildVersion string | |
| 41 | + | RunEnv string // come from env | |
| 42 | + | BuildDir string // come from env | |
| 43 | + | BuildVersion string // come from env | |
| 44 | 44 | StorageDir string `yaml:"storageDir"` | |
| 45 | + | GordonToken string `yaml:"gordonToken"` | |
| 45 | 46 | } | |
| 46 | 47 | ||
| 47 | 48 | type HttpConfig struct { | |
| @@ -61,7 +62,7 @@ type ContainerEngineConfig struct { | |||
| 61 | 62 | Network string `yaml:"network"` | |
| 62 | 63 | } | |
| 63 | 64 | ||
| 64 | - | func LoadConfig(config AppConfig) (AppConfig, error) { | |
| 65 | + | func LoadConfig(config *Config) (*Config, error) { | |
| 65 | 66 | // Load env elements | |
| 66 | 67 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | |
| 67 | 68 | config.General.RunEnv = os.Getenv("RUN_ENV") | |
| @@ -72,10 +73,6 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||
| 72 | 73 | config.General.RunEnv = "prod" | |
| 73 | 74 | config.General.BuildDir = "." | |
| 74 | 75 | } | |
| 75 | - | ||
| 76 | - | fmt.Printf("RUN_ENV: %s\n", config.General.RunEnv) | |
| 77 | - | fmt.Printf("BUILD_DIR: %s\n", config.General.BuildDir) | |
| 78 | - | ||
| 79 | 76 | // Load config file | |
| 80 | 77 | configFile := fmt.Sprintf("%s/config.yml", config.General.BuildDir) | |
| 81 | 78 | configData, err := os.ReadFile(configFile) | |
| @@ -93,7 +90,8 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||
| 93 | 90 | ||
| 94 | 91 | func NewApp() *App { | |
| 95 | 92 | // Initialize AppConfig | |
| 96 | - | config, err := LoadConfig(AppConfig{}) | |
| 93 | + | config := &Config{} | |
| 94 | + | _, err := LoadConfig(config) | |
| 97 | 95 | if err != nil { | |
| 98 | 96 | log.Fatalf("Failed to load configuration: %v", err) | |
| 99 | 97 | } | |
| @@ -119,7 +117,7 @@ func NewApp() *App { | |||
| 119 | 117 | LocYML: bytes, | |
| 120 | 118 | DBDir: DBDir, | |
| 121 | 119 | DBFilename: DBFilename, | |
| 122 | - | Config: config, | |
| 120 | + | Config: *config, | |
| 123 | 121 | } | |
| 124 | 122 | ||
| 125 | 123 | OauthCallbackURL = config.GenerateOauthCallbackURL() | |
| @@ -127,7 +125,7 @@ func NewApp() *App { | |||
| 127 | 125 | } | |
| 128 | 126 | ||
| 129 | 127 | // NewDockerConfig creates and returns a new Docker client configuration | |
| 130 | - | func (config *AppConfig) NewDockerConfig() *docker.Config { | |
| 128 | + | func (config *Config) NewDockerConfig() *docker.Config { | |
| 131 | 129 | if config.ContainerEngine.Podman { | |
| 132 | 130 | return &docker.Config{ | |
| 133 | 131 | Sock: config.ContainerEngine.PodmanSock, | |
| @@ -141,7 +139,7 @@ func (config *AppConfig) NewDockerConfig() *docker.Config { | |||
| 141 | 139 | } | |
| 142 | 140 | ||
| 143 | 141 | // GenerateOauthCallbackURL generates the OAuth callback URL | |
| 144 | - | func (config *AppConfig) GenerateOauthCallbackURL() string { | |
| 142 | + | func (config *Config) GenerateOauthCallbackURL() string { | |
| 145 | 143 | var scheme, port string | |
| 146 | 144 | ||
| 147 | 145 | if config.General.RunEnv == "dev" { | |
| @@ -28,9 +28,9 @@ type App struct { | |||||
| 28 | DBPath string | 28 | DBPath string | ||
| 29 | InitialChecksum string | 29 | InitialChecksum string | ||
| 30 | DB *sql.DB | 30 | DB *sql.DB | ||
| 31 | - | Config AppConfig | 31 | + | Config Config |
| 32 | } | 32 | } | ||
| 33 | - | type AppConfig struct { | 33 | + | type Config struct { |
| 34 | General GeneralConfig `yaml:"General"` | 34 | General GeneralConfig `yaml:"General"` | ||
| 35 | Http HttpConfig `yaml:"Http"` | 35 | Http HttpConfig `yaml:"Http"` | ||
| 36 | Admin AdminConfig `yaml:"Admin"` | 36 | Admin AdminConfig `yaml:"Admin"` | ||
| @@ -38,10 +38,11 @@ type AppConfig struct { | |||||
| 38 | } | 38 | } | ||
| 39 | 39 | ||||
| 40 | type GeneralConfig struct { | 40 | type GeneralConfig struct { | ||
| 41 | - | RunEnv string | 41 | + | RunEnv string // come from env |
| 42 | - | BuildDir string | 42 | + | BuildDir string // come from env |
| 43 | - | BuildVersion string | 43 | + | BuildVersion string // come from env |
| 44 | StorageDir string `yaml:"storageDir"` | 44 | StorageDir string `yaml:"storageDir"` | ||
| 45 | + | GordonToken string `yaml:"gordonToken"` | |||
| 45 | } | 46 | } | ||
| 46 | 47 | ||||
| 47 | type HttpConfig struct { | 48 | type HttpConfig struct { | ||
| @@ -61,7 +62,7 @@ type ContainerEngineConfig struct { | |||||
| 61 | Network string `yaml:"network"` | 62 | Network string `yaml:"network"` | ||
| 62 | } | 63 | } | ||
| 63 | 64 | ||||
| 64 | - | func LoadConfig(config AppConfig) (AppConfig, error) { | 65 | + | func LoadConfig(config *Config) (*Config, error) { |
| 65 | // Load env elements | 66 | // Load env elements | ||
| 66 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | 67 | config.General.BuildVersion = os.Getenv("BUILD_VERSION") | ||
| 67 | config.General.RunEnv = os.Getenv("RUN_ENV") | 68 | config.General.RunEnv = os.Getenv("RUN_ENV") | ||
| @@ -72,10 +73,6 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||||
| 72 | config.General.RunEnv = "prod" | 73 | config.General.RunEnv = "prod" | ||
| 73 | config.General.BuildDir = "." | 74 | config.General.BuildDir = "." | ||
| 74 | } | 75 | } | ||
| 75 | - | ||||
| 76 | - | fmt.Printf("RUN_ENV: %s\n", config.General.RunEnv) | |||
| 77 | - | fmt.Printf("BUILD_DIR: %s\n", config.General.BuildDir) | |||
| 78 | - | ||||
| 79 | // Load config file | 76 | // Load config file | ||
| 80 | configFile := fmt.Sprintf("%s/config.yml", config.General.BuildDir) | 77 | configFile := fmt.Sprintf("%s/config.yml", config.General.BuildDir) | ||
| 81 | configData, err := os.ReadFile(configFile) | 78 | configData, err := os.ReadFile(configFile) | ||
| @@ -93,7 +90,8 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||||
| 93 | 90 | ||||
| 94 | func NewApp() *App { | 91 | func NewApp() *App { | ||
| 95 | // Initialize AppConfig | 92 | // Initialize AppConfig | ||
| 96 | - | config, err := LoadConfig(AppConfig{}) | 93 | + | config := &Config{} |
| 94 | + | _, err := LoadConfig(config) | |||
| 97 | if err != nil { | 95 | if err != nil { | ||
| 98 | log.Fatalf("Failed to load configuration: %v", err) | 96 | log.Fatalf("Failed to load configuration: %v", err) | ||
| 99 | } | 97 | } | ||
| @@ -119,7 +117,7 @@ func NewApp() *App { | |||||
| 119 | LocYML: bytes, | 117 | LocYML: bytes, | ||
| 120 | DBDir: DBDir, | 118 | DBDir: DBDir, | ||
| 121 | DBFilename: DBFilename, | 119 | DBFilename: DBFilename, | ||
| 122 | - | Config: config, | 120 | + | Config: *config, |
| 123 | } | 121 | } | ||
| 124 | 122 | ||||
| 125 | OauthCallbackURL = config.GenerateOauthCallbackURL() | 123 | OauthCallbackURL = config.GenerateOauthCallbackURL() | ||
| @@ -127,7 +125,7 @@ func NewApp() *App { | |||||
| 127 | } | 125 | } | ||
| 128 | 126 | ||||
| 129 | // NewDockerConfig creates and returns a new Docker client configuration | 127 | // NewDockerConfig creates and returns a new Docker client configuration | ||
| 130 | - | func (config *AppConfig) NewDockerConfig() *docker.Config { | 128 | + | func (config *Config) NewDockerConfig() *docker.Config { |
| 131 | if config.ContainerEngine.Podman { | 129 | if config.ContainerEngine.Podman { | ||
| 132 | return &docker.Config{ | 130 | return &docker.Config{ | ||
| 133 | Sock: config.ContainerEngine.PodmanSock, | 131 | Sock: config.ContainerEngine.PodmanSock, | ||
| @@ -141,7 +139,7 @@ func (config *AppConfig) NewDockerConfig() *docker.Config { | |||||
| 141 | } | 139 | } | ||
| 142 | 140 | ||||
| 143 | // GenerateOauthCallbackURL generates the OAuth callback URL | 141 | // GenerateOauthCallbackURL generates the OAuth callback URL | ||
| 144 | - | func (config *AppConfig) GenerateOauthCallbackURL() string { | 142 | + | func (config *Config) GenerateOauthCallbackURL() string { |
| 145 | var scheme, port string | 143 | var scheme, port string | ||
| 146 | 144 | ||||
| 147 | if config.General.RunEnv == "dev" { | 145 | if config.General.RunEnv == "dev" { | ||
A package-lock.json +983 -0 generated file: collapsed
M package.json +2 -0
| @@ -1,6 +1,8 @@ | |||
| 1 | 1 | { | |
| 2 | 2 | "devDependencies": { | |
| 3 | 3 | "@catppuccin/tailwindcss": "^0.1.6", | |
| 4 | + | "prettier": "^3.0.3", | |
| 5 | + | "prettier-plugin-tailwindcss": "^0.5.5", | |
| 4 | 6 | "tailwindcss": "^3.3.3" | |
| 5 | 7 | }, | |
| 6 | 8 | "scripts": { | |
| @@ -1,6 +1,8 @@ | |||||
| 1 | { | 1 | { | ||
| 2 | "devDependencies": { | 2 | "devDependencies": { | ||
| 3 | "@catppuccin/tailwindcss": "^0.1.6", | 3 | "@catppuccin/tailwindcss": "^0.1.6", | ||
| 4 | + | "prettier": "^3.0.3", | |||
| 5 | + | "prettier-plugin-tailwindcss": "^0.5.5", | |||
| 4 | "tailwindcss": "^3.3.3" | 6 | "tailwindcss": "^3.3.3" | ||
| 5 | }, | 7 | }, | ||
| 6 | "scripts": { | 8 | "scripts": { | ||
M pkg/store/storeimages.go +2 -2
| @@ -17,13 +17,13 @@ type StorageConfig struct { | |||
| 17 | 17 | Images []types.ImageSummary | |
| 18 | 18 | } | |
| 19 | 19 | ||
| 20 | - | func NewStorageConfig(config *app.AppConfig) *StorageConfig { | |
| 20 | + | func NewStorageConfig(config *app.Config) *StorageConfig { | |
| 21 | 21 | return &StorageConfig{ | |
| 22 | 22 | StorageDir: config.General.StorageDir, | |
| 23 | 23 | } | |
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | - | func SaveImageToStorage(config *app.AppConfig, filename string, buf io.Reader) (string, error) { | |
| 26 | + | func SaveImageToStorage(config *app.Config, filename string, buf io.Reader) (string, error) { | |
| 27 | 27 | // Check if the folder exist if not create it | |
| 28 | 28 | if _, err := os.Stat(config.General.StorageDir); os.IsNotExist(err) { | |
| 29 | 29 | err := os.MkdirAll(config.General.StorageDir, 0755) | |
| @@ -17,13 +17,13 @@ type StorageConfig struct { | |||||
| 17 | Images []types.ImageSummary | 17 | Images []types.ImageSummary | ||
| 18 | } | 18 | } | ||
| 19 | 19 | ||||
| 20 | - | func NewStorageConfig(config *app.AppConfig) *StorageConfig { | 20 | + | func NewStorageConfig(config *app.Config) *StorageConfig { |
| 21 | return &StorageConfig{ | 21 | return &StorageConfig{ | ||
| 22 | StorageDir: config.General.StorageDir, | 22 | StorageDir: config.General.StorageDir, | ||
| 23 | } | 23 | } | ||
| 24 | } | 24 | } | ||
| 25 | 25 | ||||
| 26 | - | func SaveImageToStorage(config *app.AppConfig, filename string, buf io.Reader) (string, error) { | 26 | + | func SaveImageToStorage(config *app.Config, filename string, buf io.Reader) (string, error) { |
| 27 | // Check if the folder exist if not create it | 27 | // Check if the folder exist if not create it | ||
| 28 | if _, err := os.Stat(config.General.StorageDir); os.IsNotExist(err) { | 28 | if _, err := os.Stat(config.General.StorageDir); os.IsNotExist(err) { | ||
| 29 | err := os.MkdirAll(config.General.StorageDir, 0755) | 29 | err := os.MkdirAll(config.General.StorageDir, 0755) | ||