personal-website/apps/blogviewer/blogviewer.go

115 lines
2.4 KiB
Go
Raw Normal View History

2023-04-14 15:53:23 +00:00
package blogviewer
import (
2023-05-03 21:50:08 +00:00
"net/http"
2023-04-29 13:58:39 +00:00
"personalwebsite/webfilesystem"
2023-04-14 15:53:23 +00:00
"github.com/gin-gonic/gin"
2023-04-29 13:58:39 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-04-14 15:53:23 +00:00
)
type BlogViewerApplication struct {
fs *webfilesystem.WebFileSystem
appID string
2023-04-14 15:53:23 +00:00
}
2023-04-29 13:58:39 +00:00
func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication {
2023-04-14 15:53:23 +00:00
return &BlogViewerApplication{
fs: webFs,
appID: "BlogViewer",
2023-04-14 15:53:23 +00:00
}
}
func (b *BlogViewerApplication) GetAppID() string {
return b.appID
2023-04-14 15:53:23 +00:00
}
func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) {
b.PublicRoutes(route)
2023-04-14 15:53:23 +00:00
}
func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) {
2023-05-07 01:00:22 +00:00
route.GET("writeMockBlog", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
err := b.WriteMock(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.JSON(http.StatusOK, "OK")
})
2023-05-03 21:50:08 +00:00
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
isMobile := isMobileParam == "true"
ginH, err := b.Render(path, isMobile)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO")
return
}
if isMobile {
ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", ginH)
}
})
}
2023-05-07 01:00:22 +00:00
func (b *BlogViewerApplication) WriteMock(path string) error {
blogFileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "blog1.blog",
Type: "",
Icon: "",
Data: [12]byte{},
}
blogFileData := BlogFileData{
Header: "OMG THIS IS BLOG",
Blocks: []Block{
{
Type: "plain-text",
Data: []string{
"Apoqiwepoqiwepo",
".,mas;dakls;d",
"q[poqwieqpipoi]",
},
},
},
}
_, _, err := b.fs.Write("/home/user/blog1.blog", &blogFileHeader, blogFileData)
2023-04-29 13:58:39 +00:00
if err != nil {
println(err.Error())
2023-05-07 01:00:22 +00:00
return err
2023-04-29 13:58:39 +00:00
}
2023-05-07 01:00:22 +00:00
return nil
}
func (b *BlogViewerApplication) Render(filePath string, isMobile bool) (gin.H, error) {
data := BlogFileData{}
_, err := b.fs.Read(filePath, &data)
if err != nil {
return nil, err
2023-04-14 15:53:23 +00:00
}
2023-04-29 13:58:39 +00:00
return gin.H{
2023-05-07 01:00:22 +00:00
"header": data.Header,
"blocks": data.Blocks,
2023-04-29 13:58:39 +00:00
}, nil
2023-04-14 15:53:23 +00:00
}
type Block struct {
2023-04-29 13:58:39 +00:00
Type string `bson:"type"`
Data []string `bson:"data"`
2023-04-14 15:53:23 +00:00
}