WIP
- commit
- 2cec0acfc27d9d0d2d0e99d9687429917a26c70f
- author
- bnema · · 2023-09-17 18:31 UTC
- parent
- a5fe447
- signature
- unsigned
6 files · +60 -19
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.HttpPort) | |
| 52 | - | if err := e.Start(fmt.Sprintf(":%d", a.HttpPort)); err != nil { | |
| 51 | + | log.Println("Starting server on port", a.Config.HttpPort) | |
| 52 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.HttpPort)); 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.HttpPort) | 51 | + | log.Println("Starting server on port", a.Config.HttpPort) |
| 52 | - | if err := e.Start(fmt.Sprintf(":%d", a.HttpPort)); err != nil { | 52 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.HttpPort)); err != nil { |
| 53 | log.Fatal("Server error:", err) | 53 | log.Fatal("Server error:", err) | ||
| 54 | } | 54 | } | ||
| 55 | } | 55 | } | ||
M internal/app/app.go +53 -13
| @@ -4,8 +4,11 @@ import ( | |||
| 4 | 4 | "database/sql" | |
| 5 | 5 | "fmt" | |
| 6 | 6 | "io/fs" | |
| 7 | + | "log" | |
| 7 | 8 | "os" | |
| 8 | 9 | ||
| 10 | + | "github.com/bnema/gordon/internal/gotemplate" | |
| 11 | + | "github.com/bnema/gordon/internal/webui" | |
| 9 | 12 | "github.com/bnema/gordon/pkg/utils/docker" | |
| 10 | 13 | "github.com/bnema/gordon/pkg/utils/parser" | |
| 11 | 14 | ) | |
| @@ -32,19 +35,30 @@ type App struct { | |||
| 32 | 35 | DB *sql.DB | |
| 33 | 36 | Config AppConfig | |
| 34 | 37 | } | |
| 35 | - | ||
| 36 | 38 | type AppConfig struct { | |
| 37 | - | RunEnv string `yaml:"runEnv"` | |
| 38 | - | BuildVersion string `yaml:"buildVersion"` | |
| 39 | - | HttpPort int `yaml:"httpPort"` | |
| 40 | - | TopDomain string `yaml:"topDomain"` | |
| 41 | - | SubDomain string `yaml:"subDomain"` | |
| 42 | - | AdminPath string `yaml:"adminPath"` | |
| 43 | - | DockerSock string `yaml:"dockerSock"` | |
| 44 | - | PodmanEnable bool `yaml:"podmanEnable"` | |
| 45 | - | PodmanSock string `yaml:"podmanSock"` | |
| 46 | - | OauthCallbackURL string `yaml:"oauthCallbackURL"` | |
| 47 | - | BuildDir string `yaml:"buildDir"` | |
| 39 | + | General GeneralConfig `yaml:"general"` | |
| 40 | + | Http HttpConfig `yaml:"http"` | |
| 41 | + | Admin AdminConfig `yaml:"admin"` | |
| 42 | + | Docker DockerConfig `yaml:"docker"` | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | type GeneralConfig struct { | |
| 46 | + | RunEnv string `yaml:"runEnv"` | |
| 47 | + | BuildVersion string `yaml:"buildVersion"` | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | type HttpConfig struct { | |
| 51 | + | Port int `yaml:"port"` | |
| 52 | + | TopDomain string `yaml:"topDomain"` | |
| 53 | + | SubDomain string `yaml:"subDomain"` | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | type AdminConfig struct { | |
| 57 | + | Path string `yaml:"path"` | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | type DockerConfig struct { | |
| 61 | + | Sock string `yaml:"sock"` | |
| 48 | 62 | } | |
| 49 | 63 | ||
| 50 | 64 | func InitializeEnvironment() { | |
| @@ -53,6 +67,7 @@ func InitializeEnvironment() { | |||
| 53 | 67 | fmt.Errorf("Error initializing environment: %s", err) | |
| 54 | 68 | } | |
| 55 | 69 | OauthCallbackURL = GenerateOauthCallbackURL(config) | |
| 70 | + | ||
| 56 | 71 | } | |
| 57 | 72 | ||
| 58 | 73 | func LoadConfig() (AppConfig, error) { | |
| @@ -62,6 +77,28 @@ func LoadConfig() (AppConfig, error) { | |||
| 62 | 77 | return config, err | |
| 63 | 78 | } | |
| 64 | 79 | ||
| 80 | + | func NewApp() *App { | |
| 81 | + | // Initialize AppConfig | |
| 82 | + | config, err := LoadConfig() | |
| 83 | + | if err != nil { | |
| 84 | + | log.Fatalf("Failed to load configuration: %v", err) | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | // Initialize App | |
| 88 | + | a := &App{ | |
| 89 | + | TemplateFS: gotemplate.TemplateFS, | |
| 90 | + | PublicFS: webui.PublicFS, | |
| 91 | + | DBDir: DBDir, | |
| 92 | + | DBFilename: DBFilename, | |
| 93 | + | Config: config, | |
| 94 | + | } | |
| 95 | + | ||
| 96 | + | // If you have any other initializations like generating OAuth URL, do them here | |
| 97 | + | OauthCallbackURL = GenerateOauthCallbackURL(config) | |
| 98 | + | ||
| 99 | + | return a | |
| 100 | + | } | |
| 101 | + | ||
| 65 | 102 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | |
| 66 | 103 | func (config *AppConfig) NewDockerConfig() *docker.Config { | |
| 67 | 104 | return &docker.Config{ | |
| @@ -91,8 +128,11 @@ func GenerateOauthCallbackURL(config AppConfig) string { | |||
| 91 | 128 | } | |
| 92 | 129 | ||
| 93 | 130 | func getEnvPaths() (string, string) { | |
| 131 | + | wd, _ := os.Getwd() | |
| 132 | + | fmt.Println("Current Working Directory:", wd) | |
| 133 | + | ||
| 94 | 134 | if appEnv == "dev" { | |
| 95 | - | return "tmp/", "config.yml" | |
| 135 | + | return "/tmp/", "config.yml" | |
| 96 | 136 | } | |
| 97 | 137 | return "./", "config.yml" | |
| 98 | 138 | } | |
| @@ -4,8 +4,11 @@ import ( | |||||
| 4 | "database/sql" | 4 | "database/sql" | ||
| 5 | "fmt" | 5 | "fmt" | ||
| 6 | "io/fs" | 6 | "io/fs" | ||
| 7 | + | "log" | |||
| 7 | "os" | 8 | "os" | ||
| 8 | 9 | ||||
| 10 | + | "github.com/bnema/gordon/internal/gotemplate" | |||
| 11 | + | "github.com/bnema/gordon/internal/webui" | |||
| 9 | "github.com/bnema/gordon/pkg/utils/docker" | 12 | "github.com/bnema/gordon/pkg/utils/docker" | ||
| 10 | "github.com/bnema/gordon/pkg/utils/parser" | 13 | "github.com/bnema/gordon/pkg/utils/parser" | ||
| 11 | ) | 14 | ) | ||
| @@ -32,19 +35,30 @@ type App struct { | |||||
| 32 | DB *sql.DB | 35 | DB *sql.DB | ||
| 33 | Config AppConfig | 36 | Config AppConfig | ||
| 34 | } | 37 | } | ||
| 35 | - | ||||
| 36 | type AppConfig struct { | 38 | type AppConfig struct { | ||
| 37 | - | RunEnv string `yaml:"runEnv"` | 39 | + | General GeneralConfig `yaml:"general"` |
| 38 | - | BuildVersion string `yaml:"buildVersion"` | 40 | + | Http HttpConfig `yaml:"http"` |
| 39 | - | HttpPort int `yaml:"httpPort"` | 41 | + | Admin AdminConfig `yaml:"admin"` |
| 40 | - | TopDomain string `yaml:"topDomain"` | 42 | + | Docker DockerConfig `yaml:"docker"` |
| 41 | - | SubDomain string `yaml:"subDomain"` | 43 | + | } |
| 42 | - | AdminPath string `yaml:"adminPath"` | 44 | + | |
| 43 | - | DockerSock string `yaml:"dockerSock"` | 45 | + | type GeneralConfig struct { |
| 44 | - | PodmanEnable bool `yaml:"podmanEnable"` | 46 | + | RunEnv string `yaml:"runEnv"` |
| 45 | - | PodmanSock string `yaml:"podmanSock"` | 47 | + | BuildVersion string `yaml:"buildVersion"` |
| 46 | - | OauthCallbackURL string `yaml:"oauthCallbackURL"` | 48 | + | } |
| 47 | - | BuildDir string `yaml:"buildDir"` | 49 | + | |
| 50 | + | type HttpConfig struct { | |||
| 51 | + | Port int `yaml:"port"` | |||
| 52 | + | TopDomain string `yaml:"topDomain"` | |||
| 53 | + | SubDomain string `yaml:"subDomain"` | |||
| 54 | + | } | |||
| 55 | + | ||||
| 56 | + | type AdminConfig struct { | |||
| 57 | + | Path string `yaml:"path"` | |||
| 58 | + | } | |||
| 59 | + | ||||
| 60 | + | type DockerConfig struct { | |||
| 61 | + | Sock string `yaml:"sock"` | |||
| 48 | } | 62 | } | ||
| 49 | 63 | ||||
| 50 | func InitializeEnvironment() { | 64 | func InitializeEnvironment() { | ||
| @@ -53,6 +67,7 @@ func InitializeEnvironment() { | |||||
| 53 | fmt.Errorf("Error initializing environment: %s", err) | 67 | fmt.Errorf("Error initializing environment: %s", err) | ||
| 54 | } | 68 | } | ||
| 55 | OauthCallbackURL = GenerateOauthCallbackURL(config) | 69 | OauthCallbackURL = GenerateOauthCallbackURL(config) | ||
| 70 | + | ||||
| 56 | } | 71 | } | ||
| 57 | 72 | ||||
| 58 | func LoadConfig() (AppConfig, error) { | 73 | func LoadConfig() (AppConfig, error) { | ||
| @@ -62,6 +77,28 @@ func LoadConfig() (AppConfig, error) { | |||||
| 62 | return config, err | 77 | return config, err | ||
| 63 | } | 78 | } | ||
| 64 | 79 | ||||
| 80 | + | func NewApp() *App { | |||
| 81 | + | // Initialize AppConfig | |||
| 82 | + | config, err := LoadConfig() | |||
| 83 | + | if err != nil { | |||
| 84 | + | log.Fatalf("Failed to load configuration: %v", err) | |||
| 85 | + | } | |||
| 86 | + | ||||
| 87 | + | // Initialize App | |||
| 88 | + | a := &App{ | |||
| 89 | + | TemplateFS: gotemplate.TemplateFS, | |||
| 90 | + | PublicFS: webui.PublicFS, | |||
| 91 | + | DBDir: DBDir, | |||
| 92 | + | DBFilename: DBFilename, | |||
| 93 | + | Config: config, | |||
| 94 | + | } | |||
| 95 | + | ||||
| 96 | + | // If you have any other initializations like generating OAuth URL, do them here | |||
| 97 | + | OauthCallbackURL = GenerateOauthCallbackURL(config) | |||
| 98 | + | ||||
| 99 | + | return a | |||
| 100 | + | } | |||
| 101 | + | ||||
| 65 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | 102 | // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig. | ||
| 66 | func (config *AppConfig) NewDockerConfig() *docker.Config { | 103 | func (config *AppConfig) NewDockerConfig() *docker.Config { | ||
| 67 | return &docker.Config{ | 104 | return &docker.Config{ | ||
| @@ -91,8 +128,11 @@ func GenerateOauthCallbackURL(config AppConfig) string { | |||||
| 91 | } | 128 | } | ||
| 92 | 129 | ||||
| 93 | func getEnvPaths() (string, string) { | 130 | func getEnvPaths() (string, string) { | ||
| 131 | + | wd, _ := os.Getwd() | |||
| 132 | + | fmt.Println("Current Working Directory:", wd) | |||
| 133 | + | ||||
| 94 | if appEnv == "dev" { | 134 | if appEnv == "dev" { | ||
| 95 | - | return "tmp/", "config.yml" | 135 | + | return "/tmp/", "config.yml" |
| 96 | } | 136 | } | ||
| 97 | return "./", "config.yml" | 137 | return "./", "config.yml" | ||
| 98 | } | 138 | } | ||
M internal/gotemplate/render/html.go +2 -2
| @@ -17,7 +17,7 @@ type Renderer struct { | |||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | // Render function renders the template with the given data | |
| 20 | - | func (r *Renderer) Render(data interface{}, a *app.AppConfig) (string, error) { | |
| 20 | + | func (r *Renderer) Render(data interface{}, a *app.App) (string, error) { | |
| 21 | 21 | if r.ParseError != nil { | |
| 22 | 22 | return "", fmt.Errorf("failed to parse template: %w", r.ParseError) | |
| 23 | 23 | ||
| @@ -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.BuildVersion, | |
| 61 | + | BuildVersion: a.Config.BuildVersion, | |
| 62 | 62 | }, nil | |
| 63 | 63 | } | |
| @@ -17,7 +17,7 @@ type Renderer struct { | |||||
| 17 | } | 17 | } | ||
| 18 | 18 | ||||
| 19 | // Render function renders the template with the given data | 19 | // Render function renders the template with the given data | ||
| 20 | - | func (r *Renderer) Render(data interface{}, a *app.AppConfig) (string, error) { | 20 | + | func (r *Renderer) Render(data interface{}, a *app.App) (string, error) { |
| 21 | if r.ParseError != nil { | 21 | if r.ParseError != nil { | ||
| 22 | return "", fmt.Errorf("failed to parse template: %w", r.ParseError) | 22 | return "", fmt.Errorf("failed to parse template: %w", r.ParseError) | ||
| 23 | 23 | ||||
| @@ -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.BuildVersion, | 61 | + | BuildVersion: a.Config.BuildVersion, |
| 62 | }, nil | 62 | }, nil | ||
| 63 | } | 63 | } | ||
M internal/httpserve/handler/oauth.go +1 -1
| @@ -48,7 +48,7 @@ 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.OauthCallbackURL | |
| 51 | + | redirectDomain := a.Config.OauthCallbackURL | |
| 52 | 52 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | |
| 53 | 53 | fmt.Print("redirectDomain:", redirectDomain) | |
| 54 | 54 | fmt.Print("encoded state :", encodedState) | |
| @@ -48,7 +48,7 @@ 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.OauthCallbackURL | 51 | + | redirectDomain := a.Config.OauthCallbackURL |
| 52 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | 52 | encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain)) | ||
| 53 | fmt.Print("redirectDomain:", redirectDomain) | 53 | fmt.Print("redirectDomain:", redirectDomain) | ||
| 54 | fmt.Print("encoded state :", encodedState) | 54 | 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.AdminPath | |
| 17 | + | AdminPath := a.Config.AdminPath | |
| 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.AdminPath | 17 | + | AdminPath := a.Config.AdminPath |
| 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/parser/yaml.go +1 -0
| @@ -36,6 +36,7 @@ import ( | |||
| 36 | 36 | func OpenYamlFile(fs fs.FS, filename string, out interface{}, dir ...string) error { | |
| 37 | 37 | // Construct the full path of the YAML file | |
| 38 | 38 | fullPath := filename | |
| 39 | + | fmt.Println("Trying to open file at:", fullPath) | |
| 39 | 40 | if len(dir) > 0 { | |
| 40 | 41 | fullPath = filepath.Join(dir[0], filename) | |
| 41 | 42 | } | |
| @@ -36,6 +36,7 @@ import ( | |||||
| 36 | func OpenYamlFile(fs fs.FS, filename string, out interface{}, dir ...string) error { | 36 | func OpenYamlFile(fs fs.FS, filename string, out interface{}, dir ...string) error { | ||
| 37 | // Construct the full path of the YAML file | 37 | // Construct the full path of the YAML file | ||
| 38 | fullPath := filename | 38 | fullPath := filename | ||
| 39 | + | fmt.Println("Trying to open file at:", fullPath) | |||
| 39 | if len(dir) > 0 { | 40 | if len(dir) > 0 { | ||
| 40 | fullPath = filepath.Join(dir[0], filename) | 41 | fullPath = filepath.Join(dir[0], filename) | ||
| 41 | } | 42 | } | ||