personal-website/routes/private.go

189 lines
4.8 KiB
Go
Raw Normal View History

package routes
import (
"log"
"net/http"
"path"
"personalwebsite/apps"
2023-05-17 13:09:22 +00:00
"personalwebsite/errormessage"
"personalwebsite/libs"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"time"
2023-07-18 17:15:06 +00:00
mobile "github.com/floresj/go-contrib-mobile"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/location"
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
2023-06-06 22:49:26 +00:00
type AppString struct { //TODO temp
AppPath string
Args []string
}
2023-05-23 23:19:21 +00:00
func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) {
router := gin.New()
router.Use(location.Default())
2023-07-18 17:15:06 +00:00
router.Use(mobile.Resolver())
2023-05-23 23:08:01 +00:00
router.Use(favicon.New("./res/dev-fs/wde/icons/ohno.png"))
router.LoadHTMLGlob("templates/**/*")
2023-05-23 23:08:01 +00:00
router.Static("/res", "res")
2023-07-21 01:42:12 +00:00
router.Static("/front", "./front/")
// router.Use(cors.New(cors.Config{
// AllowOrigins: []string{"http://localhost:1234"},
// AllowMethods: []string{"PUT", "PATCH"},
// AllowHeaders: []string{"Origin"},
// ExposeHeaders: []string{"Content-Length"},
// AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool {
// return origin == "http://localhost:1234"
// },
// MaxAge: 12 * time.Hour,
// }))
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.GET("/", func(ctx *gin.Context) {
2023-06-06 23:17:29 +00:00
// appString := AppString{
// AppPath: "/Applications/Finder.app",
// Args: []string{"/home/user"},
// }
2023-06-06 22:49:26 +00:00
appString := AppString{
2023-06-06 23:17:29 +00:00
AppPath: "/Applications/BlogViewer.app",
Args: []string{"/home/user/Blogs/blog1.blog"},
2023-06-06 22:49:26 +00:00
}
2023-07-19 01:06:13 +00:00
_ = appString
2023-06-07 00:42:50 +00:00
aboutMe := AppString{
AppPath: "/Applications/AboutMe.app",
Args: []string{},
}
2023-07-19 01:06:13 +00:00
_ = aboutMe
2023-06-07 01:22:25 +00:00
desktop := AppString{
AppPath: "/Applications/Finder.app",
2023-07-19 01:06:13 +00:00
Args: []string{"/", "--desktop", "desktop-layer"},
2023-06-07 01:22:25 +00:00
}
2023-06-06 22:49:26 +00:00
2023-07-19 01:06:13 +00:00
autostart := []AppString{desktop}
2023-07-18 17:15:06 +00:00
d := mobile.GetDevice(ctx)
switch {
// Hey I'm a desktop!... or laptop but not a mobile or tablet!
case d.Normal():
ctx.HTML(http.StatusOK, "index.html", gin.H{
2023-07-19 01:06:13 +00:00
"autostart": autostart,
2023-07-18 17:15:06 +00:00
})
// Hey I'm a mobile device!
case d.Mobile():
ctx.HTML(http.StatusOK, "mobile-desktop.html", gin.H{
// "autostart": autostart,
})
// Woa I'm a tablet!
case d.Tablet():
ctx.JSON(http.StatusOK, "Hello I'm a tablet device")
}
})
systemGroup := router.Group("system")
{
libsGroup := systemGroup.Group("libs")
{
imgLibGroup := libsGroup.Group("img")
{
imgLib := libs.NewImgLib(webfs)
imgLib.PublicRoutes(imgLibGroup)
}
catLibGroup := libsGroup.Group("cat")
{
catLib := libs.NewCatLib(webfs)
catLib.PublicRoutes(catLibGroup)
}
appsStorageGroup := libsGroup.Group("apps")
{
2023-05-17 13:09:22 +00:00
appsStorage.PrivateRoute(appsStorageGroup)
}
}
wdeGroup := systemGroup.Group("wde")
{
2023-05-28 01:28:48 +00:00
wde.PublicRoutes(wdeGroup, webde)
}
2023-05-17 13:09:22 +00:00
fsGroup := systemGroup.Group("fs")
{
webfs.PrivateRoutes(fsGroup)
}
2023-05-17 13:09:22 +00:00
systemGroup.GET("/loadApp", func(ctx *gin.Context) {
appPath := ctx.Query("path")
if appPath == "" {
2023-05-17 13:09:22 +00:00
ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{
Message: "Path for the App not provided in request",
})
return
}
appBundleData := webfilesystem.DirectoryData{}
appBundle, err := webfs.Read(appPath, &appBundleData)
if err != nil {
2023-05-17 13:09:22 +00:00
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
Message: "Error in read App bundle",
})
return
}
if appBundle.Type != "directory" {
2023-05-17 13:09:22 +00:00
ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{
Message: "App bundle file is bad",
})
return
}
appManifestData := apps.ApplicationManifest{}
appManifestHeader, err := webfs.Read(path.Join(appPath, ".appmanifest"), &appManifestData)
if err != nil {
2023-05-17 13:09:22 +00:00
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
Message: "Error in read App manifest",
})
return
}
if appManifestHeader.Type != "application-manifest" {
2023-05-17 13:09:22 +00:00
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
Message: "Error in read App manifest",
})
return
}
ctx.JSON(http.StatusOK, appManifestData)
})
}
appsGroup := router.Group("app")
{
for _, app := range appsStorage.Apps {
appsRoutes := appsGroup.Group(app.GetAppID())
app.PrivateRoutes(appsRoutes)
}
}
router.Use(cors.New(cors.Config{
AllowAllOrigins: true,
// AllowOrigins: []string{"http://localhost:8080", "http://localhost:9090"},
// AllowMethods: []string{"PUT", "PATCH"},
// AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool {
// return origin == "https://github.com"
// },
MaxAge: 12 * time.Hour,
}))
2023-05-23 23:19:21 +00:00
err := router.Run(":" + port)
if err != nil {
log.Panicf("error: %s", err)
}
}