Refactor types into sub cats, read some config elements from config.yml
- commit
- 0ba72799ed244c059f9aead9e2d8daa6920cfdbf
- author
- bnema · · 2023-09-19 17:31 UTC
- parent
- 0ec97d9
- signature
- unsigned
6 files · +53 -47
M cmd/main.go +2 -2
| @@ -48,8 +48,8 @@ func main() { | |||
| 48 | 48 | e.HidePort = true | |
| 49 | 49 | e = httpserve.RegisterRoutes(e, a) | |
| 50 | 50 | ||
| 51 | - | log.Println("Starting server on port", a.Config.HttpPort) | |
| 52 | - | if err := e.Start(fmt.Sprintf(":%d", a.Config.HttpPort)); err != nil { | |
| 51 | + | log.Println("Starting server on port", a.Config.Http.Port) | |
| 52 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.Http.Port)); err != nil { | |
| 53 | 53 | log.Fatal("Server error:", err) | |
| 54 | 54 | } | |
| 55 | 55 | } | |
| @@ -48,8 +48,8 @@ func main() { | |||||
| 48 | e.HidePort = true | 48 | e.HidePort = true | ||
| 49 | e = httpserve.RegisterRoutes(e, a) | 49 | e = httpserve.RegisterRoutes(e, a) | ||
| 50 | 50 | ||||
| 51 | - | log.Println("Starting server on port", a.Config.HttpPort) | 51 | + | log.Println("Starting server on port", a.Config.Http.Port) |
| 52 | - | if err := e.Start(fmt.Sprintf(":%d", a.Config.HttpPort)); err != nil { | 52 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.Http.Port)); err != nil { |
| 53 | log.Fatal("Server error:", err) | 53 | log.Fatal("Server error:", err) | ||
| 54 | } | 54 | } | ||
| 55 | } | 55 | } | ||
M internal/app/app.go +20 -26
| @@ -36,10 +36,10 @@ type App struct { | |||
| 36 | 36 | Config AppConfig | |
| 37 | 37 | } | |
| 38 | 38 | type AppConfig struct { | |
| 39 | - | General GeneralConfig `yaml:"general"` | |
| 40 | - | Http HttpConfig `yaml:"http"` | |
| 41 | - | Admin AdminConfig `yaml:"admin"` | |
| 42 | - | Docker DockerConfig `yaml:"docker"` | |
| 39 | + | General GeneralConfig `yaml:"General"` | |
| 40 | + | Http HttpConfig `yaml:"Http"` | |
| 41 | + | Admin AdminConfig `yaml:"Admin"` | |
| 42 | + | ContainerEngine ContainerEngineConfig `yaml:"ContainerEngine"` | |
| 43 | 43 | } | |
| 44 | 44 | ||
| 45 | 45 | type GeneralConfig struct { | |
| @@ -57,8 +57,9 @@ type AdminConfig struct { | |||
| 57 | 57 | Path string `yaml:"path"` | |
| 58 | 58 | } | |
| 59 | 59 | ||
| 60 | - | type DockerConfig struct { | |
| 61 | - | Sock string `yaml:"sock"` | |
| 60 | + | type ContainerEngineConfig struct { | |
| 61 | + | Sock string `yaml:"sock"` | |
| 62 | + | PodmanEnable bool `yaml:"podman"` | |
| 62 | 63 | } | |
| 63 | 64 | ||
| 64 | 65 | func InitializeEnvironment() { | |
| @@ -72,8 +73,9 @@ func InitializeEnvironment() { | |||
| 72 | 73 | ||
| 73 | 74 | func LoadConfig() (AppConfig, error) { | |
| 74 | 75 | var config AppConfig | |
| 75 | - | buildDir, configFile := getEnvPaths() | |
| 76 | - | err := parser.OpenYamlFile(os.DirFS(buildDir), configFile, &config, buildDir) // Replace `nil` with your actual filesystem | |
| 76 | + | configDir, configFile := getConfigFile() | |
| 77 | + | fsys := os.DirFS(configDir) // Use directory path here | |
| 78 | + | err := parser.OpenYamlFile(fsys, configFile, &config) // Assuming it doesn't need the last argument | |
| 77 | 79 | return config, err | |
| 78 | 80 | } | |
| 79 | 81 | ||
| @@ -102,37 +104,29 @@ func NewApp() *App { | |||
| 102 | 104 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | |
| 103 | 105 | func (config *AppConfig) NewDockerConfig() *docker.Config { | |
| 104 | 106 | return &docker.Config{ | |
| 105 | - | DockerSock: config.DockerSock, | |
| 106 | - | PodmanEnable: config.PodmanEnable, | |
| 107 | - | PodmanSock: config.PodmanSock, | |
| 108 | - | } | |
| 107 | + | Sock: config.ContainerEngine.Sock, | |
| 108 | + | PodmanEnable: config.ContainerEngine.PodmanEnable} | |
| 109 | 109 | } | |
| 110 | 110 | ||
| 111 | 111 | func GenerateOauthCallbackURL(config AppConfig) string { | |
| 112 | 112 | var scheme, port string | |
| 113 | 113 | ||
| 114 | - | if config.RunEnv == "dev" { | |
| 114 | + | if config.General.RunEnv == "dev" { | |
| 115 | 115 | scheme = "http" | |
| 116 | - | port = fmt.Sprintf(":%d", config.HttpPort) | |
| 116 | + | port = fmt.Sprintf(":%d", config.Http.Port) | |
| 117 | 117 | } else { // Assuming "prod" | |
| 118 | 118 | scheme = "https" | |
| 119 | 119 | port = "" // Assuming that HTTPS will run on the default port 443 | |
| 120 | 120 | } | |
| 121 | 121 | ||
| 122 | - | domain := config.TopDomain | |
| 123 | - | if config.SubDomain != "" { | |
| 124 | - | domain = fmt.Sprintf("%s.%s", config.SubDomain, config.TopDomain) | |
| 122 | + | domain := config.Http.TopDomain | |
| 123 | + | if config.Http.SubDomain != "" { | |
| 124 | + | domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.TopDomain) | |
| 125 | 125 | } | |
| 126 | 126 | ||
| 127 | - | return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.AdminPath) | |
| 127 | + | return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.Admin.Path) | |
| 128 | 128 | } | |
| 129 | 129 | ||
| 130 | - | func getEnvPaths() (string, string) { | |
| 131 | - | wd, _ := os.Getwd() | |
| 132 | - | fmt.Println("Current Working Directory:", wd) | |
| 133 | - | ||
| 134 | - | if appEnv == "dev" { | |
| 135 | - | return "/tmp/", "config.yml" | |
| 136 | - | } | |
| 137 | - | return "./", "config.yml" | |
| 130 | + | func getConfigFile() (string, string) { | |
| 131 | + | return "tmp/", "config.yml" // assuming the file is in the current directory | |
| 138 | 132 | } | |
| @@ -36,10 +36,10 @@ type App struct { | |||||
| 36 | Config AppConfig | 36 | Config AppConfig | ||
| 37 | } | 37 | } | ||
| 38 | type AppConfig struct { | 38 | type AppConfig struct { | ||
| 39 | - | General GeneralConfig `yaml:"general"` | 39 | + | General GeneralConfig `yaml:"General"` |
| 40 | - | Http HttpConfig `yaml:"http"` | 40 | + | Http HttpConfig `yaml:"Http"` |
| 41 | - | Admin AdminConfig `yaml:"admin"` | 41 | + | Admin AdminConfig `yaml:"Admin"` |
| 42 | - | Docker DockerConfig `yaml:"docker"` | 42 | + | ContainerEngine ContainerEngineConfig `yaml:"ContainerEngine"` |
| 43 | } | 43 | } | ||
| 44 | 44 | ||||
| 45 | type GeneralConfig struct { | 45 | type GeneralConfig struct { | ||
| @@ -57,8 +57,9 @@ type AdminConfig struct { | |||||
| 57 | Path string `yaml:"path"` | 57 | Path string `yaml:"path"` | ||
| 58 | } | 58 | } | ||
| 59 | 59 | ||||
| 60 | - | type DockerConfig struct { | 60 | + | type ContainerEngineConfig struct { |
| 61 | - | Sock string `yaml:"sock"` | 61 | + | Sock string `yaml:"sock"` |
| 62 | + | PodmanEnable bool `yaml:"podman"` | |||
| 62 | } | 63 | } | ||
| 63 | 64 | ||||
| 64 | func InitializeEnvironment() { | 65 | func InitializeEnvironment() { | ||
| @@ -72,8 +73,9 @@ func InitializeEnvironment() { | |||||
| 72 | 73 | ||||
| 73 | func LoadConfig() (AppConfig, error) { | 74 | func LoadConfig() (AppConfig, error) { | ||
| 74 | var config AppConfig | 75 | var config AppConfig | ||
| 75 | - | buildDir, configFile := getEnvPaths() | 76 | + | configDir, configFile := getConfigFile() |
| 76 | - | err := parser.OpenYamlFile(os.DirFS(buildDir), configFile, &config, buildDir) // Replace `nil` with your actual filesystem | 77 | + | fsys := os.DirFS(configDir) // Use directory path here |
| 78 | + | err := parser.OpenYamlFile(fsys, configFile, &config) // Assuming it doesn't need the last argument | |||
| 77 | return config, err | 79 | return config, err | ||
| 78 | } | 80 | } | ||
| 79 | 81 | ||||
| @@ -102,37 +104,29 @@ func NewApp() *App { | |||||
| 102 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | 104 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | ||
| 103 | func (config *AppConfig) NewDockerConfig() *docker.Config { | 105 | func (config *AppConfig) NewDockerConfig() *docker.Config { | ||
| 104 | return &docker.Config{ | 106 | return &docker.Config{ | ||
| 105 | - | DockerSock: config.DockerSock, | 107 | + | Sock: config.ContainerEngine.Sock, |
| 106 | - | PodmanEnable: config.PodmanEnable, | 108 | + | PodmanEnable: config.ContainerEngine.PodmanEnable} |
| 107 | - | PodmanSock: config.PodmanSock, | |||
| 108 | - | } | |||
| 109 | } | 109 | } | ||
| 110 | 110 | ||||
| 111 | func GenerateOauthCallbackURL(config AppConfig) string { | 111 | func GenerateOauthCallbackURL(config AppConfig) string { | ||
| 112 | var scheme, port string | 112 | var scheme, port string | ||
| 113 | 113 | ||||
| 114 | - | if config.RunEnv == "dev" { | 114 | + | if config.General.RunEnv == "dev" { |
| 115 | scheme = "http" | 115 | scheme = "http" | ||
| 116 | - | port = fmt.Sprintf(":%d", config.HttpPort) | 116 | + | port = fmt.Sprintf(":%d", config.Http.Port) |
| 117 | } else { // Assuming "prod" | 117 | } else { // Assuming "prod" | ||
| 118 | scheme = "https" | 118 | scheme = "https" | ||
| 119 | port = "" // Assuming that HTTPS will run on the default port 443 | 119 | port = "" // Assuming that HTTPS will run on the default port 443 | ||
| 120 | } | 120 | } | ||
| 121 | 121 | ||||
| 122 | - | domain := config.TopDomain | 122 | + | domain := config.Http.TopDomain |
| 123 | - | if config.SubDomain != "" { | 123 | + | if config.Http.SubDomain != "" { |
| 124 | - | domain = fmt.Sprintf("%s.%s", config.SubDomain, config.TopDomain) | 124 | + | domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.TopDomain) |
| 125 | } | 125 | } | ||
| 126 | 126 | ||||
| 127 | - | return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.AdminPath) | 127 | + | return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.Admin.Path) |
| 128 | } | 128 | } | ||
| 129 | 129 | ||||
| 130 | - | func getEnvPaths() (string, string) { | 130 | + | func getConfigFile() (string, string) { |
| 131 | - | wd, _ := os.Getwd() | 131 | + | return "tmp/", "config.yml" // assuming the file is in the current directory |
| 132 | - | fmt.Println("Current Working Directory:", wd) | |||
| 133 | - | ||||
| 134 | - | if appEnv == "dev" { | |||
| 135 | - | return "/tmp/", "config.yml" | |||
| 136 | - | } | |||
| 137 | - | return "./", "config.yml" | |||
| 138 | } | 132 | } | ||
M internal/gotemplate/render/html.go +1 -1
| @@ -58,6 +58,6 @@ func GetHTMLRenderer(pathStr string, filename string, fs fs.FS, a *app.App) (*Re | |||
| 58 | 58 | } | |
| 59 | 59 | return &Renderer{ | |
| 60 | 60 | Template: tmpl, | |
| 61 | - | BuildVersion: a.Config.BuildVersion, | |
| 61 | + | BuildVersion: a.Config.General.BuildVersion, | |
| 62 | 62 | }, nil | |
| 63 | 63 | } | |
| @@ -58,6 +58,6 @@ func GetHTMLRenderer(pathStr string, filename string, fs fs.FS, a *app.App) (*Re | |||||
| 58 | } | 58 | } | ||
| 59 | return &Renderer{ | 59 | return &Renderer{ | ||
| 60 | Template: tmpl, | 60 | Template: tmpl, | ||
| 61 | - | BuildVersion: a.Config.BuildVersion, | 61 | + | BuildVersion: a.Config.General.BuildVersion, |
| 62 | }, nil | 62 | }, nil | ||
| 63 | } | 63 | } | ||
M internal/httpserve/handler/oauth.go +2 -1
| @@ -48,7 +48,8 @@ func RenderLoginPage(c echo.Context, a *app.App) error { | |||
| 48 | 48 | ||
| 49 | 49 | func StartOAuthGithub(c echo.Context, a *app.App) error { | |
| 50 | 50 | clientID := os.Getenv("GITHUB_APP_ID") | |
| 51 | - | redirectDomain := a.Config.OauthCallbackURL | |
| 51 | + | redirectDomain := app.GenerateOauthCallbackURL(a.Config) | |
| 52 | + | fmt.Print(redirectDomain) | |
| 52 | 53 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | |
| 53 | 54 | fmt.Print("redirectDomain:", redirectDomain) | |
| 54 | 55 | fmt.Print("encoded state :", encodedState) | |
| @@ -48,7 +48,8 @@ func RenderLoginPage(c echo.Context, a *app.App) error { | |||||
| 48 | 48 | ||||
| 49 | func StartOAuthGithub(c echo.Context, a *app.App) error { | 49 | func StartOAuthGithub(c echo.Context, a *app.App) error { | ||
| 50 | clientID := os.Getenv("GITHUB_APP_ID") | 50 | clientID := os.Getenv("GITHUB_APP_ID") | ||
| 51 | - | redirectDomain := a.Config.OauthCallbackURL | 51 | + | redirectDomain := app.GenerateOauthCallbackURL(a.Config) |
| 52 | + | fmt.Print(redirectDomain) | |||
| 52 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | 53 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | ||
| 53 | fmt.Print("redirectDomain:", redirectDomain) | 54 | fmt.Print("redirectDomain:", redirectDomain) | ||
| 54 | fmt.Print("encoded state :", encodedState) | 55 | fmt.Print("encoded state :", encodedState) | ||
M internal/httpserve/router.go +1 -1
| @@ -14,7 +14,7 @@ import ( | |||
| 14 | 14 | ||
| 15 | 15 | // RegisterRoutes registers all routes for the application | |
| 16 | 16 | func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo { | |
| 17 | - | AdminPath := a.Config.AdminPath | |
| 17 | + | AdminPath := a.Config.Admin.Path | |
| 18 | 18 | e.Use(middleware.SetCommonDataMiddleware(a)) | |
| 19 | 19 | e.Use(middleware.ErrorHandler) | |
| 20 | 20 | // Add session middleware | |
| @@ -14,7 +14,7 @@ import ( | |||||
| 14 | 14 | ||||
| 15 | // RegisterRoutes registers all routes for the application | 15 | // RegisterRoutes registers all routes for the application | ||
| 16 | func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo { | 16 | func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo { | ||
| 17 | - | AdminPath := a.Config.AdminPath | 17 | + | AdminPath := a.Config.Admin.Path |
| 18 | e.Use(middleware.SetCommonDataMiddleware(a)) | 18 | e.Use(middleware.SetCommonDataMiddleware(a)) | ||
| 19 | e.Use(middleware.ErrorHandler) | 19 | e.Use(middleware.ErrorHandler) | ||
| 20 | // Add session middleware | 20 | // Add session middleware | ||
M pkg/utils/docker/initclient.go +27 -16
| @@ -1,31 +1,42 @@ | |||
| 1 | 1 | package docker | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | - | "fmt" | |
| 5 | 4 | "log" | |
| 6 | - | ||
| 7 | - | "github.com/docker/docker/client" | |
| 8 | 5 | ) | |
| 9 | 6 | ||
| 7 | + | type ContainerEngineClient interface { | |
| 8 | + | InitializeClient(*Config) error | |
| 9 | + | // Add other methods that both Docker and Podman should implement | |
| 10 | + | } | |
| 10 | 11 | type Config struct { | |
| 11 | - | DockerSock string | |
| 12 | + | Sock string | |
| 12 | 13 | PodmanEnable bool | |
| 13 | - | PodmanSock string | |
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | - | var dockerClient *client.Client | |
| 16 | + | var engineClient ContainerEngineClient | |
| 17 | + | ||
| 18 | + | // DockerClient implements the ContainerEngineClient interface for Docker | |
| 19 | + | type DockerClient struct{} | |
| 20 | + | ||
| 21 | + | func (d *DockerClient) InitializeClient(config *Config) error { | |
| 22 | + | // Initialize Docker client here | |
| 23 | + | return nil // Return an error if something goes wrong | |
| 24 | + | } | |
| 25 | + | ||
| 26 | + | // PodmanClient implements the ContainerEngineClient interface for Podman | |
| 27 | + | type PodmanClient struct{} | |
| 17 | 28 | ||
| 18 | - | func InitializeDockerClient(config *Config) { | |
| 19 | - | // Based on configuration, decide whether to use Docker or Podman | |
| 20 | - | socketPath := config.DockerSock | |
| 29 | + | func (p *PodmanClient) InitializeClient(config *Config) error { | |
| 30 | + | // Initialize Podman client here | |
| 31 | + | return nil // Return an error if something goes wrong | |
| 32 | + | } | |
| 33 | + | func InitializeEngineClient(config *Config) { | |
| 21 | 34 | if config.PodmanEnable { | |
| 22 | - | socketPath = config.PodmanSock | |
| 35 | + | engineClient = &PodmanClient{} | |
| 36 | + | } else { | |
| 37 | + | engineClient = &DockerClient{} | |
| 23 | 38 | } | |
| 24 | - | ||
| 25 | - | // Initialize Docker client | |
| 26 | - | var err error | |
| 27 | - | dockerClient, err = client.NewClientWithOpts(client.WithHost(fmt.Sprintf("unix://%s", socketPath)), client.WithAPIVersionNegotiation()) | |
| 28 | - | if err != nil { | |
| 29 | - | log.Fatalf("Error initializing Docker client: %v", err) | |
| 39 | + | if err := engineClient.InitializeClient(config); err != nil { | |
| 40 | + | log.Fatalf("Error initializing container engine client: %v", err) | |
| 30 | 41 | } | |
| 31 | 42 | } | |
| @@ -1,31 +1,42 @@ | |||||
| 1 | package docker | 1 | package docker | ||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | - | "fmt" | |||
| 5 | "log" | 4 | "log" | ||
| 6 | - | ||||
| 7 | - | "github.com/docker/docker/client" | |||
| 8 | ) | 5 | ) | ||
| 9 | 6 | ||||
| 7 | + | type ContainerEngineClient interface { | |||
| 8 | + | InitializeClient(*Config) error | |||
| 9 | + | // Add other methods that both Docker and Podman should implement | |||
| 10 | + | } | |||
| 10 | type Config struct { | 11 | type Config struct { | ||
| 11 | - | DockerSock string | 12 | + | Sock string |
| 12 | PodmanEnable bool | 13 | PodmanEnable bool | ||
| 13 | - | PodmanSock string | |||
| 14 | } | 14 | } | ||
| 15 | 15 | ||||
| 16 | - | var dockerClient *client.Client | 16 | + | var engineClient ContainerEngineClient |
| 17 | + | ||||
| 18 | + | // DockerClient implements the ContainerEngineClient interface for Docker | |||
| 19 | + | type DockerClient struct{} | |||
| 20 | + | ||||
| 21 | + | func (d *DockerClient) InitializeClient(config *Config) error { | |||
| 22 | + | // Initialize Docker client here | |||
| 23 | + | return nil // Return an error if something goes wrong | |||
| 24 | + | } | |||
| 25 | + | ||||
| 26 | + | // PodmanClient implements the ContainerEngineClient interface for Podman | |||
| 27 | + | type PodmanClient struct{} | |||
| 17 | 28 | ||||
| 18 | - | func InitializeDockerClient(config *Config) { | 29 | + | func (p *PodmanClient) InitializeClient(config *Config) error { |
| 19 | - | // Based on configuration, decide whether to use Docker or Podman | 30 | + | // Initialize Podman client here |
| 20 | - | socketPath := config.DockerSock | 31 | + | return nil // Return an error if something goes wrong |
| 32 | + | } | |||
| 33 | + | func InitializeEngineClient(config *Config) { | |||
| 21 | if config.PodmanEnable { | 34 | if config.PodmanEnable { | ||
| 22 | - | socketPath = config.PodmanSock | 35 | + | engineClient = &PodmanClient{} |
| 36 | + | } else { | |||
| 37 | + | engineClient = &DockerClient{} | |||
| 23 | } | 38 | } | ||
| 24 | - | 39 | + | if err := engineClient.InitializeClient(config); err != nil { | |
| 25 | - | // Initialize Docker client | 40 | + | log.Fatalf("Error initializing container engine client: %v", err) |
| 26 | - | var err error | |||
| 27 | - | dockerClient, err = client.NewClientWithOpts(client.WithHost(fmt.Sprintf("unix://%s", socketPath)), client.WithAPIVersionNegotiation()) | |||
| 28 | - | if err != nil { | |||
| 29 | - | log.Fatalf("Error initializing Docker client: %v", err) | |||
| 30 | } | 41 | } | ||
| 31 | } | 42 | } | ||