package apps import ( "net/http" "path" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) //TODO to libs type WebDEApplication interface { // Render() GetAppID() string PublicRoutes(*gin.RouterGroup) PrivateRoutes(*gin.RouterGroup) // GetManifest() ApplicationManifest //TODO: Delete // GEtHtml() // GetId() string } type ApplicationManifest struct { AppId string `bson:"appid" json:"appId"` Js []string `bson:"js" json:"js"` Css []string `bson:"css" json:"css"` } func NewApplicationsStorage(apps map[string]WebDEApplication, webfs *webfilesystem.WebFileSystem) *ApplicationsStorage { return &ApplicationsStorage{ Apps: apps, fs: webfs, } } type ApplicationsStorage struct { Apps map[string]WebDEApplication fs *webfilesystem.WebFileSystem } func (as *ApplicationsStorage) createApp(appName string, appId string, appPath string) error { appBundleName := appName + ".app" newAppBundleHeader := webfilesystem.FileHeader{ MongoId: primitive.NewObjectID(), Name: appBundleName, Type: "directory", Icon: "", Data: primitive.NewObjectID(), } newAppBundleData := webfilesystem.DirectoryData{ MongoId: primitive.NewObjectID(), Parent: primitive.NewObjectID(), Children: []primitive.ObjectID{}, } _, _, err := as.fs.Write(appPath+"/"+appBundleName, &newAppBundleHeader, &newAppBundleData) if err != nil { return err } newAppData := ApplicationManifest{ AppId: appId, Js: []string{}, Css: []string{}, } newAppFile := webfilesystem.FileHeader{ MongoId: primitive.NewObjectID(), Name: ".appmanifest", Type: "application-manifest", Icon: "", Data: primitive.NewObjectID(), } _, _, err = as.fs.Write(appPath+"/"+appBundleName+"/"+newAppFile.Name, &newAppFile, newAppData) if err != nil { return err } return nil } func (aStorage *ApplicationsStorage) PrivateRoute(route *gin.RouterGroup) { route.GET("/get", func(ctx *gin.Context) { appId := ctx.Query("appid") if appId == "" { ctx.Status(http.StatusBadRequest) return } // app, isExist := aStorage.Apps[appId] // if !isExist { // ctx.Status(http.StatusNoContent) // return // } // ctx.JSON(http.StatusOK, app.GetManifest()) ctx.String(http.StatusMovedPermanently, "Obsolete") }) route.GET("/loadApp", func(ctx *gin.Context) { appPath := ctx.Query("path") if appPath == "" { ctx.Status(http.StatusBadRequest) return } appManifestData := ApplicationManifest{} fileHeader, err := aStorage.fs.Read(path.Join(appPath, ".appmanifest"), &appManifestData) if err != nil { ctx.Status(http.StatusInternalServerError) return } if fileHeader.Type != "application-manifest" { ctx.Status(http.StatusBadRequest) return } ctx.JSON(http.StatusOK, appManifestData) }) route.GET("/createApp", func(ctx *gin.Context) { appId := ctx.Query("appId") if appId == "" { ctx.Status(http.StatusBadRequest) return } appName := ctx.Query("appName") if appName == "" { ctx.Status(http.StatusBadRequest) return } appPath := ctx.Query("path") if appPath == "" { ctx.Status(http.StatusBadRequest) return } err := aStorage.createApp(appId, appName, appPath) if err != nil { ctx.String(http.StatusInternalServerError, err.Error()) return } ctx.Status(http.StatusOK) }) }