Skip to content

Commit cfa3bed

Browse files
Integrate Configuration struct for settings
Consolidated application settings into the `Configuration` struct in `core.go` and removed the redundant `Config` struct and global variables in `settings.go`. Updated `config.go` to load settings into `Configuration`. Refactored the codebase to use the global `AppConfig` instance instead of scattered global variables. Updated `cmd/gobookmarks` to initialize and populate `AppConfig` correctly. Updated tests to use `AppConfig`.
1 parent 4d4eb4f commit cfa3bed

28 files changed

+140
-183
lines changed

authHandlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func LoginWithProvider(w http.ResponseWriter, r *http.Request) error {
9595
http.NotFound(w, r)
9696
return nil
9797
}
98-
cfg := p.Config(creds.ID, creds.Secret, OauthRedirectURL)
98+
cfg := p.Config(creds.ID, creds.Secret, AppConfig.OauthRedirectURL)
9999
if cfg == nil {
100100
http.NotFound(w, r)
101101
return nil
@@ -129,7 +129,7 @@ func Oauth2CallbackPage(w http.ResponseWriter, r *http.Request) error {
129129
if creds == nil {
130130
return fmt.Errorf("provider does not support login")
131131
}
132-
cfg := p.Config(creds.ID, creds.Secret, OauthRedirectURL)
132+
cfg := p.Config(creds.ID, creds.Secret, AppConfig.OauthRedirectURL)
133133
if cfg == nil {
134134
return fmt.Errorf("provider does not support login")
135135
}

category_edit_http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func setupCategoryEditTest(t *testing.T) (GitProvider, string, *sessions.Session, context.Context) {
1515
tmp := t.TempDir()
16-
LocalGitPath = tmp
16+
AppConfig.LocalGitPath = tmp
1717
p := GitProvider{}
1818
user := "alice"
1919
if err := p.CreateRepo(context.Background(), user, nil, RepoName); err != nil {

cmd/gobookmarks/db_reset_password_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (c *DbResetPasswordCommand) Execute(args []string) error {
6767
return err
6868
}
6969

70-
DBConnectionProvider = cfg.DBConnectionProvider
71-
DBConnectionString = cfg.DBConnectionString
70+
AppConfig.DBConnectionProvider = cfg.DBConnectionProvider
71+
AppConfig.DBConnectionString = cfg.DBConnectionString
7272

7373
p := SQLProvider{}
7474
if err := p.SetPassword(context.Background(), c.User, c.Password); err != nil {

cmd/gobookmarks/db_users_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func (c *DbUsersCommand) Execute(args []string) error {
5050
return err
5151
}
5252

53-
DBConnectionProvider = cfg.DBConnectionProvider
54-
DBConnectionString = cfg.DBConnectionString
53+
AppConfig.DBConnectionProvider = cfg.DBConnectionProvider
54+
AppConfig.DBConnectionString = cfg.DBConnectionString
5555

5656
db, err := OpenDB()
5757
if err != nil {

cmd/gobookmarks/export_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *ExportCommand) Execute(args []string) error {
5555
return err
5656
}
5757

58-
provider, err := getConfiguredProvider(&c.parent.(*RootCommand).cfg)
58+
provider, err := getConfiguredProvider(c.parent.(*RootCommand).cfg)
5959
if err != nil {
6060
printHelp(c, err)
6161
return err

cmd/gobookmarks/import_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *ImportCommand) Execute(args []string) error {
5555
return err
5656
}
5757

58-
provider, err := getConfiguredProvider(&c.parent.(*RootCommand).cfg)
58+
provider, err := getConfiguredProvider(c.parent.(*RootCommand).cfg)
5959
if err != nil {
6060
printHelp(c, err)
6161
return err

cmd/gobookmarks/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type VersionInfo struct {
3333
type RootCommand struct {
3434
Flags *flag.FlagSet
3535
ConfigPath string
36-
cfg Config
36+
cfg *Configuration
3737
VersionInfo VersionInfo
3838

3939
ServeCmd *ServeCommand
@@ -142,15 +142,14 @@ func (c *RootCommand) loadConfig() error {
142142
log.Printf("unable to load env file %s: %v", envPath, err)
143143
}
144144

145-
c.cfg = Config{
146-
GithubClientID: os.Getenv("GITHUB_CLIENT_ID"),
147-
GithubSecret: os.Getenv("GITHUB_SECRET"),
148-
GitlabClientID: os.Getenv("GITLAB_CLIENT_ID"),
149-
GitlabSecret: os.Getenv("GITLAB_SECRET"),
150-
ExternalURL: os.Getenv("EXTERNAL_URL"),
151-
DBConnectionProvider: os.Getenv("DB_CONNECTION_PROVIDER"),
152-
DBConnectionString: os.Getenv("DB_CONNECTION_STRING"),
153-
}
145+
c.cfg = NewConfiguration()
146+
c.cfg.GithubClientID = os.Getenv("GITHUB_CLIENT_ID")
147+
c.cfg.GithubSecret = os.Getenv("GITHUB_SECRET")
148+
c.cfg.GitlabClientID = os.Getenv("GITLAB_CLIENT_ID")
149+
c.cfg.GitlabSecret = os.Getenv("GITLAB_SECRET")
150+
c.cfg.ExternalURL = os.Getenv("EXTERNAL_URL")
151+
c.cfg.DBConnectionProvider = os.Getenv("DB_CONNECTION_PROVIDER")
152+
c.cfg.DBConnectionString = os.Getenv("DB_CONNECTION_STRING")
154153

155154
configPath := DefaultConfigPath()
156155
if envCfg := os.Getenv("GOBM_CONFIG_FILE"); envCfg != "" {
@@ -166,7 +165,7 @@ func (c *RootCommand) loadConfig() error {
166165
return fmt.Errorf("unable to load config file %s: %w", configPath, err)
167166
}
168167
if found {
169-
MergeConfig(&c.cfg, fileCfg)
168+
MergeConfig(c.cfg, fileCfg)
170169
} else if cfgSpecified {
171170
return fmt.Errorf("unable to load config file %s: not found", configPath)
172171
}

cmd/gobookmarks/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestRunHandlerChain_UserErrorRedirect(t *testing.T) {
4141
func TestRunTemplate_BufferedError(t *testing.T) {
4242
gb.SessionName = "testsess"
4343
gb.SessionStore = sessions.NewCookieStore([]byte("secret"))
44-
gb.DBConnectionProvider = ""
44+
gb.AppConfig.DBConnectionProvider = ""
4545

4646
req := httptest.NewRequest("GET", "/", nil)
4747
sess, _ := gb.SessionStore.New(req, gb.SessionName)

cmd/gobookmarks/provider_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
. "github.com/arran4/gobookmarks"
77
)
88

9-
func getConfiguredProvider(cfg *Config) (Provider, error) {
9+
func getConfiguredProvider(cfg *Configuration) (Provider, error) {
1010
if cfg.DBConnectionProvider != "" && cfg.DBConnectionString != "" {
1111
return SQLProvider{}, nil
1212
}

cmd/gobookmarks/serve.go

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -189,60 +189,28 @@ func (c *ServeCommand) Execute(args []string) error {
189189
return nil
190190
}
191191

192-
UseCssColumns = cfg.CssColumns
193-
Namespace = cfg.Namespace
192+
*AppConfig = *cfg
194193
RepoName = GetBookmarksRepoName()
195-
SiteTitle = cfg.Title
196-
NoFooter = cfg.NoFooter
197-
DevMode = version == "dev"
198-
if cfg.DevMode != nil {
199-
DevMode = *cfg.DevMode
200-
}
201194

202-
if cfg.GithubServer != "" {
203-
GithubServer = cfg.GithubServer
204-
}
205-
if cfg.GitlabServer != "" {
206-
GitlabServer = cfg.GitlabServer
207-
}
208-
if cfg.FaviconCacheDir != "" {
209-
FaviconCacheDir = cfg.FaviconCacheDir
210-
}
211-
if cfg.FaviconCacheSize != 0 {
212-
FaviconCacheSize = cfg.FaviconCacheSize
213-
} else {
214-
FaviconCacheSize = DefaultFaviconCacheSize
195+
if AppConfig.DevMode == nil {
196+
isDev := version == "dev"
197+
AppConfig.DevMode = &isDev
215198
}
216-
if cfg.CommitsPerPage != 0 {
217-
CommitsPerPage = cfg.CommitsPerPage
218-
} else {
219-
CommitsPerPage = DefaultCommitsPerPage
220-
}
221-
if cfg.LocalGitPath != "" {
222-
LocalGitPath = cfg.LocalGitPath
223-
}
224-
if cfg.DBConnectionProvider != "" {
225-
DBConnectionProvider = cfg.DBConnectionProvider
199+
200+
if AppConfig.FaviconCacheSize == 0 {
201+
AppConfig.FaviconCacheSize = DefaultFaviconCacheSize
226202
}
227-
if cfg.DBConnectionString != "" {
228-
DBConnectionString = cfg.DBConnectionString
203+
if AppConfig.CommitsPerPage == 0 {
204+
AppConfig.CommitsPerPage = DefaultCommitsPerPage
229205
}
230-
githubID := cfg.GithubClientID
231-
githubSecret := cfg.GithubSecret
232-
gitlabID := cfg.GitlabClientID
233-
gitlabSecret := cfg.GitlabSecret
234-
externalUrl := strings.TrimRight(cfg.ExternalURL, "/")
235-
redirectUrl := JoinURL(externalUrl, "oauth2Callback")
236-
GithubClientID = githubID
237-
GithubClientSecret = githubSecret
238-
GitlabClientID = gitlabID
239-
GitlabClientSecret = gitlabSecret
240-
OauthRedirectURL = redirectUrl
241206

242-
SetProviderOrder(cfg.ProviderOrder)
207+
externalUrl := strings.TrimRight(AppConfig.ExternalURL, "/")
208+
AppConfig.OauthRedirectURL = JoinURL(externalUrl, "oauth2Callback")
209+
210+
SetProviderOrder(AppConfig.ProviderOrder)
243211

244212
SessionName = "gobookmarks"
245-
SessionStore = sessions.NewCookieStore(loadSessionKey(cfg))
213+
SessionStore = sessions.NewCookieStore(loadSessionKey(*AppConfig))
246214
if len(ProviderNames()) == 0 {
247215
return errors.New("no providers compiled")
248216
}
@@ -263,7 +231,7 @@ func (c *ServeCommand) Execute(args []string) error {
263231
}).Methods("GET")
264232

265233
// Development helpers to toggle layout mode
266-
if DevMode {
234+
if *AppConfig.DevMode {
267235
r.HandleFunc("/_css", runHandlerChain(EnableCssColumnsAction, redirectToHandler("/"))).Methods("GET")
268236
r.HandleFunc("/_table", runHandlerChain(DisableCssColumnsAction, redirectToHandler("/"))).Methods("GET")
269237
}
@@ -357,7 +325,7 @@ func (c *ServeCommand) Execute(args []string) error {
357325
log.Printf("gobookmarks: %s, commit %s, built at %s", version, commit, date)
358326
SetVersion(version, commit, date)
359327
RepoName = GetBookmarksRepoName()
360-
log.Printf("Redirect URL configured to: %s", redirectUrl)
328+
log.Printf("Redirect URL configured to: %s", AppConfig.OauthRedirectURL)
361329
log.Println("Server started on http://localhost:8080")
362330
log.Println("Server started on https://localhost:8443")
363331

@@ -730,7 +698,7 @@ func fileExists(filename string) bool {
730698
return !os.IsNotExist(err)
731699
}
732700

733-
func loadSessionKey(cfg Config) []byte {
701+
func loadSessionKey(cfg Configuration) []byte {
734702
if cfg.SessionKey != "" {
735703
return []byte(cfg.SessionKey)
736704
}

0 commit comments

Comments
 (0)