From 79567ea58e49dd6be3a145eb9eeb818003701443 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Thu, 27 Mar 2025 22:46:54 +0000 Subject: [PATCH] reorganize app files --- app/app.go | 144 +++++--------------------------------- app/{ => common}/meta.go | 2 +- app/common/types.go | 35 +++++++++ app/{ => common}/utils.go | 125 ++------------------------------- app/routes/account.go | 67 ++++++++++++++++++ app/routes/index.go | 126 +++++++++++++++++++++++++++++++++ app/routes/instance.go | 20 ++++++ app/routes/login.go | 78 +++++++++++++++++++++ app/routes/settings.go | 20 ++++++ app/types.go | 79 --------------------- 10 files changed, 371 insertions(+), 325 deletions(-) rename app/{ => common}/meta.go (99%) create mode 100644 app/common/types.go rename app/{ => common}/utils.go (57%) create mode 100644 app/routes/account.go create mode 100644 app/routes/index.go create mode 100644 app/routes/instance.go create mode 100644 app/routes/login.go create mode 100644 app/routes/settings.go delete mode 100644 app/types.go diff --git a/app/app.go b/app/app.go index 1cefcf6..732f314 100644 --- a/app/app.go +++ b/app/app.go @@ -3,170 +3,64 @@ package app import ( "flag" "fmt" - "html/template" "log" - "net/http" "proxmoxaas-dashboard/dist/web" // go will complain here until the first build + "proxmoxaas-dashboard/app/common" + "proxmoxaas-dashboard/app/routes" + "github.com/gin-gonic/gin" "github.com/tdewolff/minify" ) -var tmpl *template.Template -var global Config - func Run() { gin.SetMode(gin.ReleaseMode) configPath := flag.String("config", "config.json", "path to config.json file") flag.Parse() - global = GetConfig(*configPath) + common.Global = common.GetConfig(*configPath) router := gin.Default() - m := InitMinify() + m := common.InitMinify() ServeStatic(router, m) - html := MinifyStatic(m, web.Templates) - tmpl = LoadHTMLToGin(router, html) + html := common.MinifyStatic(m, web.Templates) + common.TMPL = common.LoadHTMLToGin(router, html) - router.GET("/account.html", handle_GET_Account) - router.GET("/", handle_GET_Index) - router.GET("/index.html", handle_GET_Index) - router.GET("/instance.html", handle_GET_Instance) - router.GET("/login.html", handle_GET_Login) - router.GET("/settings.html", handle_GET_Settings) + router.GET("/account.html", routes.HandleGETAccount) + router.GET("/", routes.HandleGETIndex) + router.GET("/index.html", routes.HandleGETIndex) + router.GET("/instance.html", routes.HandleGETInstance) + router.GET("/login.html", routes.HandleGETLogin) + router.GET("/settings.html", routes.HandleGETSettings) - router.GET("/instances_fragment", handle_GET_Instances_Fragment) + router.GET("/instances_fragment", routes.HandleGETInstancesFragment) - log.Fatal(router.Run(fmt.Sprintf("0.0.0.0:%d", global.Port))) + log.Fatal(router.Run(fmt.Sprintf("0.0.0.0:%d", common.Global.Port))) } func ServeStatic(router *gin.Engine, m *minify.M) { - css := MinifyStatic(m, web.CSS_fs) + css := common.MinifyStatic(m, web.CSS_fs) router.GET("/css/*css", func(c *gin.Context) { path, _ := c.Params.Get("css") data := css[fmt.Sprintf("css%s", path)] c.Data(200, data.MimeType.Type, []byte(data.Data)) }) - images := MinifyStatic(m, web.Images_fs) + images := common.MinifyStatic(m, web.Images_fs) router.GET("/images/*image", func(c *gin.Context) { path, _ := c.Params.Get("image") data := images[fmt.Sprintf("images%s", path)] c.Data(200, data.MimeType.Type, []byte(data.Data)) }) - modules := MinifyStatic(m, web.Modules_fs) + modules := common.MinifyStatic(m, web.Modules_fs) router.GET("/modules/*module", func(c *gin.Context) { path, _ := c.Params.Get("module") data := modules[fmt.Sprintf("modules%s", path)] c.Data(200, data.MimeType.Type, []byte(data.Data)) }) - scripts := MinifyStatic(m, web.Scripts_fs) + scripts := common.MinifyStatic(m, web.Scripts_fs) router.GET("/scripts/*script", func(c *gin.Context) { path, _ := c.Params.Get("script") data := scripts[fmt.Sprintf("scripts%s", path)] c.Data(200, data.MimeType.Type, []byte(data.Data)) }) } - -func handle_GET_Account(c *gin.Context) { - username, token, csrf, err := GetAuth(c) - if err == nil { - account, err := GetUserAccount(username, token, csrf) - if err != nil { - HandleNonFatalError(c, err) - return - } - - c.HTML(http.StatusOK, "html/account.html", gin.H{ - "global": global, - "page": "account", - "account": account, - }) - } else { - c.Redirect(http.StatusFound, "/login.html") // if user is not authed, redirect user to login page - } -} - -func handle_GET_Index(c *gin.Context) { - _, token, csrf, err := GetAuth(c) - if err == nil { // user should be authed, try to return index with population - instances, _, err := GetClusterResources(token, csrf) - if err != nil { - HandleNonFatalError(c, err) - } - c.HTML(http.StatusOK, "html/index.html", gin.H{ - "global": global, - "page": "index", - "instances": instances, - }) - } else { // return index without populating - c.Redirect(http.StatusFound, "/login.html") // if user is not authed, redirect user to login page - } -} - -func handle_GET_Instances_Fragment(c *gin.Context) { - _, token, csrf, err := GetAuth(c) - if err == nil { // user should be authed, try to return index with population - instances, _, err := GetClusterResources(token, csrf) - if err != nil { - HandleNonFatalError(c, err) - } - c.Header("Content-Type", "text/plain") - tmpl.ExecuteTemplate(c.Writer, "templates/instances.frag", gin.H{ - "instances": instances, - }) - c.Status(http.StatusOK) - } else { // return index without populating - c.Status(http.StatusUnauthorized) - } - -} - -func handle_GET_Instance(c *gin.Context) { - _, _, _, err := GetAuth(c) - if err == nil { - c.HTML(http.StatusOK, "html/instance.html", gin.H{ - "global": global, - "page": "instance", - }) - } else { - c.Redirect(http.StatusFound, "/login.html") - } -} - -func handle_GET_Login(c *gin.Context) { - realms, err := GetLoginRealms() - if err != nil { - HandleNonFatalError(c, err) - } - - sel := Select{ - ID: "realm", - Name: "realm", - } - - for _, realm := range realms { - sel.Options = append(sel.Options, Option{ - Selected: realm.Default != 0, - Value: realm.Realm, - Display: realm.Comment, - }) - } - - c.HTML(http.StatusOK, "html/login.html", gin.H{ - "global": global, - "page": "login", - "realms": sel, - }) -} - -func handle_GET_Settings(c *gin.Context) { - _, _, _, err := GetAuth(c) - if err == nil { - c.HTML(http.StatusOK, "html/settings.html", gin.H{ - "global": global, - "page": "settings", - }) - } else { - c.Redirect(http.StatusFound, "/login.html") - } -} diff --git a/app/meta.go b/app/common/meta.go similarity index 99% rename from app/meta.go rename to app/common/meta.go index 6c754f0..a71e05c 100644 --- a/app/meta.go +++ b/app/common/meta.go @@ -1,4 +1,4 @@ -package app +package common import ( "io" diff --git a/app/common/types.go b/app/common/types.go new file mode 100644 index 0000000..7b4a9ea --- /dev/null +++ b/app/common/types.go @@ -0,0 +1,35 @@ +package common + +type Config struct { + Port int `json:"listenPort"` + Organization string `json:"organization"` + DASH string `json:"dashurl"` + PVE string `json:"pveurl"` + API string `json:"apiurl"` +} + +type StaticFile struct { + Data string + MimeType MimeType +} + +// type used for templated -type Select struct { - ID string - Name string - Options []Option -} - -// type used for templated