personal-website/routes/public.go
2023-07-18 20:15:06 +03:00

104 lines
2.2 KiB
Go

package routes
import (
"log"
"net/http"
"path"
"personalwebsite/apps"
"personalwebsite/libs"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"github.com/gin-contrib/location"
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
func PublicRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) {
router := gin.New()
router.Use(location.Default())
router.LoadHTMLGlob("templates/**/*")
router.Use(favicon.New("./res/dev-fs/wde/icons/ohno.png")) // set favicon middleware
router.Static("/res", "res")
router.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", gin.H{})
})
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)
}
}
wdeGroup := systemGroup.Group("wde")
{
wde.PublicRoutes(wdeGroup, webde)
}
systemGroup.GET("/loadApp", func(ctx *gin.Context) {
appPath := ctx.Query("path")
if appPath == "" {
ctx.Status(http.StatusBadRequest)
return
}
appBundleData := webfilesystem.DirectoryData{}
appBundle, err := webfs.Read(appPath, &appBundleData)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
if appBundle.Type != "directory" {
ctx.Status(http.StatusBadRequest)
return
}
appManifestData := apps.ApplicationManifest{}
appManifestHeader, err := webfs.Read(path.Join(appPath, ".appmanifest"), &appManifestData)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
if appManifestHeader.Type != "application-manifest" {
ctx.Status(http.StatusBadRequest)
return
}
ctx.JSON(http.StatusOK, appManifestData)
})
fsGroup := systemGroup.Group("fs")
{
webfs.PublicRoutes(fsGroup)
}
}
appGroup := router.Group("app")
{
for _, app := range appsStorage.Apps {
appRoutes := appGroup.Group(app.GetAppID())
app.PublicRoutes(appRoutes)
}
}
err := router.Run(":" + port)
if err != nil {
log.Panicf("error: %s", err)
}
}