129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
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()
|
|
GetManifest() ApplicationManifest
|
|
// 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 {
|
|
newAppData := ApplicationManifest{
|
|
AppId: appId,
|
|
Js: []string{},
|
|
Css: []string{},
|
|
}
|
|
|
|
newAppFile := webfilesystem.FileHeader{
|
|
MongoId: primitive.NewObjectID(),
|
|
Name: ".appmanifest",
|
|
Type: "application-manifest",
|
|
Icon: "",
|
|
Data: primitive.NewObjectID(),
|
|
}
|
|
|
|
//TODO: Create folder for app, etc
|
|
_, _, err := as.fs.Write(appPath+"/"+newAppFile.Name, &newAppFile, newAppData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// func NewApplicationsStorage() *ApplicationsStorage {
|
|
// newStorage := ApplicationsStorage{}
|
|
|
|
// return &newStorage
|
|
// }
|
|
|
|
func (aStorage *ApplicationsStorage) Route(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())
|
|
})
|
|
|
|
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.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusOK)
|
|
})
|
|
}
|