Added news sorting and image support

This commit is contained in:
ngn 2023-12-23 00:22:23 +03:00
parent 6b503d8555
commit 6477240dd4
4 changed files with 34 additions and 6 deletions

View File

@ -13,11 +13,14 @@ func main(){
Views: engine, Views: engine,
}) })
app.Static("/", "./public") app.Static("/", "./public")
app.Static("/images", "./content/images")
app.Get("/", routes.IndexRoute) app.Get("/", routes.IndexRoute)
app.Get("/news", routes.NewsRoute) app.Get("/news", routes.NewsRoute)
app.Get("/news/:id", routes.PostRoute) app.Get("/news/:id", routes.PostRoute)
app.Get("*", func(c *fiber.Ctx) error {
return routes.RenderError(c, 404)
})
log.Fatal(app.Listen(":9878")) log.Fatal(app.Listen(":9878"))
} }

View File

@ -9,7 +9,7 @@ func IndexRoute(c *fiber.Ctx) error{
content, err := GetContent("content", "index") content, err := GetContent("content", "index")
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return c.Status(505).SendString("Server error") return RenderError(c, 500)
} }
return c.Render("index", fiber.Map{ return c.Render("index", fiber.Map{

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path" "path"
"sort"
"strings" "strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -28,18 +29,27 @@ func NewsRoute(c *fiber.Ctx) error {
var con Content var con Content
jsonc, err := os.ReadFile(path.Join(news_path, e.Name())) jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
if err != nil { 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) return RenderError(c, 500)
} }
err = json.Unmarshal(jsonc, &con) err = json.Unmarshal(jsonc, &con)
if err != nil { 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) 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) 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{ return c.Render("news", fiber.Map{
"news": res, "news": res,
}) })

View File

@ -6,6 +6,7 @@ import (
"html/template" "html/template"
"os" "os"
"path" "path"
"time"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
@ -17,6 +18,16 @@ type Content struct {
Date string `json:"date"` Date string `json:"date"`
Author string `json:"author"` Author string `json:"author"`
Content template.HTML `json:"content"` 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) { 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) 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) res.Content = template.HTML(md)
return res, nil return res, nil
} }