From 6477240dd4e77f8245ebbef36fd40da0708746e3 Mon Sep 17 00:00:00 2001 From: ngn Date: Sat, 23 Dec 2023 00:22:23 +0300 Subject: [PATCH] Added news sorting and image support --- main.go | 5 ++++- routes/index.go | 2 +- routes/news.go | 16 +++++++++++++--- routes/util.go | 17 ++++++++++++++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 08ac896..bb736e4 100644 --- a/main.go +++ b/main.go @@ -13,11 +13,14 @@ func main(){ Views: engine, }) app.Static("/", "./public") + app.Static("/images", "./content/images") app.Get("/", routes.IndexRoute) app.Get("/news", routes.NewsRoute) app.Get("/news/:id", routes.PostRoute) + app.Get("*", func(c *fiber.Ctx) error { + return routes.RenderError(c, 404) + }) log.Fatal(app.Listen(":9878")) } - diff --git a/routes/index.go b/routes/index.go index 35ca16c..77c9a5f 100644 --- a/routes/index.go +++ b/routes/index.go @@ -9,7 +9,7 @@ func IndexRoute(c *fiber.Ctx) error{ content, err := GetContent("content", "index") if err != nil { log.Error(err) - return c.Status(505).SendString("Server error") + return RenderError(c, 500) } return c.Render("index", fiber.Map{ diff --git a/routes/news.go b/routes/news.go index c3af3e6..87567dc 100644 --- a/routes/news.go +++ b/routes/news.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path" + "sort" "strings" "github.com/gofiber/fiber/v2" @@ -28,18 +29,27 @@ func NewsRoute(c *fiber.Ctx) error { var con Content jsonc, err := os.ReadFile(path.Join(news_path, e.Name())) if err != nil { - log.Errorf("Cannot news JSON: %s", err) + log.Errorf("Cannot news JSON for %s: %s", e.Name(), err) return RenderError(c, 500) } err = json.Unmarshal(jsonc, &con) if err != nil { - log.Errorf("Cannot parse news JSON: %s", err) + log.Errorf("Cannot parse news JSON for %s: %s", e.Name(), err) return RenderError(c, 500) } - + + con.Time, err = TimeFromString(con.Date) + if err != nil { + log.Errorf("Cannot parse time for '%s': %s", con.Name, err) + } res = append(res, con) + } + sort.Slice(res, func(i, j int) bool { + return res[i].Time.After(res[j].Time) + }) + return c.Render("news", fiber.Map{ "news": res, }) diff --git a/routes/util.go b/routes/util.go index 12f4b33..6a70b1a 100644 --- a/routes/util.go +++ b/routes/util.go @@ -6,6 +6,7 @@ import ( "html/template" "os" "path" + "time" "github.com/gofiber/fiber/v2" "github.com/russross/blackfriday/v2" @@ -17,6 +18,16 @@ type Content struct { Date string `json:"date"` Author string `json:"author"` Content template.HTML `json:"content"` + Time time.Time +} + +func TimeFromString(date string) (time.Time, error) { + res, err := time.Parse("02/01/06", date) + if err == nil { + return res, nil + } + + return time.Now(), nil } func GetContent(pth string, name string) (Content, error) { @@ -38,7 +49,11 @@ func GetContent(pth string, name string) (Content, error) { return res, errors.New("Cannot parse JSON: "+jsonfile) } - md := string(blackfriday.Run(mdcontent, blackfriday.WithExtensions(blackfriday.BackslashLineBreak))) + ext := blackfriday.FencedCode + ext |= blackfriday.BackslashLineBreak + ext |= blackfriday.Strikethrough + + md := string(blackfriday.Run(mdcontent, blackfriday.WithExtensions(ext))) res.Content = template.HTML(md) return res, nil }