Move apps to classes

This commit is contained in:
cyber-dream 2023-05-04 00:50:08 +03:00
parent 0ae5eb4325
commit 139831365a
10 changed files with 278 additions and 233 deletions

View File

@ -1,8 +1,9 @@
package blogviewer package blogviewer
import ( import (
"net/http"
"personalwebsite/apps"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -10,20 +11,20 @@ import (
type BlogViewerApplication struct { type BlogViewerApplication struct {
fs *webfilesystem.WebFileSystem fs *webfilesystem.WebFileSystem
manifest websiteapp.ApplicationManifest manifest apps.ApplicationManifest
} }
func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication {
return &BlogViewerApplication{ return &BlogViewerApplication{
fs: webFs, fs: webFs,
manifest: websiteapp.ApplicationManifest{ manifest: apps.ApplicationManifest{
AppId: "blog-viewer", AppId: "blog-viewer",
WindowName: "", WindowName: "",
}, },
} }
} }
func (b *BlogViewerApplication) GetManifest() websiteapp.ApplicationManifest { func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest {
return b.manifest return b.manifest
} }
@ -31,6 +32,41 @@ func (b *BlogViewerApplication) GetId() string {
return b.manifest.AppId 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) { func (b *BlogViewerApplication) WriteMock(path string) {
blogFile := webfilesystem.WebFSFile{ blogFile := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(), MongoId: primitive.NewObjectID(),

59
apps/finder/finder.go Normal file
View File

@ -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{})
})
}

View File

@ -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() {
}

View File

@ -1,9 +1,10 @@
package imgviewer package imgviewer
import ( import (
"net/http"
websiteapp "personalwebsite/apps"
"personalwebsite/libs" "personalwebsite/libs"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -24,6 +25,28 @@ func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp {
return newApp 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 { func (p *ImgViewerApp) GetManifest() websiteapp.ApplicationManifest {
return p.manifest return p.manifest
} }

View File

@ -2,9 +2,10 @@ package personalprops
import ( import (
"errors" "errors"
"net/http"
websiteapp "personalwebsite/apps"
"personalwebsite/libs" "personalwebsite/libs"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -26,6 +27,22 @@ func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp {
return newApp 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 { func (p *PersonalPropertiesApp) GetManifest() websiteapp.ApplicationManifest {
return p.manifest return p.manifest
} }

View File

@ -1,4 +1,4 @@
package websiteapp package apps
import ( import (
"net/http" "net/http"

234
main.go
View File

@ -8,14 +8,14 @@ import (
"net/http" "net/http"
"os" "os"
"personalwebsite/apps"
"personalwebsite/apps/blogviewer"
"personalwebsite/apps/finder"
imgviewer "personalwebsite/apps/img-viewer"
"personalwebsite/apps/personalprops"
"personalwebsite/routewde" "personalwebsite/routewde"
"personalwebsite/wde" "personalwebsite/wde"
"personalwebsite/webfilesystem" "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-contrib/location"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -74,8 +74,8 @@ func main() {
finderApp := finder.NewFinderApplication(webfs) finderApp := finder.NewFinderApplication(webfs)
imgViewerApp := imgviewer.NewImgViewerApp(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs)
blogViewerApp := blogviewer.NewBlogViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
appsStorage := websiteapp.ApplicationsStorage{ appsStorage := apps.ApplicationsStorage{
Apps: map[string]websiteapp.WebDEApplication{}, Apps: map[string]apps.WebDEApplication{},
} }
appsStorage.Apps["personal-properties"] = &persPropsApp appsStorage.Apps["personal-properties"] = &persPropsApp
appsStorage.Apps["finder"] = finderApp appsStorage.Apps["finder"] = finderApp
@ -140,216 +140,66 @@ func main() {
{ {
routewde.Route(wdeGroup, webde) routewde.Route(wdeGroup, webde)
} }
apps := system.Group("applications") // apps := system.Group("applications")
{ // {
apps.GET("/:appid/:method", func(ctx *gin.Context) { // apps.GET("/:appid/:method", func(ctx *gin.Context) {
appId := ctx.Param("appid") // appId := ctx.Param("appid")
method := ctx.Param("method") // method := ctx.Param("method")
app, isExist := appsStorage.Apps[appId] // app, isExist := appsStorage.Apps[appId]
if !isExist { // if !isExist {
ctx.Status(http.StatusNoContent) // ctx.Status(http.StatusNoContent)
return // return
} // }
switch method { // switch method {
case "getmanifest": // case "getmanifest":
ctx.JSON(http.StatusOK, app.GetManifest()) // ctx.JSON(http.StatusOK, app.GetManifest())
case "app.js": // case "app.js":
ctx.File("resources/sys/" + appId + "/" + appId + ".js") // ctx.File("resources/sys/" + appId + "/" + appId + ".js")
case "app.css": // case "app.css":
ctx.File("resources/sys/" + appId + "/" + appId + ".css") // ctx.File("resources/sys/" + appId + "/" + appId + ".css")
default: // default:
ctx.Status(http.StatusBadRequest) // ctx.Status(http.StatusBadRequest)
} // }
}) // })
} // }
websiteapp.Route(apps.Group("/storage"), &appsStorage) // apps.Route(apps.Group("/storage"), &appsStorage)
} }
fs := router.Group("fs") fs := router.Group("fs")
{ {
fs.GET("writeFile", func(ctx *gin.Context) { webfs.Route(fs)
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)
})
} }
app := router.Group("application") app := router.Group("application")
{ {
persPropApp := app.Group("personal-properties") persPropApp := app.Group("personal-properties")
{ {
persPropApp.GET("render", func(ctx *gin.Context) { persPropsApp.Route(persPropApp)
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)
}
})
} }
finderAppRoute := app.Group("finder") finderAppRoute := app.Group("finder")
{ {
finderAppRoute.GET("render", func(ctx *gin.Context) { finderApp.Routes(finderAppRoute)
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{})
})
} }
imgViewerRoute := app.Group("img-viewer") imgViewerRoute := app.Group("img-viewer")
{ {
imgViewerRoute.GET("render", func(ctx *gin.Context) { imgViewerApp.Route(imgViewerRoute)
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)
}
})
} }
blogViewerRoute := app.Group("blog-viewer") blogViewerRoute := app.Group("blog-viewer")
{ {
blogViewerRoute.GET("writeMockBlog", func(ctx *gin.Context) { blogViewerApp.Route(blogViewerRoute)
path := ctx.Query("path") }
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
blogViewerApp.WriteMock(path)
ctx.JSON(http.StatusOK, "OK")
})
blogViewerRoute.GET("render", func(ctx *gin.Context) { err = router.Run(":8080")
isMobileParam := ctx.Query("isMobile") if err != nil {
path := ctx.Query("path") log.Panicf("error: %s", err)
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)
}
})
} }
} }
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) { // func index(c *gin.Context) {
c.HTML(http.StatusOK, "base.html", nil) // c.HTML(http.StatusOK, "base.html", nil)
} // }
func FindEnv(parameter string) (string, error) { func FindEnv(parameter string) (string, error) {
path, exists := os.LookupEnv(parameter) path, exists := os.LookupEnv(parameter)

View File

@ -3,8 +3,10 @@ package webfilesystem
import ( import (
"context" "context"
"errors" "errors"
"net/http"
"strings" "strings"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -172,6 +174,76 @@ func (fs *WebFileSystem) GetParentPath(path string) string {
return parentPath 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 { type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"` MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"` Name string `bson:"name" json:"name"`

View File

@ -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{}
}