From d92edafb0389c6a1032f39c4c86f37ee3ded83de Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Wed, 28 Dec 2022 11:56:17 -0500 Subject: [PATCH] feat!: APP_URL env, theme switcher icon with redirect --- public/question.css | 19 +++++++++++++++++++ src/routes/options.go | 19 +++++++++++++++++++ src/routes/question.go | 2 ++ templates/question.html | 22 +++++++++++++++------- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/public/question.css b/public/question.css index 414e1ed..4a09154 100644 --- a/public/question.css +++ b/public/question.css @@ -24,6 +24,25 @@ body { width: fit-content; } +.header { + display: flex; + justify-content: space-between; +} + +.icon { + width: 2rem; + min-width: 2rem; + height: 2rem; + min-height: 2rem; + cursor: pointer; +} + +.icon img { + background: white; + border-radius: 50%; + padding: .25rem; +} + code { background-color: var(--code-bg); padding: .15rem; diff --git a/src/routes/options.go b/src/routes/options.go index 795ad6c..1728479 100644 --- a/src/routes/options.go +++ b/src/routes/options.go @@ -2,6 +2,8 @@ package routes import ( "fmt" + "os" + "strings" "github.com/gin-gonic/gin" ) @@ -17,6 +19,23 @@ func ChangeOptions(c *gin.Context) { } c.SetCookie("disable_images", fmt.Sprintf("%t", !c.MustGet("disable_images").(bool)), 60*60*24*365*10, "/", "", false, true) c.String(200, "Images are now %s", text) + case "theme": + text := "dark" + if c.MustGet("theme").(string) == "dark" { + text = "light" + } + c.SetCookie("theme", text, 60*60*24*365*10, "/", "", false, true) + // get redirect url from query + redirectUrl := c.Query("redirect_url") + if redirectUrl == "" { + redirectUrl = "/" + } + + if !strings.HasPrefix(redirectUrl, os.Getenv("APP_URL")) { + redirectUrl = "/" + } + + c.Redirect(302, redirectUrl) default: c.String(400, "400 Bad Request") } diff --git a/src/routes/question.go b/src/routes/question.go index 26a74be..cd65a08 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -3,6 +3,7 @@ package routes import ( "fmt" "html/template" + "os" "strings" "github.com/PuerkitoBio/goquery" @@ -157,6 +158,7 @@ func ViewQuestion(c *gin.Context) { "imagePolicy": imagePolicy, "shortenedBody": questionBodyParent.Text()[0:50], "theme": c.MustGet("theme").(string), + "currentUrl": fmt.Sprintf("%s/questions/%s/%s", os.Getenv("APP_URL"), questionId, questionTitle), }) } diff --git a/templates/question.html b/templates/question.html index b6a7fd8..c7a11b0 100644 --- a/templates/question.html +++ b/templates/question.html @@ -16,13 +16,21 @@
- - - +
+ + + +
+ + Toggle theme + +
+

{{ .title }}