mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-09-28 16:25:58 +00:00
55 lines
1003 B
Go
55 lines
1003 B
Go
package routes
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/medium.rip/pkg/client"
|
|
)
|
|
|
|
func show(c *fiber.Ctx) error {
|
|
postId := c.Params("id", "")
|
|
if postId == "" {
|
|
return c.Redirect("/")
|
|
}
|
|
|
|
e, err := client.PostData(postId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
post := e.Data.Post
|
|
publishDate := time.UnixMilli(e.Data.Post.CreatedAt)
|
|
log.Println(publishDate)
|
|
|
|
var sb strings.Builder
|
|
|
|
for _, node := range post.Content.BodyModel.Paragraphs {
|
|
switch node.Type {
|
|
case "H3":
|
|
sb.WriteString(fmt.Sprintf("<h3>%s</h3>", node.Text))
|
|
}
|
|
}
|
|
|
|
return c.Render("show", fiber.Map {
|
|
"Title": post.Title,
|
|
"UserId": post.Creator.ID,
|
|
"Author": post.Creator.Name,
|
|
"PublishDate": publishDate.Format(time.DateOnly),
|
|
"Nodes": post.Content.BodyModel.Paragraphs,
|
|
})
|
|
}
|
|
|
|
func index(c *fiber.Ctx) error {
|
|
return c.Render("index", fiber.Map {
|
|
"Title": "medium.rip",
|
|
})
|
|
}
|
|
|
|
func RegisterRoutes(app *fiber.App) {
|
|
app.Get("/", index)
|
|
app.Get("/:id", show)
|
|
} |