Added news sorting and image support
This commit is contained in:
parent
6b503d8555
commit
6477240dd4
5
main.go
5
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"))
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user