reorganize app files

This commit is contained in:
2025-03-27 22:46:54 +00:00
parent b8ebbf6c3d
commit 79567ea58e
10 changed files with 371 additions and 325 deletions

View File

@@ -3,170 +3,64 @@ package app
import ( import (
"flag" "flag"
"fmt" "fmt"
"html/template"
"log" "log"
"net/http"
"proxmoxaas-dashboard/dist/web" // go will complain here until the first build "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/gin-gonic/gin"
"github.com/tdewolff/minify" "github.com/tdewolff/minify"
) )
var tmpl *template.Template
var global Config
func Run() { func Run() {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
configPath := flag.String("config", "config.json", "path to config.json file") configPath := flag.String("config", "config.json", "path to config.json file")
flag.Parse() flag.Parse()
global = GetConfig(*configPath) common.Global = common.GetConfig(*configPath)
router := gin.Default() router := gin.Default()
m := InitMinify() m := common.InitMinify()
ServeStatic(router, m) ServeStatic(router, m)
html := MinifyStatic(m, web.Templates) html := common.MinifyStatic(m, web.Templates)
tmpl = LoadHTMLToGin(router, html) common.TMPL = common.LoadHTMLToGin(router, html)
router.GET("/account.html", handle_GET_Account) router.GET("/account.html", routes.HandleGETAccount)
router.GET("/", handle_GET_Index) router.GET("/", routes.HandleGETIndex)
router.GET("/index.html", handle_GET_Index) router.GET("/index.html", routes.HandleGETIndex)
router.GET("/instance.html", handle_GET_Instance) router.GET("/instance.html", routes.HandleGETInstance)
router.GET("/login.html", handle_GET_Login) router.GET("/login.html", routes.HandleGETLogin)
router.GET("/settings.html", handle_GET_Settings) 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) { 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) { router.GET("/css/*css", func(c *gin.Context) {
path, _ := c.Params.Get("css") path, _ := c.Params.Get("css")
data := css[fmt.Sprintf("css%s", path)] data := css[fmt.Sprintf("css%s", path)]
c.Data(200, data.MimeType.Type, []byte(data.Data)) 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) { router.GET("/images/*image", func(c *gin.Context) {
path, _ := c.Params.Get("image") path, _ := c.Params.Get("image")
data := images[fmt.Sprintf("images%s", path)] data := images[fmt.Sprintf("images%s", path)]
c.Data(200, data.MimeType.Type, []byte(data.Data)) 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) { router.GET("/modules/*module", func(c *gin.Context) {
path, _ := c.Params.Get("module") path, _ := c.Params.Get("module")
data := modules[fmt.Sprintf("modules%s", path)] data := modules[fmt.Sprintf("modules%s", path)]
c.Data(200, data.MimeType.Type, []byte(data.Data)) 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) { router.GET("/scripts/*script", func(c *gin.Context) {
path, _ := c.Params.Get("script") path, _ := c.Params.Get("script")
data := scripts[fmt.Sprintf("scripts%s", path)] data := scripts[fmt.Sprintf("scripts%s", path)]
c.Data(200, data.MimeType.Type, []byte(data.Data)) 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")
}
}

View File

@@ -1,4 +1,4 @@
package app package common
import ( import (
"io" "io"

35
app/common/types.go Normal file
View File

@@ -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 <select>
type Select struct {
ID string
Name string
Options []Option
}
// type used for templated <option>
type Option struct {
Selected bool
Value string
Display string
}
type RequestType int
type RequestContext struct {
Cookies map[string]string
Body map[string]any
}

View File

@@ -1,4 +1,4 @@
package app package common
import ( import (
"bufio" "bufio"
@@ -15,10 +15,12 @@ import (
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-viper/mapstructure/v2"
"github.com/tdewolff/minify" "github.com/tdewolff/minify"
) )
var TMPL *template.Template
var Global Config
func GetConfig(configPath string) Config { func GetConfig(configPath string) Config {
content, err := os.ReadFile(configPath) content, err := os.ReadFile(configPath)
if err != nil { if err != nil {
@@ -143,7 +145,7 @@ func HandleNonFatalError(c *gin.Context, err error) {
} }
func RequestGetAPI(path string, context RequestContext) (*http.Response, int, error) { func RequestGetAPI(path string, context RequestContext) (*http.Response, int, error) {
req, err := http.NewRequest("GET", global.API+path, nil) req, err := http.NewRequest("GET", Global.API+path, nil)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@@ -188,120 +190,3 @@ func GetAuth(c *gin.Context) (string, string, string, error) {
return username, token, csrf, nil return username, token, csrf, nil
} }
} }
func GetClusterResources(token string, csrf string) (map[uint]Instance, map[string]Node, error) {
ctx := RequestContext{
Cookies: map[string]string{
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
},
Body: map[string]any{},
}
res, code, err := RequestGetAPI("/proxmox/cluster/resources", ctx)
if err != nil {
return nil, nil, err
}
if code != 200 { // if we did not successfully retrieve resources, then return 500 because auth was 1 but was invalid somehow
return nil, nil, fmt.Errorf("request to /cluster/resources/ resulted in %+v", res)
}
instances := map[uint]Instance{}
nodes := map[string]Node{}
// if we successfully retrieved the resources, then process it and return index
for _, v := range ctx.Body["data"].([]any) {
m := v.(map[string]any)
if m["type"] == "node" {
node := Node{}
err := mapstructure.Decode(v, &node)
if err != nil {
return nil, nil, err
}
nodes[node.Node] = node
} else if m["type"] == "lxc" || m["type"] == "qemu" {
instance := Instance{}
err := mapstructure.Decode(v, &instance)
if err != nil {
return nil, nil, err
}
instances[instance.VMID] = instance
}
}
for vmid, instance := range instances {
status := instance.Status
icons := Icons[status]
instance.StatusIcon = icons["status"]
instance.PowerBtnIcon = icons["power"]
instance.PowerBtnIcon.ID = "power-btn"
instance.ConfigureBtnIcon = icons["config"]
instance.ConfigureBtnIcon.ID = "configure-btn"
instance.ConsoleBtnIcon = icons["console"]
instance.ConsoleBtnIcon.ID = "console-btn"
instance.DeleteBtnIcon = icons["delete"]
instance.DeleteBtnIcon.ID = "delete-btn"
nodestatus := nodes[instance.Node].Status
icons = Icons[nodestatus]
instance.NodeStatus = nodestatus
instance.NodeStatusIcon = icons["status"]
instances[vmid] = instance
}
return instances, nodes, nil
}
func GetLoginRealms() ([]Realm, error) {
realms := []Realm{}
ctx := RequestContext{
Cookies: nil,
Body: map[string]any{},
}
res, code, err := RequestGetAPI("/proxmox/access/domains", ctx)
if err != nil {
//HandleNonFatalError(c, err)
return realms, err
}
if code != 200 { // we expect /access/domains to always be avaliable
//HandleNonFatalError(c, err)
return realms, fmt.Errorf("request to /proxmox/access/do9mains resulted in %+v", res)
}
for _, v := range ctx.Body["data"].([]any) {
v = v.(map[string]any)
realm := Realm{}
err := mapstructure.Decode(v, &realm)
if err != nil {
return realms, err
}
realms = append(realms, realm)
}
return realms, nil
}
func GetUserAccount(username string, token string, csrf string) (Account, error) {
account := Account{}
ctx := RequestContext{
Cookies: map[string]string{
"username": username,
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
},
Body: map[string]any{},
}
res, code, err := RequestGetAPI("/user/config/cluster", ctx)
if err != nil {
return account, err
}
if code != 200 {
return account, fmt.Errorf("request to /user/config/cluster resulted in %+v", res)
}
err = mapstructure.Decode(ctx.Body, &account)
if err != nil {
return account, err
} else {
account.Username = username
return account, nil
}
}

67
app/routes/account.go Normal file
View File

@@ -0,0 +1,67 @@
package routes
import (
"fmt"
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
"github.com/go-viper/mapstructure/v2"
)
type Account struct {
Username string
Pools map[string]bool
Nodes map[string]bool
VMID struct {
Min int
Max int
}
}
func HandleGETAccount(c *gin.Context) {
username, token, csrf, err := common.GetAuth(c)
if err == nil {
account, err := GetUserAccount(username, token, csrf)
if err != nil {
common.HandleNonFatalError(c, err)
return
}
c.HTML(http.StatusOK, "html/account.html", gin.H{
"global": common.Global,
"page": "account",
"account": account,
})
} else {
c.Redirect(http.StatusFound, "/login.html") // if user is not authed, redirect user to login page
}
}
func GetUserAccount(username string, token string, csrf string) (Account, error) {
account := Account{}
ctx := common.RequestContext{
Cookies: map[string]string{
"username": username,
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
},
Body: map[string]any{},
}
res, code, err := common.RequestGetAPI("/user/config/cluster", ctx)
if err != nil {
return account, err
}
if code != 200 {
return account, fmt.Errorf("request to /user/config/cluster resulted in %+v", res)
}
err = mapstructure.Decode(ctx.Body, &account)
if err != nil {
return account, err
} else {
account.Username = username
return account, nil
}
}

126
app/routes/index.go Normal file
View File

@@ -0,0 +1,126 @@
package routes
import (
"fmt"
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
"github.com/go-viper/mapstructure/v2"
)
// used in constructing instance cards in index
type Node struct {
Node string `json:"node"`
Status string `json:"status"`
}
// used in constructing instance cards in index
type Instance struct {
VMID uint
Name string
Type string
Status string
Node string
StatusIcon common.Icon
NodeStatus string
NodeStatusIcon common.Icon
PowerBtnIcon common.Icon
ConsoleBtnIcon common.Icon
ConfigureBtnIcon common.Icon
DeleteBtnIcon common.Icon
}
func GetClusterResources(token string, csrf string) (map[uint]Instance, map[string]Node, error) {
ctx := common.RequestContext{
Cookies: map[string]string{
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
},
Body: map[string]any{},
}
res, code, err := common.RequestGetAPI("/proxmox/cluster/resources", ctx)
if err != nil {
return nil, nil, err
}
if code != 200 { // if we did not successfully retrieve resources, then return 500 because auth was 1 but was invalid somehow
return nil, nil, fmt.Errorf("request to /cluster/resources/ resulted in %+v", res)
}
instances := map[uint]Instance{}
nodes := map[string]Node{}
// if we successfully retrieved the resources, then process it and return index
for _, v := range ctx.Body["data"].([]any) {
m := v.(map[string]any)
if m["type"] == "node" {
node := Node{}
err := mapstructure.Decode(v, &node)
if err != nil {
return nil, nil, err
}
nodes[node.Node] = node
} else if m["type"] == "lxc" || m["type"] == "qemu" {
instance := Instance{}
err := mapstructure.Decode(v, &instance)
if err != nil {
return nil, nil, err
}
instances[instance.VMID] = instance
}
}
for vmid, instance := range instances {
status := instance.Status
icons := common.Icons[status]
instance.StatusIcon = icons["status"]
instance.PowerBtnIcon = icons["power"]
instance.PowerBtnIcon.ID = "power-btn"
instance.ConfigureBtnIcon = icons["config"]
instance.ConfigureBtnIcon.ID = "configure-btn"
instance.ConsoleBtnIcon = icons["console"]
instance.ConsoleBtnIcon.ID = "console-btn"
instance.DeleteBtnIcon = icons["delete"]
instance.DeleteBtnIcon.ID = "delete-btn"
nodestatus := nodes[instance.Node].Status
icons = common.Icons[nodestatus]
instance.NodeStatus = nodestatus
instance.NodeStatusIcon = icons["status"]
instances[vmid] = instance
}
return instances, nodes, nil
}
func HandleGETIndex(c *gin.Context) {
_, token, csrf, err := common.GetAuth(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(token, csrf)
if err != nil {
common.HandleNonFatalError(c, err)
}
c.HTML(http.StatusOK, "html/index.html", gin.H{
"global": common.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 HandleGETInstancesFragment(c *gin.Context) {
_, token, csrf, err := common.GetAuth(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(token, csrf)
if err != nil {
common.HandleNonFatalError(c, err)
}
c.Header("Content-Type", "text/plain")
common.TMPL.ExecuteTemplate(c.Writer, "templates/instances.frag", gin.H{
"instances": instances,
})
c.Status(http.StatusOK)
} else { // return index without populating
c.Status(http.StatusUnauthorized)
}
}

20
app/routes/instance.go Normal file
View File

@@ -0,0 +1,20 @@
package routes
import (
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
)
func HandleGETInstance(c *gin.Context) {
_, _, _, err := common.GetAuth(c)
if err == nil {
c.HTML(http.StatusOK, "html/instance.html", gin.H{
"global": common.Global,
"page": "instance",
})
} else {
c.Redirect(http.StatusFound, "/login.html")
}
}

78
app/routes/login.go Normal file
View File

@@ -0,0 +1,78 @@
package routes
import (
"fmt"
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
"github.com/go-viper/mapstructure/v2"
)
// used when requesting GET /access/domains
type GetRealmsBody struct {
Data []Realm `json:"data"`
}
// stores each realm's data
type Realm struct {
Default int `json:"default"`
Realm string `json:"realm"`
Comment string `json:"comment"`
}
func GetLoginRealms() ([]Realm, error) {
realms := []Realm{}
ctx := common.RequestContext{
Cookies: nil,
Body: map[string]any{},
}
res, code, err := common.RequestGetAPI("/proxmox/access/domains", ctx)
if err != nil {
//HandleNonFatalError(c, err)
return realms, err
}
if code != 200 { // we expect /access/domains to always be avaliable
//HandleNonFatalError(c, err)
return realms, fmt.Errorf("request to /proxmox/access/do9mains resulted in %+v", res)
}
for _, v := range ctx.Body["data"].([]any) {
v = v.(map[string]any)
realm := Realm{}
err := mapstructure.Decode(v, &realm)
if err != nil {
return realms, err
}
realms = append(realms, realm)
}
return realms, nil
}
func HandleGETLogin(c *gin.Context) {
realms, err := GetLoginRealms()
if err != nil {
common.HandleNonFatalError(c, err)
}
sel := common.Select{
ID: "realm",
Name: "realm",
}
for _, realm := range realms {
sel.Options = append(sel.Options, common.Option{
Selected: realm.Default != 0,
Value: realm.Realm,
Display: realm.Comment,
})
}
c.HTML(http.StatusOK, "html/login.html", gin.H{
"global": common.Global,
"page": "login",
"realms": sel,
})
}

20
app/routes/settings.go Normal file
View File

@@ -0,0 +1,20 @@
package routes
import (
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
)
func HandleGETSettings(c *gin.Context) {
_, _, _, err := common.GetAuth(c)
if err == nil {
c.HTML(http.StatusOK, "html/settings.html", gin.H{
"global": common.Global,
"page": "settings",
})
} else {
c.Redirect(http.StatusFound, "/login.html")
}
}

View File

@@ -1,79 +0,0 @@
package app
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
}
// used when requesting GET /access/domains
type GetRealmsBody struct {
Data []Realm `json:"data"`
}
// stores each realm's data
type Realm struct {
Default int `json:"default"`
Realm string `json:"realm"`
Comment string `json:"comment"`
}
// type used for templated <select>
type Select struct {
ID string
Name string
Options []Option
}
// type used for templated <option>
type Option struct {
Selected bool
Value string
Display string
}
type RequestType int
type RequestContext struct {
Cookies map[string]string
Body map[string]any
}
// used in constructing instance cards in index
type Node struct {
Node string `json:"node"`
Status string `json:"status"`
}
// used in constructing instance cards in index
type Instance struct {
VMID uint
Name string
Type string
Status string
Node string
StatusIcon Icon
NodeStatus string
NodeStatusIcon Icon
PowerBtnIcon Icon
ConsoleBtnIcon Icon
ConfigureBtnIcon Icon
DeleteBtnIcon Icon
}
type Account struct {
Username string
Pools map[string]bool
Nodes map[string]bool
VMID struct {
Min int
Max int
}
}