Compare commits

...

1 Commits

Author SHA1 Message Date
28ccfb58c3 start create frontend work of app 2023-09-13 04:37:49 +03:00
5 changed files with 134 additions and 68 deletions

40
main.go
View File

@ -7,12 +7,6 @@ import (
"os" "os"
"personalwebsite/apps" "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/routes"
"personalwebsite/wde" "personalwebsite/wde"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
@ -42,10 +36,10 @@ func main() {
panic(err.Error()) panic(err.Error())
} }
publicPort, err := FindEnv("PUBLIC_PORT") // publicPort, err := FindEnv("PUBLIC_PORT")
if err != nil { // if err != nil {
panic(err.Error()) // panic(err.Error())
} // }
privatePort, err := FindEnv("PRIVATE_PORT") privatePort, err := FindEnv("PRIVATE_PORT")
if err != nil { if err != nil {
@ -68,21 +62,21 @@ func main() {
webde := wde.NewWDE(webfs) webde := wde.NewWDE(webfs)
//TODO Split to different apps init for private and public? //TODO Split to different apps init for private and public?
persPropsApp := aboutme.NewAboutMeApp(webfs) // persPropsApp := aboutme.NewAboutMeApp(webfs)
finderApp := finder.NewFinderApplication(webfs) // finderApp := finder.NewFinderApplication(webfs)
imgViewerApp := imgviewer.NewImgViewerApp(webfs) // imgViewerApp := imgviewer.NewImgViewerApp(webfs)
blogViewerApp := blogviewer.NewBlogViewerApp(webfs) // blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
blogWriterApp := blogwriter.NewBlogWriterApp(webfs) // blogWriterApp := blogwriter.NewBlogWriterApp(webfs)
sunBoardApp := sunboard.NewSunboardApp(webfs, webde, appsStorage) // sunBoardApp := sunboard.NewSunboardApp(webfs, webde, appsStorage)
appsStorage.Apps["personal-properties"] = persPropsApp // appsStorage.Apps["personal-properties"] = persPropsApp
appsStorage.Apps["finder"] = finderApp // appsStorage.Apps["finder"] = finderApp
appsStorage.Apps["img-viewer"] = imgViewerApp // appsStorage.Apps["img-viewer"] = imgViewerApp
appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp // appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp
appsStorage.Apps["BlogWriter"] = blogWriterApp // appsStorage.Apps["BlogWriter"] = blogWriterApp
appsStorage.Apps["Sunboard"] = sunBoardApp // appsStorage.Apps["Sunboard"] = sunBoardApp
go routes.PublicRoutes(publicPort, webfs, webde, appsStorage) // go routes.PublicRoutes(publicPort, webfs, webde, appsStorage)
routes.PrivateRoutes(privatePort, webfs, webde, appsStorage) routes.PrivateRoutes(privatePort, webfs, webde, appsStorage)
} }

View File

@ -31,58 +31,59 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W
router.LoadHTMLGlob("templates/**/*") router.LoadHTMLGlob("templates/**/*")
router.Static("/res", "res") router.Static("/res", "res")
router.Static("/front", "./front/") router.Static("/front", "./front/")
// router.Use(cors.New(cors.Config{ router.Use(cors.New(cors.Config{
// AllowOrigins: []string{"http://localhost:1234"}, AllowOrigins: []string{"http://localhost:5173"},
// AllowMethods: []string{"PUT", "PATCH"}, AllowMethods: []string{"PUT", "PATCH"},
// AllowHeaders: []string{"Origin"}, AllowHeaders: []string{"Origin"},
// ExposeHeaders: []string{"Content-Length"}, ExposeHeaders: []string{"Content-Length"},
// AllowCredentials: true, AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool { AllowOriginFunc: func(origin string) bool {
// return origin == "http://localhost:1234" return origin == "http://localhost:5173"
// }, },
// MaxAge: 12 * time.Hour, MaxAge: 12 * time.Hour,
// })) }))
// Set a lower memory limit for multipart forms (default is 32 MiB) // Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.GET("/", func(ctx *gin.Context) { router.GET("/", func(ctx *gin.Context) {
ctx.Status(200)
// appString := AppString{ // appString := AppString{
// AppPath: "/Applications/Finder.app", // AppPath: "/Applications/Finder.app",
// Args: []string{"/home/user"}, // Args: []string{"/home/user"},
// } // }
appString := AppString{ // appString := AppString{
AppPath: "/Applications/BlogViewer.app", // AppPath: "/Applications/BlogViewer.app",
Args: []string{"/home/user/Blogs/blog1.blog"}, // Args: []string{"/home/user/Blogs/blog1.blog"},
} // }
_ = appString // _ = appString
aboutMe := AppString{ // aboutMe := AppString{
AppPath: "/Applications/AboutMe.app", // AppPath: "/Applications/AboutMe.app",
Args: []string{}, // Args: []string{},
} // }
_ = aboutMe // _ = aboutMe
desktop := AppString{ // desktop := AppString{
AppPath: "/Applications/Finder.app", // AppPath: "/Applications/Finder.app",
Args: []string{"/", "--desktop", "desktop-layer"}, // Args: []string{"/", "--desktop", "desktop-layer"},
} // }
autostart := []AppString{desktop} // autostart := []AppString{desktop}
d := mobile.GetDevice(ctx) // d := mobile.GetDevice(ctx)
switch { // switch {
// Hey I'm a desktop!... or laptop but not a mobile or tablet! // // Hey I'm a desktop!... or laptop but not a mobile or tablet!
case d.Normal(): // case d.Normal():
ctx.HTML(http.StatusOK, "index.html", gin.H{ // ctx.HTML(http.StatusOK, "index.html", gin.H{
"autostart": autostart, // "autostart": autostart,
}) // })
// Hey I'm a mobile device! // // Hey I'm a mobile device!
case d.Mobile(): // case d.Mobile():
ctx.HTML(http.StatusOK, "mobile-desktop.html", gin.H{ // ctx.HTML(http.StatusOK, "mobile-desktop.html", gin.H{
// "autostart": autostart, // // "autostart": autostart,
}) // })
// Woa I'm a tablet! // // Woa I'm a tablet!
case d.Tablet(): // case d.Tablet():
ctx.JSON(http.StatusOK, "Hello I'm a tablet device") // ctx.JSON(http.StatusOK, "Hello I'm a tablet device")
} // }
}) })
systemGroup := router.Group("system") systemGroup := router.Group("system")
@ -114,6 +115,9 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W
fsGroup := systemGroup.Group("fs") fsGroup := systemGroup.Group("fs")
{ {
fsGroup.GET("/", func(ctx *gin.Context) {
ctx.Status(http.StatusInternalServerError)
})
webfs.PrivateRoutes(fsGroup) webfs.PrivateRoutes(fsGroup)
} }

View File

@ -6,7 +6,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive" "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) { func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) {
fileHeader := &FileHeader{} fileHeader := &FileHeader{}
filter := primitive.M{ filter := primitive.M{
@ -28,6 +28,27 @@ func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interfac
return fileHeader, nil 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) { func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data) resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data)
if err != nil { if err != nil {
@ -71,7 +92,7 @@ func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error {
return nil return nil
} }
//Deprecated // Deprecated
func (fs *WebFileSystem) Validate() error { func (fs *WebFileSystem) Validate() error {
// filter := primitive.D{ // filter := primitive.D{
// { // {

View File

@ -16,6 +16,36 @@ func (fs *WebFileSystem) Read(filePath string, fileData interface{}) (*FileHeade
return fileHeader, nil 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) { func (fs *WebFileSystem) ReadByObjectID(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) {
fileHeader, err := fs.readFSDocs(fileID, fileData) fileHeader, err := fs.readFSDocs(fileID, fileData)
if err != nil { if err != nil {

View File

@ -117,7 +117,7 @@ func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) {
ctx.JSON(http.StatusOK, "OK") ctx.JSON(http.StatusOK, "OK")
}) })
route.GET("list", func(ctx *gin.Context) { route.GET("listDir", func(ctx *gin.Context) {
path := ctx.Query("path") path := ctx.Query("path")
if path == "" { if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct 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) 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) { route.GET("validate", func(ctx *gin.Context) {
err := fs.validate() err := fs.validate()
if err != nil { if err != nil {