summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Wilhelmsson <samuel.wilhelmsson@gmail.com>2024-04-01 17:11:50 +0200
committerSamuel Wilhelmsson <samuel.wilhelmsson@gmail.com>2024-11-14 09:27:54 +0000
commit191e2ddf095a5e57b33caf4a9235c686a462232c (patch)
tree183e0dab0166d32a33e6179d7d4a0221007e20a0
parent9e8b98891bc6446532bd8be203a4aadf30f326be (diff)
downloadtinygram-191e2ddf095a5e57b33caf4a9235c686a462232c.tar.gz
tinygram-191e2ddf095a5e57b33caf4a9235c686a462232c.zip
render time as sweden
-rw-r--r--index.templ8
-rw-r--r--index_templ.go10
-rw-r--r--main.go10
3 files changed, 17 insertions, 11 deletions
diff --git a/index.templ b/index.templ
index 28fb754..23c389a 100644
--- a/index.templ
+++ b/index.templ
@@ -4,7 +4,7 @@ import "fmt"
import "time"
import "net/url"
-templ index(ps []Post) {
+templ index(ps []Post, tz *time.Location) {
<!DOCTYPE html>
<html>
<head>
@@ -21,7 +21,7 @@ templ index(ps []Post) {
<a id="uploadlink" href="/upload"><h2>Tinygram</h2></a>
</div>
<div id="posts">
- @posts(ps)
+ @posts(ps, tz)
</div>
</div>
</body>
@@ -88,7 +88,7 @@ templ uploadPage(csrfToken string) {
</html>
}
-templ posts(posts []Post) {
+templ posts(posts []Post, tz *time.Location) {
for _, post := range posts {
<div class="post">
<div class="imgcontainer">
@@ -96,7 +96,7 @@ templ posts(posts []Post) {
</div>
<div class="summary">
<p class="description">{ post.Description }</p>
- <p class="date">{ post.CreatedAt.Format("2006-01-02 - 15:04") }</p>
+ <p class="date">{ post.CreatedAt.In(tz).Format("2006-01-02 - 15:04") }</p>
</div>
</div>
}
diff --git a/index_templ.go b/index_templ.go
index ff198bb..f4b3f93 100644
--- a/index_templ.go
+++ b/index_templ.go
@@ -14,7 +14,7 @@ import "fmt"
import "time"
import "net/url"
-func index(ps []Post) templ.Component {
+func index(ps []Post, tz *time.Location) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
@@ -31,7 +31,7 @@ func index(ps []Post) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- templ_7745c5c3_Err = posts(ps).Render(ctx, templ_7745c5c3_Buffer)
+ templ_7745c5c3_Err = posts(ps, tz).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -110,7 +110,7 @@ func uploadPage(csrfToken string) templ.Component {
})
}
-func posts(posts []Post) templ.Component {
+func posts(posts []Post, tz *time.Location) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
@@ -158,9 +158,9 @@ func posts(posts []Post) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
- templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(post.CreatedAt.Format("2006-01-02 - 15:04"))
+ templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(post.CreatedAt.In(tz).Format("2006-01-02 - 15:04"))
if templ_7745c5c3_Err != nil {
- return templ.Error{Err: templ_7745c5c3_Err, FileName: `index.templ`, Line: 98, Col: 65}
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `index.templ`, Line: 98, Col: 72}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
diff --git a/main.go b/main.go
index d9f51c5..03b5474 100644
--- a/main.go
+++ b/main.go
@@ -47,6 +47,8 @@ func main() {
assetsPath = "assets"
}
+ swedenTime, _ := time.LoadLocation("Europe/Stockholm")
+
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
@@ -71,7 +73,7 @@ func main() {
e.GET("/", func(c echo.Context) error {
var posts []Post
db.Order("created_at DESC").Limit(5).Find(&posts)
- component := index(posts)
+ component := index(posts, swedenTime)
err := component.Render(c.Request().Context(), c.Response().Writer)
if err != nil {
return err
@@ -134,6 +136,10 @@ func main() {
})
e.POST("/upload", func(c echo.Context) error {
+ sess, _ := session.Get("session", c)
+ if sess.Values["user"] != "admin" {
+ return c.Redirect(http.StatusSeeOther, "/login")
+ }
file, err := c.FormFile("file")
if err != nil {
return err
@@ -181,7 +187,7 @@ func main() {
var ps []Post
db.Order("created_at DESC").Limit(5).Where("created_at < ?", after).Find(&ps)
- component := posts(ps)
+ component := posts(ps, swedenTime)
err = component.Render(c.Request().Context(), c.Response().Writer)
if err != nil {
return err