detached 4e979c5

Fix Goreleaser Build Bugs and Refactors

-Add 'dist/' to .gitignore
-Refactor build version extraction in main.go
-Move BuildVersion and RunEnv to BuildConfig
-Remove debug print in oauth.go
-Update session security settings based on HTTPS config
-Modify scheme and domain logic in appserver.go
-Update BuildVersion in html renderer
commit
4e979c516ba4cfd09cc2269311d6a9d664966f48
author
bnema · · 2023-10-30 09:21 UTC
parent
31c4687
signature
unsigned
8 files · +85 -26
M .gitignore +2 -1
@@ -7,4 +7,5 @@ bun.lockb
77 *.log
88 *.txt
99 *sync-conflict*
10-main⏎̸
10+main
11+dist/
@@ -7,4 +7,5 @@ bun.lockb
7 *.log7 *.log
8 *.txt8 *.txt
9 *sync-conflict*9 *sync-conflict*
10-main⏎̸10+main
11+dist/
A .goreleaser.yaml +36 -0
@@ -0,0 +1,36 @@
1+before:
2+ hooks:
3+ # You may remove this if you don't use go modules.
4+ - go mod tidy
5+
6+builds:
7+ - id: "gordon"
8+ main: ./cmd/cli/main.go
9+ ldflags:
10+ - -s -w -X main.build={{.Version}}
11+ goos:
12+ - linux
13+ - windows
14+ - darwin
15+
16+archives:
17+ - format: tar.gz
18+ # this name template makes the OS and Arch compatible with the results of `uname`.
19+ name_template: >-
20+ {{ .ProjectName }}_
21+ {{- title .Os }}_
22+ {{- if eq .Arch "amd64" }}x86_64
23+ {{- else if eq .Arch "386" }}i386
24+ {{- else }}{{ .Arch }}{{ end }}
25+ {{- if .Arm }}v{{ .Arm }}{{ end }}
26+ # use zip for windows archives
27+ format_overrides:
28+ - goos: windows
29+ format: zip
30+
31+changelog:
32+ sort: asc
33+ filters:
34+ exclude:
35+ - "^docs:"
36+ - "^test:"
@@ -0,0 +1,36 @@
1+before:
2+ hooks:
3+ # You may remove this if you don't use go modules.
4+ - go mod tidy
5+
6+builds:
7+ - id: "gordon"
8+ main: ./cmd/cli/main.go
9+ ldflags:
10+ - -s -w -X main.build={{.Version}}
11+ goos:
12+ - linux
13+ - windows
14+ - darwin
15+
16+archives:
17+ - format: tar.gz
18+ # this name template makes the OS and Arch compatible with the results of `uname`.
19+ name_template: >-
20+ {{ .ProjectName }}_
21+ {{- title .Os }}_
22+ {{- if eq .Arch "amd64" }}x86_64
23+ {{- else if eq .Arch "386" }}i386
24+ {{- else }}{{ .Arch }}{{ end }}
25+ {{- if .Arm }}v{{ .Arm }}{{ end }}
26+ # use zip for windows archives
27+ format_overrides:
28+ - goos: windows
29+ format: zip
30+
31+changelog:
32+ sort: asc
33+ filters:
34+ exclude:
35+ - "^docs:"
36+ - "^test:"
M cmd/cli/main.go +9 -0
@@ -2,6 +2,7 @@ package main
22
33 import (
44 "fmt"
5+ "regexp"
56
67 "github.com/bnema/gordon/internal/cli"
78 "github.com/bnema/gordon/internal/cli/cmd"
@@ -10,6 +11,8 @@ import (
1011 "github.com/spf13/cobra"
1112 )
1213
14+var build string
15+
1316 var rootCmd = &cobra.Command{Use: "gordon"}
1417
1518 func InitializeCommands(a *cli.App, s *server.App) {
@@ -35,5 +38,11 @@ func main() {
3538
3639 common.DockerInit(&s.Config.ContainerEngine)
3740
41+ // build looks like this: "0.0.901-b98a337"
42+ //use regex to only get the version number
43+ build = regexp.MustCompile(`\d+\.\d+\.\d+`).FindString(build)
44+ // Set the BuildVersion
45+ s.Config.Build.BuildVersion = build
46+
3847 Execute(a, s)
3948 }
@@ -2,6 +2,7 @@ package main
2 2
3 import (3 import (
4 "fmt"4 "fmt"
5+ "regexp"
5 6
6 "github.com/bnema/gordon/internal/cli"7 "github.com/bnema/gordon/internal/cli"
7 "github.com/bnema/gordon/internal/cli/cmd"8 "github.com/bnema/gordon/internal/cli/cmd"
@@ -10,6 +11,8 @@ import (
10 "github.com/spf13/cobra"11 "github.com/spf13/cobra"
11 )12 )
12 13
14+var build string
15+
13 var rootCmd = &cobra.Command{Use: "gordon"}16 var rootCmd = &cobra.Command{Use: "gordon"}
14 17
15 func InitializeCommands(a *cli.App, s *server.App) {18 func InitializeCommands(a *cli.App, s *server.App) {
@@ -35,5 +38,11 @@ func main() {
35 38
36 common.DockerInit(&s.Config.ContainerEngine)39 common.DockerInit(&s.Config.ContainerEngine)
37 40
41+ // build looks like this: "0.0.901-b98a337"
42+ //use regex to only get the version number
43+ build = regexp.MustCompile(`\d+\.\d+\.\d+`).FindString(build)
44+ // Set the BuildVersion
45+ s.Config.Build.BuildVersion = build
46+
38 Execute(a, s)47 Execute(a, s)
39 }48 }
M internal/common/initconfig.go +24 -12
@@ -17,6 +17,12 @@ type Config struct {
1717 Http HttpConfig `yaml:"Http"`
1818 Admin AdminConfig `yaml:"Admin"`
1919 ContainerEngine ContainerEngineConfig `yaml:"ContainerEngine"`
20+ Build BuildConfig `yaml:"-"`
21+}
22+
23+type BuildConfig struct {
24+ RunEnv string `yaml:"-"` // come from env
25+ BuildVersion string `yaml:"-"` // come from build ldflags
2026 }
2127
2228 type AdminConfig struct {
@@ -24,16 +30,15 @@ type AdminConfig struct {
2430 }
2531
2632 type GeneralConfig struct {
27- RunEnv string // come from env
28- BuildVersion string // come from env
29- StorageDir string `yaml:"storageDir"`
30- Token string `yaml:"token"`
33+ StorageDir string `yaml:"storageDir"`
34+ Token string `yaml:"token"`
3135 }
3236 type HttpConfig struct {
3337 Port string `yaml:"port"`
34- TopDomain string `yaml:"topDomain"`
38+ Domain string `yaml:"domain"`
3539 SubDomain string `yaml:"subDomain"`
3640 BackendURL string `yaml:"backendURL"`
41+ Https bool `yaml:"https"`
3742 }
3843
3944 type ContainerEngineConfig struct {
@@ -124,14 +129,11 @@ func (config *Config) LoadConfig() (*Config, error) {
124129 return nil, err
125130 }
126131
127- // Load env elements
128- config.General.BuildVersion = os.Getenv("BUILD_VERSION")
129- config.General.RunEnv = os.Getenv("RUN_ENV")
132+ // Load run env
133+ config.Build.RunEnv = os.Getenv("RUN_ENV")
130134
131- // if RUN_ENV is not set, assume "prod" and config dir is the current dir
132- if config.General.RunEnv == "" {
133- config.General.RunEnv = "prod"
134- configDir = "."
135+ if config.Build.RunEnv == "" {
136+ config.Build.RunEnv = "prod"
135137 }
136138
137139 return config, nil
@@ -162,3 +164,13 @@ func (config *Config) GetToken() (string, error) {
162164 return token, nil
163165
164166 }
167+
168+// GetBuildVersion returns the build version
169+func (config *BuildConfig) GetBuildVersion() string {
170+ return config.BuildVersion
171+}
172+
173+// GetRunEnv returns the run environment
174+func (config *BuildConfig) GetRunEnv() string {
175+ return config.RunEnv
176+}
@@ -17,6 +17,12 @@ type Config struct {
17 Http HttpConfig `yaml:"Http"`17 Http HttpConfig `yaml:"Http"`
18 Admin AdminConfig `yaml:"Admin"`18 Admin AdminConfig `yaml:"Admin"`
19 ContainerEngine ContainerEngineConfig `yaml:"ContainerEngine"`19 ContainerEngine ContainerEngineConfig `yaml:"ContainerEngine"`
20+ Build BuildConfig `yaml:"-"`
21+}
22+
23+type BuildConfig struct {
24+ RunEnv string `yaml:"-"` // come from env
25+ BuildVersion string `yaml:"-"` // come from build ldflags
20 }26 }
21 27
22 type AdminConfig struct {28 type AdminConfig struct {
@@ -24,16 +30,15 @@ type AdminConfig struct {
24 }30 }
25 31
26 type GeneralConfig struct {32 type GeneralConfig struct {
27- RunEnv string // come from env33+ StorageDir string `yaml:"storageDir"`
28- BuildVersion string // come from env34+ Token string `yaml:"token"`
29- StorageDir string `yaml:"storageDir"`
30- Token string `yaml:"token"`
31 }35 }
32 type HttpConfig struct {36 type HttpConfig struct {
33 Port string `yaml:"port"`37 Port string `yaml:"port"`
34- TopDomain string `yaml:"topDomain"`38+ Domain string `yaml:"domain"`
35 SubDomain string `yaml:"subDomain"`39 SubDomain string `yaml:"subDomain"`
36 BackendURL string `yaml:"backendURL"`40 BackendURL string `yaml:"backendURL"`
41+ Https bool `yaml:"https"`
37 }42 }
38 43
39 type ContainerEngineConfig struct {44 type ContainerEngineConfig struct {
@@ -124,14 +129,11 @@ func (config *Config) LoadConfig() (*Config, error) {
124 return nil, err129 return nil, err
125 }130 }
126 131
127- // Load env elements132+ // Load run env
128- config.General.BuildVersion = os.Getenv("BUILD_VERSION")133+ config.Build.RunEnv = os.Getenv("RUN_ENV")
129- config.General.RunEnv = os.Getenv("RUN_ENV")
130 134
131- // if RUN_ENV is not set, assume "prod" and config dir is the current dir135+ if config.Build.RunEnv == "" {
132- if config.General.RunEnv == "" {136+ config.Build.RunEnv = "prod"
133- config.General.RunEnv = "prod"
134- configDir = "."
135 }137 }
136 138
137 return config, nil139 return config, nil
@@ -162,3 +164,13 @@ func (config *Config) GetToken() (string, error) {
162 return token, nil164 return token, nil
163 165
164 }166 }
167+
168+// GetBuildVersion returns the build version
169+func (config *BuildConfig) GetBuildVersion() string {
170+ return config.BuildVersion
171+}
172+
173+// GetRunEnv returns the run environment
174+func (config *BuildConfig) GetRunEnv() string {
175+ return config.RunEnv
176+}
M internal/httpserve/handler/oauth.go +0 -2
@@ -219,8 +219,6 @@ func setSessionValues(c echo.Context, sess *sessions.Session, user *db.User, acc
219219 sess.Values["expires"] = expires
220220 sess.Values["sessionID"] = sessionID
221221
222- fmt.Print(sess)
223-
224222 if err := sess.Save(c.Request(), c.Response()); err != nil {
225223 return fmt.Errorf("could not save session: %w", err)
226224 }
@@ -219,8 +219,6 @@ func setSessionValues(c echo.Context, sess *sessions.Session, user *db.User, acc
219 sess.Values["expires"] = expires219 sess.Values["expires"] = expires
220 sess.Values["sessionID"] = sessionID220 sess.Values["sessionID"] = sessionID
221 221
222- fmt.Print(sess)
223-
224 if err := sess.Save(c.Request(), c.Response()); err != nil {222 if err := sess.Save(c.Request(), c.Response()); err != nil {
225 return fmt.Errorf("could not save session: %w", err)223 return fmt.Errorf("could not save session: %w", err)
226 }224 }
M internal/httpserve/middleware/secure.go +2 -2
@@ -25,7 +25,7 @@ func SecureRoutes() echo.MiddlewareFunc {
2525
2626 // InitSessionMiddleware initializes the session middleware with secure options
2727 func InitSessionMiddleware(a *server.App) echo.MiddlewareFunc {
28- isDev := a.IsDevEnvironment()
28+ isHttps := a.Config.Http.Https
2929 secret := os.Getenv("SESSION_SECRET")
3030 if secret == "" {
3131 log.Fatal("Environment variable SESSION_SECRET is not set or cannot be read")
@@ -34,7 +34,7 @@ func InitSessionMiddleware(a *server.App) echo.MiddlewareFunc {
3434 store.Options = &sessions.Options{
3535 Path: "/",
3636 HttpOnly: true,
37- Secure: !isDev, // Set to true in production
37+ Secure: isHttps,
3838 MaxAge: 86400,
3939 }
4040 return func(next echo.HandlerFunc) echo.HandlerFunc {
@@ -25,7 +25,7 @@ func SecureRoutes() echo.MiddlewareFunc {
25 25
26 // InitSessionMiddleware initializes the session middleware with secure options26 // InitSessionMiddleware initializes the session middleware with secure options
27 func InitSessionMiddleware(a *server.App) echo.MiddlewareFunc {27 func InitSessionMiddleware(a *server.App) echo.MiddlewareFunc {
28- isDev := a.IsDevEnvironment()28+ isHttps := a.Config.Http.Https
29 secret := os.Getenv("SESSION_SECRET")29 secret := os.Getenv("SESSION_SECRET")
30 if secret == "" {30 if secret == "" {
31 log.Fatal("Environment variable SESSION_SECRET is not set or cannot be read")31 log.Fatal("Environment variable SESSION_SECRET is not set or cannot be read")
@@ -34,7 +34,7 @@ func InitSessionMiddleware(a *server.App) echo.MiddlewareFunc {
34 store.Options = &sessions.Options{34 store.Options = &sessions.Options{
35 Path: "/",35 Path: "/",
36 HttpOnly: true,36 HttpOnly: true,
37- Secure: !isDev, // Set to true in production37+ Secure: isHttps,
38 MaxAge: 86400,38 MaxAge: 86400,
39 }39 }
40 return func(next echo.HandlerFunc) echo.HandlerFunc {40 return func(next echo.HandlerFunc) echo.HandlerFunc {
M internal/server/appserver.go +11 -8
@@ -36,24 +36,27 @@ type DBTables struct {
3636 func (a *App) GenerateOauthCallbackURL() string {
3737 var scheme, port string
3838 config := a.Config
39- if config.General.RunEnv == "dev" {
40- scheme = "http"
41- port = ":" + config.Http.Port
42- } else { // Assuming "prod"
39+
40+ if config.Http.Https {
4341 scheme = "https"
4442 port = ""
4543 }
4644
47- domain := config.Http.TopDomain
45+ if !config.Http.Https {
46+ scheme = "http"
47+ port = fmt.Sprintf(":%s", a.Config.Http.Port)
48+ }
49+
50+ domain := config.Http.Domain
4851 if config.Http.SubDomain != "" {
49- domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.TopDomain)
52+ domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.Domain)
5053 }
5154
5255 return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.Admin.Path)
5356 }
5457
5558 func (a *App) IsDevEnvironment() bool {
56- return a.Config.General.RunEnv == "dev"
59+ return a.Config.Build.RunEnv == "dev"
5760 }
5861
5962 func (a *App) GetUptime() string {
@@ -63,5 +66,5 @@ func (a *App) GetUptime() string {
6366
6467 // GetVersion returns the version of the app
6568 func (a *App) GetVersionstring() string {
66- return fmt.Sprint(a.Config.General.BuildVersion)
69+ return fmt.Sprint(a.Config.Build.BuildVersion)
6770 }
@@ -36,24 +36,27 @@ type DBTables struct {
36 func (a *App) GenerateOauthCallbackURL() string {36 func (a *App) GenerateOauthCallbackURL() string {
37 var scheme, port string37 var scheme, port string
38 config := a.Config38 config := a.Config
39- if config.General.RunEnv == "dev" {39+
40- scheme = "http"40+ if config.Http.Https {
41- port = ":" + config.Http.Port
42- } else { // Assuming "prod"
43 scheme = "https"41 scheme = "https"
44 port = ""42 port = ""
45 }43 }
46 44
47- domain := config.Http.TopDomain45+ if !config.Http.Https {
46+ scheme = "http"
47+ port = fmt.Sprintf(":%s", a.Config.Http.Port)
48+ }
49+
50+ domain := config.Http.Domain
48 if config.Http.SubDomain != "" {51 if config.Http.SubDomain != "" {
49- domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.TopDomain)52+ domain = fmt.Sprintf("%s.%s", config.Http.SubDomain, config.Http.Domain)
50 }53 }
51 54
52 return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.Admin.Path)55 return fmt.Sprintf("%s://%s%s%s/login/oauth/callback", scheme, domain, port, config.Admin.Path)
53 }56 }
54 57
55 func (a *App) IsDevEnvironment() bool {58 func (a *App) IsDevEnvironment() bool {
56- return a.Config.General.RunEnv == "dev"59+ return a.Config.Build.RunEnv == "dev"
57 }60 }
58 61
59 func (a *App) GetUptime() string {62 func (a *App) GetUptime() string {
@@ -63,5 +66,5 @@ func (a *App) GetUptime() string {
63 66
64 // GetVersion returns the version of the app67 // GetVersion returns the version of the app
65 func (a *App) GetVersionstring() string {68 func (a *App) GetVersionstring() string {
66- return fmt.Sprint(a.Config.General.BuildVersion)69+ return fmt.Sprint(a.Config.Build.BuildVersion)
67 }70 }
M internal/templating/render/html.go +1 -1
@@ -71,6 +71,6 @@ func GetHTMLRenderer(mainPath string, filename string, fs fs.FS, a *server.App,
7171 // Return the Renderer instance
7272 return &Renderer{
7373 Template: tmpl,
74- BuildVersion: a.Config.General.BuildVersion,
74+ BuildVersion: a.Config.Build.BuildVersion,
7575 }, nil
7676 }
@@ -71,6 +71,6 @@ func GetHTMLRenderer(mainPath string, filename string, fs fs.FS, a *server.App,
71 // Return the Renderer instance71 // Return the Renderer instance
72 return &Renderer{72 return &Renderer{
73 Template: tmpl,73 Template: tmpl,
74- BuildVersion: a.Config.General.BuildVersion,74+ BuildVersion: a.Config.Build.BuildVersion,
75 }, nil75 }, nil
76 }76 }

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