detached 0ba7279

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() {
4848 e.HidePort = true
4949 e = httpserve.RegisterRoutes(e, a)
5050
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 {
5353 log.Fatal("Server error:", err)
5454 }
5555 }
@@ -48,8 +48,8 @@ func main() {
48 e.HidePort = true48 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 {
3636 Config AppConfig
3737 }
3838 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"`
4343 }
4444
4545 type GeneralConfig struct {
@@ -57,8 +57,9 @@ type AdminConfig struct {
5757 Path string `yaml:"path"`
5858 }
5959
60-type DockerConfig struct {
61- Sock string `yaml:"sock"`
60+type ContainerEngineConfig struct {
61+ Sock string `yaml:"sock"`
62+ PodmanEnable bool `yaml:"podman"`
6263 }
6364
6465 func InitializeEnvironment() {
@@ -72,8 +73,9 @@ func InitializeEnvironment() {
7273
7374 func LoadConfig() (AppConfig, error) {
7475 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
7779 return config, err
7880 }
7981
@@ -102,37 +104,29 @@ func NewApp() *App {
102104 // NewDockerConfig creates and returns a new Docker client configuration based on AppConfig.
103105 func (config *AppConfig) NewDockerConfig() *docker.Config {
104106 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}
109109 }
110110
111111 func GenerateOauthCallbackURL(config AppConfig) string {
112112 var scheme, port string
113113
114- if config.RunEnv == "dev" {
114+ if config.General.RunEnv == "dev" {
115115 scheme = "http"
116- port = fmt.Sprintf(":%d", config.HttpPort)
116+ port = fmt.Sprintf(":%d", config.Http.Port)
117117 } else { // Assuming "prod"
118118 scheme = "https"
119119 port = "" // Assuming that HTTPS will run on the default port 443
120120 }
121121
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)
125125 }
126126
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)
128128 }
129129
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
138132 }
@@ -36,10 +36,10 @@ type App struct {
36 Config AppConfig36 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 AppConfig75 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 filesystem77+ 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, err79 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 string112 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 443119 port = "" // Assuming that HTTPS will run on the default port 443
120 }120 }
121 121
122- domain := config.TopDomain122+ 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
5858 }
5959 return &Renderer{
6060 Template: tmpl,
61- BuildVersion: a.Config.BuildVersion,
61+ BuildVersion: a.Config.General.BuildVersion,
6262 }, nil
6363 }
@@ -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 }, nil62 }, nil
63 }63 }
M internal/httpserve/handler/oauth.go +2 -1
@@ -48,7 +48,8 @@ func RenderLoginPage(c echo.Context, a *app.App) error {
4848
4949 func StartOAuthGithub(c echo.Context, a *app.App) error {
5050 clientID := os.Getenv("GITHUB_APP_ID")
51- redirectDomain := a.Config.OauthCallbackURL
51+ redirectDomain := app.GenerateOauthCallbackURL(a.Config)
52+ fmt.Print(redirectDomain)
5253 encodedState := base64.StdEncoding.EncodeToString([]byte("redirectDomain:" + redirectDomain))
5354 fmt.Print("redirectDomain:", redirectDomain)
5455 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.OauthCallbackURL51+ 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 (
1414
1515 // RegisterRoutes registers all routes for the application
1616 func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo {
17- AdminPath := a.Config.AdminPath
17+ AdminPath := a.Config.Admin.Path
1818 e.Use(middleware.SetCommonDataMiddleware(a))
1919 e.Use(middleware.ErrorHandler)
2020 // Add session middleware
@@ -14,7 +14,7 @@ import (
14 14
15 // RegisterRoutes registers all routes for the application15 // 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.AdminPath17+ 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 middleware20 // Add session middleware
M pkg/utils/docker/initclient.go +27 -16
@@ -1,31 +1,42 @@
11 package docker
22
33 import (
4- "fmt"
54 "log"
6-
7- "github.com/docker/docker/client"
85 )
96
7+type ContainerEngineClient interface {
8+ InitializeClient(*Config) error
9+ // Add other methods that both Docker and Podman should implement
10+}
1011 type Config struct {
11- DockerSock string
12+ Sock string
1213 PodmanEnable bool
13- PodmanSock string
1414 }
1515
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{}
1728
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) {
2134 if config.PodmanEnable {
22- socketPath = config.PodmanSock
35+ engineClient = &PodmanClient{}
36+ } else {
37+ engineClient = &DockerClient{}
2338 }
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)
3041 }
3142 }
@@ -1,31 +1,42 @@
1 package docker1 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 string12+ Sock string
12 PodmanEnable bool13 PodmanEnable bool
13- PodmanSock string
14 }14 }
15 15
16-var dockerClient *client.Client16+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 Podman30+ // Initialize Podman client here
20- socketPath := config.DockerSock31+ 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.PodmanSock35+ engineClient = &PodmanClient{}
36+ } else {
37+ engineClient = &DockerClient{}
23 }38 }
24-39+ if err := engineClient.InitializeClient(config); err != nil {
25- // Initialize Docker client40+ 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 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close