first commit
This commit is contained in:
18
routes/index.go
Normal file
18
routes/index.go
Normal file
@ -0,0 +1,18 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
)
|
||||
|
||||
func IndexRoute(c *fiber.Ctx) error{
|
||||
content, err := GetContent("content", "readme")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return c.Status(505).SendString("Server error")
|
||||
}
|
||||
|
||||
return c.Render("index", fiber.Map{
|
||||
"readme": content,
|
||||
})
|
||||
}
|
92
routes/news.go
Normal file
92
routes/news.go
Normal file
@ -0,0 +1,92 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"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)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
continue
|
||||
}
|
||||
|
||||
var con Content
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
err = json.Unmarshal(jsonc, &con)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot parse news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
res = append(res, con)
|
||||
}
|
||||
|
||||
return c.Render("news", fiber.Map{
|
||||
"news": res,
|
||||
})
|
||||
}
|
||||
|
||||
func PostRoute(c *fiber.Ctx) error {
|
||||
postid := c.Params("id")
|
||||
if len(postid) == 0 {
|
||||
return RenderError(c, 404)
|
||||
}
|
||||
|
||||
var res Content
|
||||
entries, err := os.ReadDir(news_path)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
continue
|
||||
}
|
||||
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.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)
|
||||
}
|
||||
|
||||
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 RenderError(c, 404)
|
||||
}
|
58
routes/util.go
Normal file
58
routes/util.go
Normal file
@ -0,0 +1,58 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"html/template"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
type Content struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Date string `json:"date"`
|
||||
Author string `json:"author"`
|
||||
Content template.HTML `json:"content"`
|
||||
}
|
||||
|
||||
func GetContent(pth string, name string) (Content, error) {
|
||||
var res Content
|
||||
jsonfile := path.Join(pth, name+".json")
|
||||
mdfile := path.Join(pth, name+".md")
|
||||
|
||||
jsoncontent, err := os.ReadFile(jsonfile)
|
||||
if err != nil {
|
||||
return res, errors.New("Cannot read JSON file: "+jsonfile)
|
||||
}
|
||||
|
||||
mdcontent, err := os.ReadFile(mdfile)
|
||||
if err != nil {
|
||||
return res, errors.New("Cannot read markdown file: "+mdfile)
|
||||
}
|
||||
|
||||
if(json.Unmarshal(jsoncontent, &res)!= nil) {
|
||||
return res, errors.New("Cannot parse JSON: "+jsonfile)
|
||||
}
|
||||
|
||||
md := string(blackfriday.Run(mdcontent, blackfriday.WithExtensions(blackfriday.BackslashLineBreak)))
|
||||
res.Content = template.HTML(md)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func RenderError(c *fiber.Ctx, code int) error{
|
||||
var msg string = "Server Error"
|
||||
c.Status(code)
|
||||
|
||||
switch code {
|
||||
case 404:
|
||||
msg = "Not Found"
|
||||
}
|
||||
|
||||
return c.Render("error", fiber.Map{
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user