From 191e2ddf095a5e57b33caf4a9235c686a462232c Mon Sep 17 00:00:00 2001 From: Samuel Wilhelmsson Date: Mon, 1 Apr 2024 17:11:50 +0200 Subject: render time as sweden --- index.templ | 8 ++++---- index_templ.go | 10 +++++----- main.go | 10 ++++++++-- 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) { @@ -21,7 +21,7 @@ templ index(ps []Post) {

Tinygram

- @posts(ps) + @posts(ps, tz)
@@ -88,7 +88,7 @@ templ uploadPage(csrfToken string) { } -templ posts(posts []Post) { +templ posts(posts []Post, tz *time.Location) { for _, post := range posts {
@@ -96,7 +96,7 @@ templ posts(posts []Post) {

{ post.Description }

-

{ post.CreatedAt.Format("2006-01-02 - 15:04") }

+

{ post.CreatedAt.In(tz).Format("2006-01-02 - 15:04") }

} 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 -- cgit v1.2.3