feat!: APP_URL env, theme switcher icon with redirect

This commit is contained in:
httpjamesm 2022-12-28 11:56:17 -05:00
parent 3d53fd5806
commit d92edafb03
4 changed files with 55 additions and 7 deletions

View File

@ -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;

View File

@ -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")
}

View File

@ -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),
})
}

View File

@ -16,13 +16,21 @@
</head>
<body>
<div class="parent">
<a href="/" class="logo-link">
<img
class="logo"
src="/static/codecircles.png"
alt="4 circles with alternating colors between green and white"
/>
</a>
<div class="header">
<a href="/" class="logo-link">
<img
class="logo"
src="/static/codecircles.png"
alt="4 circles with alternating colors between green and white"
/>
</a>
<div class="icon">
<a href="/options/theme?redirect_url={{ .currentUrl }}">
<img src="/static/icons/{{ if eq .theme "dark" }}sun{{
else }}moon{{ end }}.svg" alt="Toggle theme" />
</a>
</div>
</div>
<div class="card">
<div class="card-header">
<h1>{{ .title }}</h1>