From 139831365a868d9ada235820401a4822208db85e Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 4 May 2023 00:50:08 +0300 Subject: [PATCH] Move apps to classes --- {websiteapp => apps}/blogviewer/blogfile.go | 0 {websiteapp => apps}/blogviewer/blogviewer.go | 44 +++- apps/finder/finder.go | 59 +++++ apps/finder/finderadmin.go | 22 ++ {websiteapp => apps}/img-viewer/imgviewer.go | 25 +- .../personalprops/personalprops.go | 19 +- {websiteapp => apps}/websiteapp.go | 2 +- main.go | 234 ++++-------------- webfilesystem/webfilesystem.go | 72 ++++++ websiteapp/finder/finder.go | 34 --- 10 files changed, 278 insertions(+), 233 deletions(-) rename {websiteapp => apps}/blogviewer/blogfile.go (100%) rename {websiteapp => apps}/blogviewer/blogviewer.go (63%) create mode 100644 apps/finder/finder.go create mode 100644 apps/finder/finderadmin.go rename {websiteapp => apps}/img-viewer/imgviewer.go (59%) rename {websiteapp => apps}/personalprops/personalprops.go (91%) rename {websiteapp => apps}/websiteapp.go (97%) delete mode 100644 websiteapp/finder/finder.go diff --git a/websiteapp/blogviewer/blogfile.go b/apps/blogviewer/blogfile.go similarity index 100% rename from websiteapp/blogviewer/blogfile.go rename to apps/blogviewer/blogfile.go diff --git a/websiteapp/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go similarity index 63% rename from websiteapp/blogviewer/blogviewer.go rename to apps/blogviewer/blogviewer.go index 8ba103f..318c27f 100644 --- a/websiteapp/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -1,8 +1,9 @@ package blogviewer import ( + "net/http" + "personalwebsite/apps" "personalwebsite/webfilesystem" - "personalwebsite/websiteapp" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" @@ -10,20 +11,20 @@ import ( type BlogViewerApplication struct { fs *webfilesystem.WebFileSystem - manifest websiteapp.ApplicationManifest + manifest apps.ApplicationManifest } func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ fs: webFs, - manifest: websiteapp.ApplicationManifest{ + manifest: apps.ApplicationManifest{ AppId: "blog-viewer", WindowName: "", }, } } -func (b *BlogViewerApplication) GetManifest() websiteapp.ApplicationManifest { +func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest { return b.manifest } @@ -31,6 +32,41 @@ 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(), diff --git a/apps/finder/finder.go b/apps/finder/finder.go new file mode 100644 index 0000000..34a31fc --- /dev/null +++ b/apps/finder/finder.go @@ -0,0 +1,59 @@ +package finder + +import ( + "net/http" + "personalwebsite/apps" + "personalwebsite/webfilesystem" + + "github.com/gin-gonic/gin" +) + +type FinderApplication struct { + fs *webfilesystem.WebFileSystem + manifest apps.ApplicationManifest +} + +func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication { + return &FinderApplication{ + fs: webFs, + manifest: apps.ApplicationManifest{ + AppId: "finder", + WindowName: "TODO DELETE", //TODO DELETE + }, + } +} +func (f *FinderApplication) GetManifest() apps.ApplicationManifest { + return f.manifest +} +func (f *FinderApplication) GetId() string { + return f.manifest.AppId +} + +func (f *FinderApplication) Render(isMobile bool) gin.H { + + return gin.H{} +} + +func (f *FinderApplication) Routes(routes *gin.RouterGroup) { + routes.GET("render", func(ctx *gin.Context) { + isMobileParam := ctx.Query("isMobile") + isMobile := isMobileParam == "true" + if isMobile { + ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile)) + } else { + ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile)) + } + + }) + routes.GET("renderMobileDesktop", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{}) + }) + routes.GET("renderDesktop", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "no path provided") + return + } + ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{}) + }) +} diff --git a/apps/finder/finderadmin.go b/apps/finder/finderadmin.go new file mode 100644 index 0000000..f88205f --- /dev/null +++ b/apps/finder/finderadmin.go @@ -0,0 +1,22 @@ +package finder + +import ( + "personalwebsite/apps" + "personalwebsite/webfilesystem" +) + +type FinderAdminApp struct { + fs *webfilesystem.WebFileSystem + manifest apps.ApplicationManifest +} + +func NewFinderAdminApp(webfs *webfilesystem.WebFileSystem) FinderAdminApp { + return FinderAdminApp{ + fs: webfs, + manifest: apps.ApplicationManifest{}, + } +} + +func (f *FinderAdminApp) RenderWindow() { + +} diff --git a/websiteapp/img-viewer/imgviewer.go b/apps/img-viewer/imgviewer.go similarity index 59% rename from websiteapp/img-viewer/imgviewer.go rename to apps/img-viewer/imgviewer.go index 29775f0..ba2898e 100644 --- a/websiteapp/img-viewer/imgviewer.go +++ b/apps/img-viewer/imgviewer.go @@ -1,9 +1,10 @@ package imgviewer import ( + "net/http" + websiteapp "personalwebsite/apps" "personalwebsite/libs" "personalwebsite/webfilesystem" - "personalwebsite/websiteapp" "github.com/gin-gonic/gin" ) @@ -24,6 +25,28 @@ func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp { return newApp } +func (p *ImgViewerApp) Route(route *gin.RouterGroup) { + route.GET("render", func(ctx *gin.Context) { + isMobileParam := ctx.Query("isMobile") + isMobile := isMobileParam == "true" + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "no path provided") + return + } + ginH, err := p.Render(path, isMobile) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") + return + } + if isMobile { + ctx.HTML(http.StatusOK, "img-viewer/mobile-app.tmpl", ginH) + } else { + ctx.HTML(http.StatusOK, "img-viewer/app.tmpl", ginH) + } + }) +} + func (p *ImgViewerApp) GetManifest() websiteapp.ApplicationManifest { return p.manifest } diff --git a/websiteapp/personalprops/personalprops.go b/apps/personalprops/personalprops.go similarity index 91% rename from websiteapp/personalprops/personalprops.go rename to apps/personalprops/personalprops.go index 675e8f3..230df73 100644 --- a/websiteapp/personalprops/personalprops.go +++ b/apps/personalprops/personalprops.go @@ -2,9 +2,10 @@ package personalprops import ( "errors" + "net/http" + websiteapp "personalwebsite/apps" "personalwebsite/libs" "personalwebsite/webfilesystem" - "personalwebsite/websiteapp" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" @@ -26,6 +27,22 @@ func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp { return newApp } +func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) { + route.GET("render", func(ctx *gin.Context) { + isMobileParam := ctx.Query("isMobile") + isMobile := isMobileParam == "true" + ginH, err := p.Render() + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO + } + if isMobile { + ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH) + } else { + ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", ginH) + } + }) +} + func (p *PersonalPropertiesApp) GetManifest() websiteapp.ApplicationManifest { return p.manifest } diff --git a/websiteapp/websiteapp.go b/apps/websiteapp.go similarity index 97% rename from websiteapp/websiteapp.go rename to apps/websiteapp.go index 1408c71..63a0fa5 100644 --- a/websiteapp/websiteapp.go +++ b/apps/websiteapp.go @@ -1,4 +1,4 @@ -package websiteapp +package apps import ( "net/http" diff --git a/main.go b/main.go index ef69756..3022afe 100644 --- a/main.go +++ b/main.go @@ -8,14 +8,14 @@ import ( "net/http" "os" + "personalwebsite/apps" + "personalwebsite/apps/blogviewer" + "personalwebsite/apps/finder" + imgviewer "personalwebsite/apps/img-viewer" + "personalwebsite/apps/personalprops" "personalwebsite/routewde" "personalwebsite/wde" "personalwebsite/webfilesystem" - "personalwebsite/websiteapp" - "personalwebsite/websiteapp/blogviewer" - "personalwebsite/websiteapp/finder" - imgviewer "personalwebsite/websiteapp/img-viewer" - "personalwebsite/websiteapp/personalprops" "github.com/gin-contrib/location" "github.com/gin-gonic/gin" @@ -74,8 +74,8 @@ func main() { finderApp := finder.NewFinderApplication(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs) - appsStorage := websiteapp.ApplicationsStorage{ - Apps: map[string]websiteapp.WebDEApplication{}, + appsStorage := apps.ApplicationsStorage{ + Apps: map[string]apps.WebDEApplication{}, } appsStorage.Apps["personal-properties"] = &persPropsApp appsStorage.Apps["finder"] = finderApp @@ -140,216 +140,66 @@ func main() { { routewde.Route(wdeGroup, webde) } - apps := system.Group("applications") - { - apps.GET("/:appid/:method", func(ctx *gin.Context) { - appId := ctx.Param("appid") - method := ctx.Param("method") + // apps := system.Group("applications") + // { + // apps.GET("/:appid/:method", func(ctx *gin.Context) { + // appId := ctx.Param("appid") + // method := ctx.Param("method") - app, isExist := appsStorage.Apps[appId] - if !isExist { - ctx.Status(http.StatusNoContent) - return - } - switch method { - case "getmanifest": - ctx.JSON(http.StatusOK, app.GetManifest()) - case "app.js": - ctx.File("resources/sys/" + appId + "/" + appId + ".js") - case "app.css": - ctx.File("resources/sys/" + appId + "/" + appId + ".css") - default: - ctx.Status(http.StatusBadRequest) - } - }) - } + // app, isExist := appsStorage.Apps[appId] + // if !isExist { + // ctx.Status(http.StatusNoContent) + // return + // } + // switch method { + // case "getmanifest": + // ctx.JSON(http.StatusOK, app.GetManifest()) + // case "app.js": + // ctx.File("resources/sys/" + appId + "/" + appId + ".js") + // case "app.css": + // ctx.File("resources/sys/" + appId + "/" + appId + ".css") + // default: + // ctx.Status(http.StatusBadRequest) + // } + // }) + // } - websiteapp.Route(apps.Group("/storage"), &appsStorage) + // apps.Route(apps.Group("/storage"), &appsStorage) } fs := router.Group("fs") { - fs.GET("writeFile", func(ctx *gin.Context) { - parentPath := ctx.Query("parentPath") - if parentPath == "" { - ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct - return - } - file := webfilesystem.WebFSFile{ - MongoId: primitive.NewObjectID(), - Name: "pp", - Type: "test", - Data: nil, - } - err := webfs.CreateFile(&file, parentPath) - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct - return - } - - ctx.JSON(http.StatusOK, "OK") - }) - fs.GET("createDir", func(ctx *gin.Context) { - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct - return - } - - err := webfs.CreateDirectory(path) - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct - return - } - - ctx.JSON(http.StatusOK, "OK") - }) - - fs.GET("list", func(ctx *gin.Context) { - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct - return - } - - files, err := webfs.List(path) - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct - return - } - - ctx.JSON(http.StatusOK, &files) - }) - - fs.GET("read", func(ctx *gin.Context) { - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct - return - } - - file, err := webfs.Read(path) - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct - return - } - - ctx.JSON(http.StatusOK, &file) - }) + webfs.Route(fs) } app := router.Group("application") { persPropApp := app.Group("personal-properties") { - persPropApp.GET("render", func(ctx *gin.Context) { - isMobileParam := ctx.Query("isMobile") - isMobile := isMobileParam == "true" - ginH, err := persPropsApp.Render() - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO - } - if isMobile { - ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH) - } else { - ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", ginH) - } - }) + persPropsApp.Route(persPropApp) } finderAppRoute := app.Group("finder") { - finderAppRoute.GET("render", func(ctx *gin.Context) { - isMobileParam := ctx.Query("isMobile") - isMobile := isMobileParam == "true" - if isMobile { - ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", finderApp.Render(isMobile)) - } else { - ctx.HTML(http.StatusOK, "finder/app.tmpl", finderApp.Render(isMobile)) - } - - }) - finderAppRoute.GET("renderMobileDesktop", func(ctx *gin.Context) { - ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{}) - }) - finderAppRoute.GET("renderDesktop", func(ctx *gin.Context) { - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "no path provided") - return - } - ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{}) - }) + finderApp.Routes(finderAppRoute) } imgViewerRoute := app.Group("img-viewer") { - imgViewerRoute.GET("render", func(ctx *gin.Context) { - isMobileParam := ctx.Query("isMobile") - isMobile := isMobileParam == "true" - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "no path provided") - return - } - ginH, err := imgViewerApp.Render(path, isMobile) - if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") - return - } - if isMobile { - ctx.HTML(http.StatusOK, "img-viewer/mobile-app.tmpl", ginH) - } else { - ctx.HTML(http.StatusOK, "img-viewer/app.tmpl", ginH) - } - - }) + imgViewerApp.Route(imgViewerRoute) } blogViewerRoute := app.Group("blog-viewer") { - blogViewerRoute.GET("writeMockBlog", func(ctx *gin.Context) { - path := ctx.Query("path") - if path == "" { - ctx.JSON(http.StatusBadRequest, "no path provided") - return - } - blogViewerApp.WriteMock(path) - ctx.JSON(http.StatusOK, "OK") - }) + blogViewerApp.Route(blogViewerRoute) + } - blogViewerRoute.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 := blogViewerApp.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) - } - - }) + err = router.Run(":8080") + if err != nil { + log.Panicf("error: %s", err) } } - router.GET("/test", func(ctx *gin.Context) { - ctx.HTML(200, "kek/kek.tmpl", gin.H{}) - }) - - if err := router.Run(":8080"); err != nil { - log.Panicf("error: %s", err) - } } -func index(c *gin.Context) { - c.HTML(http.StatusOK, "base.html", nil) -} +// func index(c *gin.Context) { +// c.HTML(http.StatusOK, "base.html", nil) +// } func FindEnv(parameter string) (string, error) { path, exists := os.LookupEnv(parameter) diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 467b051..1fc56ab 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -3,8 +3,10 @@ package webfilesystem import ( "context" "errors" + "net/http" "strings" + "github.com/gin-gonic/gin" "github.com/mitchellh/mapstructure" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -172,6 +174,76 @@ func (fs *WebFileSystem) GetParentPath(path string) string { return parentPath } +func (fs *WebFileSystem) Route(route *gin.RouterGroup) { + route.GET("writeFile", func(ctx *gin.Context) { + parentPath := ctx.Query("parentPath") + if parentPath == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + file := WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: "pp", + Type: "test", + Data: nil, + } + err := fs.CreateFile(&file, parentPath) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, "OK") + }) + route.GET("createDir", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + err := fs.CreateDirectory(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, "OK") + }) + + route.GET("list", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + files, err := fs.List(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, &files) + }) + + route.GET("read", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + file, err := fs.Read(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, &file) + }) +} + type WebFSFile struct { MongoId primitive.ObjectID `bson:"_id" json:"-"` Name string `bson:"name" json:"name"` diff --git a/websiteapp/finder/finder.go b/websiteapp/finder/finder.go deleted file mode 100644 index d7a65a4..0000000 --- a/websiteapp/finder/finder.go +++ /dev/null @@ -1,34 +0,0 @@ -package finder - -import ( - "personalwebsite/webfilesystem" - "personalwebsite/websiteapp" - - "github.com/gin-gonic/gin" -) - -type FinderApplication struct { - fs *webfilesystem.WebFileSystem - manifest websiteapp.ApplicationManifest -} - -func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication { - return &FinderApplication{ - fs: webFs, - manifest: websiteapp.ApplicationManifest{ - AppId: "finder", - WindowName: "TODO DELETE", //TODO DELETE - }, - } -} -func (f *FinderApplication) GetManifest() websiteapp.ApplicationManifest { - return f.manifest -} -func (f *FinderApplication) GetId() string { - return f.manifest.AppId -} - -func (f *FinderApplication) Render(isMobile bool) gin.H { - - return gin.H{} -}