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