Adding the wiki route and better styling
This commit is contained in:
@ -1,102 +1,69 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.matterlinux.xyz/Matterlinux/website/lib"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
)
|
||||
|
||||
var news_path = path.Join("content", "news")
|
||||
|
||||
func NewsRoute(c *fiber.Ctx) error {
|
||||
var res []Content
|
||||
entries, err := os.ReadDir(news_path)
|
||||
contents, err := lib.ListContent("news")
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("ListContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
continue
|
||||
sort.Slice(contents, func(i, j int) bool {
|
||||
time1, err := lib.TimeFromString(contents[i].Date)
|
||||
if err != nil {
|
||||
log.Errorf("Bad date while sorting: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
var con Content
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news JSON for %s: %s", e.Name(), err)
|
||||
return RenderError(c, 500)
|
||||
time2, err := lib.TimeFromString(contents[j].Date)
|
||||
if err != nil {
|
||||
log.Errorf("Bad date while sorting: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
err = json.Unmarshal(jsonc, &con)
|
||||
if err != nil {
|
||||
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 time1.After(time2)
|
||||
})
|
||||
|
||||
return c.Render("news", fiber.Map{
|
||||
"news": res,
|
||||
"news": contents,
|
||||
})
|
||||
}
|
||||
|
||||
func PostRoute(c *fiber.Ctx) error {
|
||||
postid := c.Params("id")
|
||||
if len(postid) == 0 {
|
||||
return RenderError(c, 404)
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
||||
|
||||
var res Content
|
||||
entries, err := os.ReadDir(news_path)
|
||||
contents, err := lib.ListContent("news")
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("ListContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
for _, con := range contents {
|
||||
if(con.ID != postid) {
|
||||
continue
|
||||
}
|
||||
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
|
||||
con, err = lib.GetContent(con.Dir, con.Name)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonc, &res)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot parse news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("GetContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
if(res.ID == postid) {
|
||||
res, err = GetContent(news_path, strings.Split(e.Name(), ".json")[0])
|
||||
if err != nil {
|
||||
log.Errorf("Cannot get content: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
return c.Render("post", fiber.Map{
|
||||
"post": res,
|
||||
})
|
||||
}
|
||||
return c.Render("post", fiber.Map{
|
||||
"title": "News",
|
||||
"post": con,
|
||||
})
|
||||
}
|
||||
|
||||
return RenderError(c, 404)
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
||||
|
Reference in New Issue
Block a user