package blogviewer import ( "net/http" "personalwebsite/apps" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) type BlogViewerApplication struct { fs *webfilesystem.WebFileSystem manifest apps.ApplicationManifest } func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ fs: webFs, manifest: apps.ApplicationManifest{ AppId: "blog-viewer", WindowName: "", }, } } func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest { return b.manifest } func (b *BlogViewerApplication) GetId() string { return b.manifest.AppId } func (b *BlogViewerApplication) Route(route *gin.RouterGroup) { route.GET("writeMockBlog", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { ctx.JSON(http.StatusBadRequest, "no path provided") return } b.WriteMock(path) ctx.JSON(http.StatusOK, "OK") }) 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) } }) } func (b *BlogViewerApplication) WriteMock(path string) { blogFile := webfilesystem.WebFSFile{ MongoId: primitive.NewObjectID(), Name: "test-1.blog", Type: "blog-page", Data: BlogFileData{ Header: "OMG THIS IS BLOG", Blocks: []Block{ { Type: "plain-text", Data: []string{ "Apoqiwepoqiwepo", ".,mas;dakls;d", "q[poqwieqpipoi]", }, }, }, }, } err := b.fs.CreateFile(&blogFile, path) if err != nil { println(err.Error()) } } func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) { file, err := b.fs.Read(path) if err != nil { println(err.Error()) return nil, err } blogDataRaw := file.Data.(primitive.D).Map() header := blogDataRaw["header"] blocks := []Block{} for _, rawBlock := range blogDataRaw["blocks"].(primitive.A) { lel := rawBlock.(primitive.D).Map() block := Block{ Type: lel["type"].(string), Data: []string{}, } for _, v := range lel["data"].(primitive.A) { block.Data = append(block.Data, v.(string)) } blocks = append(blocks, block) } return gin.H{ "header": header, "blocks": blocks, }, nil } type Block struct { Type string `bson:"type"` Data []string `bson:"data"` }