personal-website/main.go
2023-04-14 18:53:23 +03:00

140 lines
3.7 KiB
Go

package main
import (
"log"
"net/http"
"personalwebsite/routewde"
"personalwebsite/websiteapp"
"personalwebsite/websiteapp/blogviewer"
"personalwebsite/websiteapp/finder"
imgviewer "personalwebsite/websiteapp/img-viewer"
"personalwebsite/websiteapp/personalprops"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.New()
router.LoadHTMLGlob("templates/**/*")
router.Static("/res", "resources")
router.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.tmpl", gin.H{})
})
persPropsApp := personalprops.NewPersPropsApp()
finderApp := finder.FinerApplication{}
imgViewerApp := imgviewer.NewImgViewerApp()
blogViewerApp := blogviewer.NewBlogViewerApp()
appsStorage := websiteapp.ApplicationsStorage{
Apps: map[string]websiteapp.WebDEApplication{},
}
appsStorage.Apps["personal-properties"] = &persPropsApp
appsStorage.Apps["finder"] = &finderApp
appsStorage.Apps["img-viewer"] = &imgViewerApp
appsStorage.Apps["blog-viewer"] = blogViewerApp
system := router.Group("system")
{
wde := system.Group("wde")
{
routewde.Route(wde)
}
apps := system.Group("applications")
{
apps.GET("/:appid/:method", func(ctx *gin.Context) {
appId := ctx.Param("appid")
method := ctx.Param("method")
app, isExist := appsStorage.Apps[appId]
if !isExist {
ctx.Status(http.StatusNoContent)
return
}
switch method {
case "getmanifest":
ctx.JSON(http.StatusOK, app.GetManifest())
case "app.js":
ctx.File("resources/sys/" + appId + "/" + appId + ".js")
case "app.css":
ctx.File("resources/sys/" + appId + "/" + appId + ".css")
default:
ctx.Status(http.StatusBadRequest)
}
})
}
websiteapp.Route(apps.Group("/storage"), &appsStorage)
}
app := router.Group("application")
{
persPropApp := app.Group("personal-properties")
{
persPropApp.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
if isMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", persPropsApp.Render())
} else {
ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", persPropsApp.Render())
}
})
}
finderAppRoute := app.Group("finder")
{
finderAppRoute.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
if isMobile {
ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", finderApp.Render(isMobile))
} else {
ctx.HTML(http.StatusOK, "finder/app.tmpl", finderApp.Render(isMobile))
}
})
finderAppRoute.GET("renderMobileDesktop", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{})
})
}
imgViewerRoute := app.Group("img-viewer")
{
imgViewerRoute.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
if isMobile {
ctx.HTML(http.StatusOK, "img-viewer/mobile-app.tmpl", imgViewerApp.Render(isMobile))
} else {
ctx.HTML(http.StatusOK, "img-viewer/app.tmpl", imgViewerApp.Render(isMobile))
}
})
}
blogViewerRoute := app.Group("blog-viewer")
{
blogViewerRoute.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
if isMobile {
ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", blogViewerApp.Render(isMobile))
} else {
ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", blogViewerApp.Render(isMobile))
}
})
}
}
router.GET("/test", func(ctx *gin.Context) {
ctx.HTML(200, "kek/kek.tmpl", gin.H{})
})
if err := router.Run(":8080"); err != nil {
log.Panicf("error: %s", err)
}
}
func index(c *gin.Context) {
c.HTML(http.StatusOK, "base.html", nil)
}