From 578e4b173e0a141d5db894852476caa5c2adeed1 Mon Sep 17 00:00:00 2001 From: httpjamesm <51917118+httpjamesm@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:03:55 -0700 Subject: [PATCH] Move all theme environment variable logic to a utils function Move theme environment variable logic to a utils function and update routes to use it. * Add `GetThemeFromEnv` function in `src/utils/theme.go` to derive the theme from environment variables and default to "auto" if not set. * Update `src/routes/home.go` to import and use `GetThemeFromEnv` in the `GetHome` function. * Update `src/routes/options.go` to import and use `GetThemeFromEnv` in the `ChangeOptions` function. * Update `src/routes/question.go` to import and use `GetThemeFromEnv` in the `ViewQuestion` function. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/httpjamesm/AnonymousOverflow/pull/145?shareId=a0dab6f3-027c-4f6e-85fe-60e7675d0e70). --- src/routes/home.go | 6 ++---- src/routes/options.go | 9 ++------- src/routes/question.go | 5 +---- src/utils/theme.go | 11 +++++++++++ 4 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 src/utils/theme.go diff --git a/src/routes/home.go b/src/routes/home.go index 2d12e9d..e5120b4 100644 --- a/src/routes/home.go +++ b/src/routes/home.go @@ -2,6 +2,7 @@ package routes import ( "anonymousoverflow/config" + "anonymousoverflow/src/utils" "fmt" "os" "regexp" @@ -11,10 +12,7 @@ import ( ) func GetHome(c *gin.Context) { - theme := os.Getenv("THEME") - if theme == "" { - theme = "auto" - } + theme := utils.GetThemeFromEnv() c.HTML(200, "home.html", gin.H{ "version": config.Version, "theme": theme, diff --git a/src/routes/options.go b/src/routes/options.go index 9d91e64..4787eca 100644 --- a/src/routes/options.go +++ b/src/routes/options.go @@ -2,10 +2,8 @@ package routes import ( "anonymousoverflow/config" + "anonymousoverflow/src/utils" "fmt" - "os" - - "github.com/gin-gonic/gin" ) func ChangeOptions(c *gin.Context) { @@ -18,10 +16,7 @@ func ChangeOptions(c *gin.Context) { text = "enabled" } c.SetCookie("disable_images", fmt.Sprintf("%t", !c.MustGet("disable_images").(bool)), 60*60*24*365*10, "/", "", false, true) - theme := os.Getenv("THEME") - if theme == "" { - theme = "auto" - } + theme := utils.GetThemeFromEnv() c.HTML(200, "home.html", gin.H{ "successMessage": "Images are now " + text, "version": config.Version, diff --git a/src/routes/question.go b/src/routes/question.go index a7e1a3c..3b6274b 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -101,10 +101,7 @@ func ViewQuestion(c *gin.Context) { imagePolicy = "'self'" } - theme := os.Getenv("THEME") - if theme == "" { - theme = "auto" - } + theme := utils.GetThemeFromEnv() c.HTML(200, "question.html", gin.H{ "question": newFilteredQuestion, diff --git a/src/utils/theme.go b/src/utils/theme.go new file mode 100644 index 0000000..b78d9c4 --- /dev/null +++ b/src/utils/theme.go @@ -0,0 +1,11 @@ +package utils + +import "os" + +func GetThemeFromEnv() string { + theme := os.Getenv("THEME") + if theme == "" { + theme = "auto" + } + return theme +}