personal-website/apps/websiteapp.go

146 lines
3.4 KiB
Go
Raw Permalink Normal View History

2023-05-03 21:50:08 +00:00
package apps
2023-03-15 12:34:03 +00:00
2023-03-17 01:16:51 +00:00
import (
"net/http"
2023-05-07 21:20:40 +00:00
"path"
"personalwebsite/webfilesystem"
2023-03-17 01:16:51 +00:00
"github.com/gin-gonic/gin"
2023-05-07 21:20:40 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-03-17 01:16:51 +00:00
)
2023-05-07 21:20:40 +00:00
//TODO to libs
2023-03-15 12:34:03 +00:00
type WebDEApplication interface {
2023-03-17 01:16:51 +00:00
// Render()
GetAppID() string
PublicRoutes(*gin.RouterGroup)
PrivateRoutes(*gin.RouterGroup)
2023-07-23 03:12:17 +00:00
GetPath() string
GetManifest() ApplicationManifest
2023-03-17 01:16:51 +00:00
// GEtHtml()
// GetId() string
2023-03-17 01:16:51 +00:00
}
type ApplicationManifest struct {
2023-07-23 03:12:17 +00:00
AppId string `bson:"appid" json:"appId"`
Js []string `bson:"js" json:"js"`
Css []string `bson:"css" json:"css"`
Iconpath string `bson:"iconpath" json:"iconpath"`
2023-05-07 21:20:40 +00:00
}
func NewApplicationsStorage(apps map[string]WebDEApplication, webfs *webfilesystem.WebFileSystem) *ApplicationsStorage {
return &ApplicationsStorage{
Apps: apps,
fs: webfs,
}
2023-03-17 01:16:51 +00:00
}
type ApplicationsStorage struct {
Apps map[string]WebDEApplication
2023-05-07 21:20:40 +00:00
fs *webfilesystem.WebFileSystem
}
func (as *ApplicationsStorage) createApp(appName string, appId string, appPath string) error {
2023-05-17 13:57:55 +00:00
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
}
2023-05-07 21:20:40 +00:00
newAppData := ApplicationManifest{
AppId: appId,
2023-05-09 00:51:33 +00:00
Js: []string{},
Css: []string{},
2023-05-07 21:20:40 +00:00
}
newAppFile := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
2023-05-09 00:57:53 +00:00
Name: ".appmanifest",
Type: "application-manifest",
2023-05-07 21:20:40 +00:00
Icon: "",
Data: primitive.NewObjectID(),
}
2023-05-17 13:57:55 +00:00
_, _, err = as.fs.Write(appPath+"/"+appBundleName+"/"+newAppFile.Name, &newAppFile, newAppData)
2023-05-07 21:20:40 +00:00
if err != nil {
return err
}
return nil
2023-03-17 01:16:51 +00:00
}
2023-05-17 13:09:22 +00:00
func (aStorage *ApplicationsStorage) PrivateRoute(route *gin.RouterGroup) {
2023-03-17 01:16:51 +00:00
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")
2023-03-17 01:16:51 +00:00
})
2023-05-07 21:20:40 +00:00
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 {
2023-05-17 13:57:55 +00:00
ctx.String(http.StatusInternalServerError, err.Error())
2023-05-07 21:20:40 +00:00
return
}
ctx.Status(http.StatusOK)
})
2023-03-15 12:34:03 +00:00
}