From bd969567a4b83b708f87f872ddc58504e6727702 Mon Sep 17 00:00:00 2001
From: httpjamesm <51917118+httpjamesm@users.noreply.github.com>
Date: Thu, 25 Jul 2024 09:57:47 -0700
Subject: [PATCH 1/6] Add theme support using environment variable
---
public/globals.css | 28 +++++++++++++++++++++++++++-
src/routes/home.go | 6 ++++++
src/routes/question.go | 6 ++++++
templates/home.html | 4 ++--
templates/question.html | 4 ++--
5 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/public/globals.css b/public/globals.css
index 8470cd0..0216442 100644
--- a/public/globals.css
+++ b/public/globals.css
@@ -26,6 +26,32 @@
}
}
+[data-theme="dark"] {
+ --main-bg: #1b1f26;
+ --text-color: #fff;
+ --muted-text-color: #b3b3b3;
+ --code-bg: #36383d;
+ --code-fg: #ffffff;
+ --input-bg: #2b303b;
+ --input-bg-hover: #3b404b;
+ --meta-bg: #525262;
+ --divider-color: #42464e;
+ --link-color: #92adff;
+}
+
+[data-theme="light"] {
+ --main-bg: #dbdbdb;
+ --text-color: #000;
+ --muted-text-color: #636363;
+ --code-bg: #36383d;
+ --code-fg: #ffffff;
+ --input-bg: #bcbcbc;
+ --input-bg-hover: #a8a8a8;
+ --meta-bg: #aaa8a8;
+ --divider-color: #b5b5b5;
+ --link-color: #335ad0;
+}
+
a {
color: var(--link-color);
}
@@ -78,4 +104,4 @@ details {
.fw-nowrap {
flex-wrap: nowrap;
-}
\ No newline at end of file
+}
diff --git a/src/routes/home.go b/src/routes/home.go
index a45b4f7..2d12e9d 100644
--- a/src/routes/home.go
+++ b/src/routes/home.go
@@ -3,6 +3,7 @@ package routes
import (
"anonymousoverflow/config"
"fmt"
+ "os"
"regexp"
"strings"
@@ -10,8 +11,13 @@ import (
)
func GetHome(c *gin.Context) {
+ theme := os.Getenv("THEME")
+ if theme == "" {
+ theme = "auto"
+ }
c.HTML(200, "home.html", gin.H{
"version": config.Version,
+ "theme": theme,
})
}
diff --git a/src/routes/question.go b/src/routes/question.go
index 7d5f7bf..a7e1a3c 100644
--- a/src/routes/question.go
+++ b/src/routes/question.go
@@ -101,6 +101,11 @@ func ViewQuestion(c *gin.Context) {
imagePolicy = "'self'"
}
+ theme := os.Getenv("THEME")
+ if theme == "" {
+ theme = "auto"
+ }
+
c.HTML(200, "question.html", gin.H{
"question": newFilteredQuestion,
"answers": answers,
@@ -108,6 +113,7 @@ func ViewQuestion(c *gin.Context) {
"currentUrl": fmt.Sprintf("%s%s", os.Getenv("APP_URL"), c.Request.URL.Path),
"sortValue": params.SoSortValue,
"domain": domain,
+ "theme": theme,
})
}
diff --git a/templates/home.html b/templates/home.html
index cb47709..48bae31 100644
--- a/templates/home.html
+++ b/templates/home.html
@@ -1,5 +1,5 @@
-
+
AnonymousOverflow - Private frontend for StackOverflow
@@ -58,4 +58,4 @@ Get programming help without compromising your privacy.
-
\ No newline at end of file
+
diff --git a/templates/question.html b/templates/question.html
index a0b41f0..6cff3aa 100644
--- a/templates/question.html
+++ b/templates/question.html
@@ -1,5 +1,5 @@
-
+
{{ .question.Title }} | AnonymousOverflow
@@ -92,4 +92,4 @@ Answers
{{ end }}
-
\ No newline at end of file
+
From 31390edfc4d169bfd0f974357cf786d5af36407b Mon Sep 17 00:00:00 2001
From: httpjamesm <51917118+httpjamesm@users.noreply.github.com>
Date: Thu, 25 Jul 2024 10:00:09 -0700
Subject: [PATCH 2/6] Propagate theme variable to template in options.go
Propagate the `theme` variable from the environment to the template in `src/routes/options.go`
* Retrieve the `theme` variable from the environment using `os.Getenv("THEME")`
* Set the `theme` variable in the `gin.H` map when rendering the `home.html` template
---
For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/httpjamesm/AnonymousOverflow/pull/145?shareId=6397c9b4-9450-425c-bbbe-019425965d2b).
---
src/routes/options.go | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/routes/options.go b/src/routes/options.go
index 1bafa59..9d91e64 100644
--- a/src/routes/options.go
+++ b/src/routes/options.go
@@ -3,6 +3,7 @@ package routes
import (
"anonymousoverflow/config"
"fmt"
+ "os"
"github.com/gin-gonic/gin"
)
@@ -17,9 +18,14 @@ 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"
+ }
c.HTML(200, "home.html", gin.H{
"successMessage": "Images are now " + text,
"version": config.Version,
+ "theme": theme,
})
default:
c.String(400, "400 Bad Request")
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 3/6] 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
+}
From 6b9afed2dd88b34c21d02d2231cc693bc6152160 Mon Sep 17 00:00:00 2001
From: httpjamesm
Date: Thu, 25 Jul 2024 10:09:32 -0700
Subject: [PATCH 4/6] fix: imports removed by copilot
---
src/routes/home.go | 1 -
src/routes/options.go | 2 ++
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/routes/home.go b/src/routes/home.go
index e5120b4..176c2a3 100644
--- a/src/routes/home.go
+++ b/src/routes/home.go
@@ -4,7 +4,6 @@ import (
"anonymousoverflow/config"
"anonymousoverflow/src/utils"
"fmt"
- "os"
"regexp"
"strings"
diff --git a/src/routes/options.go b/src/routes/options.go
index 4787eca..c0c8718 100644
--- a/src/routes/options.go
+++ b/src/routes/options.go
@@ -4,6 +4,8 @@ import (
"anonymousoverflow/config"
"anonymousoverflow/src/utils"
"fmt"
+
+ "github.com/gin-gonic/gin"
)
func ChangeOptions(c *gin.Context) {
From ac69c3a59deef7faaa1c5f02621dfaac39e63aa0 Mon Sep 17 00:00:00 2001
From: httpjamesm
Date: Thu, 25 Jul 2024 10:14:06 -0700
Subject: [PATCH 5/6] fix: override theme in posthome
---
src/routes/home.go | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/routes/home.go b/src/routes/home.go
index 176c2a3..78202e1 100644
--- a/src/routes/home.go
+++ b/src/routes/home.go
@@ -71,8 +71,10 @@ func PostHome(c *gin.Context) {
translated := translateUrl(body.URL)
if translated == "" {
+ theme := utils.GetThemeFromEnv()
c.HTML(400, "home.html", gin.H{
"errorMessage": "Invalid stack overflow/exchange URL",
+ "theme": theme,
})
return
}
From ef2f8afc4b5462c47fa6b12522d1b823b948da54 Mon Sep 17 00:00:00 2001
From: httpjamesm
Date: Thu, 25 Jul 2024 10:45:49 -0700
Subject: [PATCH 6/6] style: reduced repetition in themes with common vars
---
public/globals.css | 29 ++++++++---------------------
1 file changed, 8 insertions(+), 21 deletions(-)
diff --git a/public/globals.css b/public/globals.css
index 0216442..4912ddd 100644
--- a/public/globals.css
+++ b/public/globals.css
@@ -1,9 +1,13 @@
:root {
+ --code-bg: #36383d;
+ --code-fg: #ffffff;
+}
+
+:root,
+[data-theme="dark"] {
--main-bg: #1b1f26;
--text-color: #fff;
--muted-text-color: #b3b3b3;
- --code-bg: #36383d;
- --code-fg: #ffffff;
--input-bg: #2b303b;
--input-bg-hover: #3b404b;
--meta-bg: #525262;
@@ -12,12 +16,10 @@
}
@media (prefers-color-scheme: light) {
- :root {
+ :root:not([data-theme="dark"]) {
--main-bg: #dbdbdb;
--text-color: #000;
--muted-text-color: #636363;
- --code-bg: #36383d;
- --code-fg: #ffffff;
--input-bg: #bcbcbc;
--input-bg-hover: #a8a8a8;
--meta-bg: #aaa8a8;
@@ -26,25 +28,10 @@
}
}
-[data-theme="dark"] {
- --main-bg: #1b1f26;
- --text-color: #fff;
- --muted-text-color: #b3b3b3;
- --code-bg: #36383d;
- --code-fg: #ffffff;
- --input-bg: #2b303b;
- --input-bg-hover: #3b404b;
- --meta-bg: #525262;
- --divider-color: #42464e;
- --link-color: #92adff;
-}
-
[data-theme="light"] {
--main-bg: #dbdbdb;
--text-color: #000;
--muted-text-color: #636363;
- --code-bg: #36383d;
- --code-fg: #ffffff;
--input-bg: #bcbcbc;
--input-bg-hover: #a8a8a8;
--meta-bg: #aaa8a8;
@@ -104,4 +91,4 @@ details {
.fw-nowrap {
flex-wrap: nowrap;
-}
+}
\ No newline at end of file