diff --git a/app/routes/account.go b/app/routes/account.go index c075040..f9009c1 100644 --- a/app/routes/account.go +++ b/app/routes/account.go @@ -1,7 +1,6 @@ package routes import ( - "bytes" "fmt" "net/http" paas "proxmoxaas-common-lib" @@ -55,21 +54,12 @@ func HandleGETAccount(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/account.html", map[string]any{ + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/account.html", map[string]any{ "global": common.Global, "page": "account", "user": user, "pools": pools, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { http.Redirect(w, r, "/login", http.StatusFound) // if user is not authed, redirect user to login page return diff --git a/app/routes/backups.go b/app/routes/backups.go index 1165877..0f3e3fd 100644 --- a/app/routes/backups.go +++ b/app/routes/backups.go @@ -1,7 +1,6 @@ package routes import ( - "bytes" "fmt" "net/http" paas "proxmoxaas-common-lib" @@ -41,21 +40,12 @@ func HandleGETBackups(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/index.html", map[string]any{ + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/backups.html", map[string]any{ "global": common.Global, "page": "backups", "backups": backups, "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { http.Redirect(w, r, "/login", http.StatusFound) return @@ -77,18 +67,9 @@ func HandleGETBackupsFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/backups-backups.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/backups-backups.go.tmpl", map[string]any{ "backups": backups, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { // return 401 w.WriteHeader(http.StatusUnauthorized) return diff --git a/app/routes/config.go b/app/routes/config.go index 994c6cf..f4529ee 100644 --- a/app/routes/config.go +++ b/app/routes/config.go @@ -1,7 +1,6 @@ package routes import ( - "bytes" "fmt" "net/http" paas "proxmoxaas-common-lib" @@ -59,20 +58,11 @@ func HandleGETConfig(w http.ResponseWriter, r *http.Request) { } } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/config.html", map[string]any{ + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/config.html", map[string]any{ "global": common.Global, "page": "config", "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { http.Redirect(w, r, "/login", http.StatusFound) return @@ -94,18 +84,9 @@ func HandleGETConfigVolumesFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/config-volumes.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/config-volumes.go.tmpl", map[string]any{ "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { w.WriteHeader(http.StatusUnauthorized) return @@ -127,18 +108,9 @@ func HandleGETConfigNetsFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/config-nets.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/config-nets.go.tmpl", map[string]any{ "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { // return 401 w.WriteHeader(http.StatusUnauthorized) return @@ -160,18 +132,9 @@ func HandleGETConfigDevicesFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/config-devices.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/config-devices.go.tmpl", map[string]any{ "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { // return 401 w.WriteHeader(http.StatusUnauthorized) return @@ -193,18 +156,9 @@ func HandleGETConfigBootFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/config-boot.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/config-boot.go.tmpl", map[string]any{ "config": config, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { // return 401 w.WriteHeader(http.StatusUnauthorized) return diff --git a/app/routes/index.go b/app/routes/index.go index 9b7cac2..0155e29 100644 --- a/app/routes/index.go +++ b/app/routes/index.go @@ -1,7 +1,6 @@ package routes import ( - "bytes" "fmt" "net/http" paas "proxmoxaas-common-lib" @@ -59,16 +58,7 @@ func HandleGETIndex(w http.ResponseWriter, r *http.Request) { "instances": instances, } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/index.html", page) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/index.html", page) } else { // return index without populating http.Redirect(w, r, "/login", http.StatusFound) return @@ -84,18 +74,9 @@ func HandleGETInstancesFragment(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/plain") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/index-instances.go.tmpl", map[string]any{ + paas.HTTPExecuteHTMLFragmentTemplate(w, common.TMPL, "html/index-instances.go.tmpl", map[string]any{ "instances": instances, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { // return 401 w.WriteHeader(http.StatusUnauthorized) return diff --git a/app/routes/login.go b/app/routes/login.go index 3c4594c..236ed65 100644 --- a/app/routes/login.go +++ b/app/routes/login.go @@ -1,9 +1,9 @@ package routes import ( - "bytes" "fmt" "net/http" + paas "proxmoxaas-common-lib" "proxmoxaas-dashboard/app/common" "github.com/go-viper/mapstructure/v2" @@ -61,18 +61,9 @@ func HandleGETLogin(w http.ResponseWriter, r *http.Request) { }) } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/login.html", map[string]any{ + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/login.html", map[string]any{ "global": common.Global, "page": "login", "realms": sel, }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } diff --git a/app/routes/settings.go b/app/routes/settings.go index 18bed57..2246c40 100644 --- a/app/routes/settings.go +++ b/app/routes/settings.go @@ -1,8 +1,8 @@ package routes import ( - "bytes" "net/http" + paas "proxmoxaas-common-lib" "proxmoxaas-dashboard/app/common" ) @@ -15,19 +15,10 @@ func HandleGETSettings(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "text/html") - body := bytes.Buffer{} - err = common.TMPL.ExecuteTemplate(&body, "html/settings.html", map[string]any{ + paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/settings.html", map[string]any{ "global": common.Global, "page": "settings", }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.Write(body.Bytes()) - return - } } else { http.Redirect(w, r, "/login", http.StatusFound) return diff --git a/proxmoxaas-common-lib b/proxmoxaas-common-lib index 8284ffb..35ecaf8 160000 --- a/proxmoxaas-common-lib +++ b/proxmoxaas-common-lib @@ -1 +1 @@ -Subproject commit 8284ffbc43a3be694652399942615f35784df4ef +Subproject commit 35ecaf84e01d0ef6ffd5473cc8219897425b6a65