From 28ccfb58c3a29a7e16aefba8c0bb08020639a950 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 13 Sep 2023 04:37:49 +0300 Subject: [PATCH] start create frontend work of app --- main.go | 40 ++++++++----------- routes/private.go | 88 +++++++++++++++++++++-------------------- webfilesystem/mongo.go | 25 +++++++++++- webfilesystem/public.go | 30 ++++++++++++++ webfilesystem/routes.go | 19 ++++++++- 5 files changed, 134 insertions(+), 68 deletions(-) diff --git a/main.go b/main.go index e74d836..85282c8 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,6 @@ import ( "os" "personalwebsite/apps" - blogwriter "personalwebsite/apps/BlogWriter" - "personalwebsite/apps/aboutme" - "personalwebsite/apps/blogviewer" - "personalwebsite/apps/finder" - imgviewer "personalwebsite/apps/img-viewer" - "personalwebsite/apps/sunboard" "personalwebsite/routes" "personalwebsite/wde" "personalwebsite/webfilesystem" @@ -42,10 +36,10 @@ func main() { panic(err.Error()) } - publicPort, err := FindEnv("PUBLIC_PORT") - if err != nil { - panic(err.Error()) - } + // publicPort, err := FindEnv("PUBLIC_PORT") + // if err != nil { + // panic(err.Error()) + // } privatePort, err := FindEnv("PRIVATE_PORT") if err != nil { @@ -68,21 +62,21 @@ func main() { webde := wde.NewWDE(webfs) //TODO Split to different apps init for private and public? - persPropsApp := aboutme.NewAboutMeApp(webfs) - finderApp := finder.NewFinderApplication(webfs) - imgViewerApp := imgviewer.NewImgViewerApp(webfs) - blogViewerApp := blogviewer.NewBlogViewerApp(webfs) - blogWriterApp := blogwriter.NewBlogWriterApp(webfs) - sunBoardApp := sunboard.NewSunboardApp(webfs, webde, appsStorage) + // persPropsApp := aboutme.NewAboutMeApp(webfs) + // finderApp := finder.NewFinderApplication(webfs) + // imgViewerApp := imgviewer.NewImgViewerApp(webfs) + // blogViewerApp := blogviewer.NewBlogViewerApp(webfs) + // blogWriterApp := blogwriter.NewBlogWriterApp(webfs) + // sunBoardApp := sunboard.NewSunboardApp(webfs, webde, appsStorage) - appsStorage.Apps["personal-properties"] = persPropsApp - appsStorage.Apps["finder"] = finderApp - appsStorage.Apps["img-viewer"] = imgViewerApp - appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp - appsStorage.Apps["BlogWriter"] = blogWriterApp - appsStorage.Apps["Sunboard"] = sunBoardApp + // appsStorage.Apps["personal-properties"] = persPropsApp + // appsStorage.Apps["finder"] = finderApp + // appsStorage.Apps["img-viewer"] = imgViewerApp + // appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp + // appsStorage.Apps["BlogWriter"] = blogWriterApp + // appsStorage.Apps["Sunboard"] = sunBoardApp - go routes.PublicRoutes(publicPort, webfs, webde, appsStorage) + // go routes.PublicRoutes(publicPort, webfs, webde, appsStorage) routes.PrivateRoutes(privatePort, webfs, webde, appsStorage) } diff --git a/routes/private.go b/routes/private.go index b4f3d25..b4f45f5 100644 --- a/routes/private.go +++ b/routes/private.go @@ -31,58 +31,59 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W router.LoadHTMLGlob("templates/**/*") router.Static("/res", "res") router.Static("/front", "./front/") - // router.Use(cors.New(cors.Config{ - // AllowOrigins: []string{"http://localhost:1234"}, - // AllowMethods: []string{"PUT", "PATCH"}, - // AllowHeaders: []string{"Origin"}, - // ExposeHeaders: []string{"Content-Length"}, - // AllowCredentials: true, - // AllowOriginFunc: func(origin string) bool { - // return origin == "http://localhost:1234" - // }, - // MaxAge: 12 * time.Hour, - // })) + router.Use(cors.New(cors.Config{ + AllowOrigins: []string{"http://localhost:5173"}, + AllowMethods: []string{"PUT", "PATCH"}, + AllowHeaders: []string{"Origin"}, + ExposeHeaders: []string{"Content-Length"}, + AllowCredentials: true, + AllowOriginFunc: func(origin string) bool { + return origin == "http://localhost:5173" + }, + MaxAge: 12 * time.Hour, + })) // Set a lower memory limit for multipart forms (default is 32 MiB) router.MaxMultipartMemory = 8 << 20 // 8 MiB router.GET("/", func(ctx *gin.Context) { + ctx.Status(200) // appString := AppString{ // AppPath: "/Applications/Finder.app", // Args: []string{"/home/user"}, // } - appString := AppString{ - AppPath: "/Applications/BlogViewer.app", - Args: []string{"/home/user/Blogs/blog1.blog"}, - } - _ = appString - aboutMe := AppString{ - AppPath: "/Applications/AboutMe.app", - Args: []string{}, - } - _ = aboutMe - desktop := AppString{ - AppPath: "/Applications/Finder.app", - Args: []string{"/", "--desktop", "desktop-layer"}, - } + // appString := AppString{ + // AppPath: "/Applications/BlogViewer.app", + // Args: []string{"/home/user/Blogs/blog1.blog"}, + // } + // _ = appString + // aboutMe := AppString{ + // AppPath: "/Applications/AboutMe.app", + // Args: []string{}, + // } + // _ = aboutMe + // desktop := AppString{ + // AppPath: "/Applications/Finder.app", + // Args: []string{"/", "--desktop", "desktop-layer"}, + // } - autostart := []AppString{desktop} - d := mobile.GetDevice(ctx) - switch { - // Hey I'm a desktop!... or laptop but not a mobile or tablet! - case d.Normal(): - ctx.HTML(http.StatusOK, "index.html", gin.H{ - "autostart": autostart, - }) - // Hey I'm a mobile device! - case d.Mobile(): - ctx.HTML(http.StatusOK, "mobile-desktop.html", gin.H{ - // "autostart": autostart, - }) - // Woa I'm a tablet! - case d.Tablet(): - ctx.JSON(http.StatusOK, "Hello I'm a tablet device") - } + // autostart := []AppString{desktop} + // d := mobile.GetDevice(ctx) + // switch { + // // Hey I'm a desktop!... or laptop but not a mobile or tablet! + // case d.Normal(): + // ctx.HTML(http.StatusOK, "index.html", gin.H{ + // "autostart": autostart, + // }) + // // Hey I'm a mobile device! + // case d.Mobile(): + // ctx.HTML(http.StatusOK, "mobile-desktop.html", gin.H{ + // // "autostart": autostart, + // }) + // // Woa I'm a tablet! + // case d.Tablet(): + // ctx.JSON(http.StatusOK, "Hello I'm a tablet device") + // } }) systemGroup := router.Group("system") @@ -114,6 +115,9 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W fsGroup := systemGroup.Group("fs") { + fsGroup.GET("/", func(ctx *gin.Context) { + ctx.Status(http.StatusInternalServerError) + }) webfs.PrivateRoutes(fsGroup) } diff --git a/webfilesystem/mongo.go b/webfilesystem/mongo.go index 1c6da1f..f438b40 100644 --- a/webfilesystem/mongo.go +++ b/webfilesystem/mongo.go @@ -6,7 +6,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) -//TODO Transparently split to single data and header reading methods +// TODO Transparently split to single data and header reading methods func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) { fileHeader := &FileHeader{} filter := primitive.M{ @@ -28,6 +28,27 @@ func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interfac return fileHeader, nil } +// New method +func (fs *WebFileSystem) readFileData(fileID primitive.ObjectID) (fileData interface{}, err error) { + fileHeader := &FileHeader{} + filter := primitive.M{ + "_id": fileID, + } + + err = fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader) + if err != nil { + return nil, err + } + filterData := primitive.M{ + "_id": fileHeader.Data, + } + err = fs.webfsFilesData.FindOne(fs.ctx, filterData).Decode(fileData) + if err != nil { + return nil, err + } + return fileData, nil +} + func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data) if err != nil { @@ -71,7 +92,7 @@ func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error { return nil } -//Deprecated +// Deprecated func (fs *WebFileSystem) Validate() error { // filter := primitive.D{ // { diff --git a/webfilesystem/public.go b/webfilesystem/public.go index 023affc..1cdb546 100644 --- a/webfilesystem/public.go +++ b/webfilesystem/public.go @@ -16,6 +16,36 @@ func (fs *WebFileSystem) Read(filePath string, fileData interface{}) (*FileHeade return fileHeader, nil } +// Oboslete method +func (fs *WebFileSystem) ReadData(filePath string, fileData interface{}) (*FileHeader, error) { + fileId, err := fs.FindFile(filePath) + if err != nil { + return nil, err + } + + fileHeader, err := fs.ReadByObjectID(fileId, fileData) + if err != nil { + return nil, err + } + + return fileHeader, nil +} + +// New method +func (fs *WebFileSystem) readData(filePath string) (interface{}, error) { + fileId, err := fs.FindFile(filePath) + if err != nil { + return nil, err + } + + fileData, err := fs.readFileData(fileId) + if err != nil { + return nil, err + } + + return fileData, nil +} + func (fs *WebFileSystem) ReadByObjectID(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) { fileHeader, err := fs.readFSDocs(fileID, fileData) if err != nil { diff --git a/webfilesystem/routes.go b/webfilesystem/routes.go index d83848e..9c6c8df 100644 --- a/webfilesystem/routes.go +++ b/webfilesystem/routes.go @@ -117,7 +117,7 @@ func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) { ctx.JSON(http.StatusOK, "OK") }) - route.GET("list", func(ctx *gin.Context) { + route.GET("listDir", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct @@ -149,6 +149,23 @@ func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) { ctx.JSON(http.StatusOK, &file) }) + route.GET("readFileData", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + // data := + file, err := fs.readData(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + // fs. + ctx.JSON(http.StatusOK, &file) + }) + route.GET("validate", func(ctx *gin.Context) { err := fs.validate() if err != nil {