Files
ProxmoxAAS-Dashboard/app/routes/login.go
T
alu 9b3ec1221e
tronnet/workflows: Static Analysis -- Golang / static-go (push) Failing after 6s
rewrite using net/http
2026-10-22 18:58:14 +00:00

80 lines
1.6 KiB
Go

package routes
import (
"bytes"
"fmt"
"net/http"
"proxmoxaas-dashboard/app/common"
"github.com/gin-gonic/gin"
"github.com/go-viper/mapstructure/v2"
)
// 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{}
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/access/domains", nil, &body)
if err != nil {
return realms, err
}
if code != 200 { // we expect /access/domains to always be avaliable
return realms, fmt.Errorf("request to /proxmox/access/domains resulted in %+v", res)
}
for _, v := range body {
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(w http.ResponseWriter, r *http.Request) {
realms, err := GetLoginRealms()
if err != nil {
common.HandleNonFatalError(w, err)
return
}
sel := common.Select{
ID: "realm",
Required: true,
}
for _, realm := range realms {
sel.Options = append(sel.Options, common.Option{
Selected: realm.Default != 0,
Value: realm.Realm,
Display: realm.Comment,
})
}
w.Header().Set("Content-Type", "text/html")
body := bytes.Buffer{}
err = common.TMPL.ExecuteTemplate(&body, "html/login.html", gin.H{
"global": common.Global,
"page": "login",
"realms": sel,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.Write(body.Bytes())
return
}
}