switch to gin, add basic server redering to login

This commit is contained in:
2025-03-04 22:40:22 +00:00
parent 323129185b
commit 9bd1fc8321
9 changed files with 246 additions and 211 deletions

View File

@@ -1,196 +1,123 @@
package app
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/fs"
"io"
"log"
"net/http"
"proxmoxaas-dashboard/dist/web" // go will complain here until the first build
"strings"
"text/template"
"github.com/gin-gonic/gin"
"github.com/tdewolff/minify"
)
func ParseTemplates() map[string]*template.Template {
// create html map which stores html files to parsed templates
html := make(map[string]*template.Template)
fs.WalkDir(web.HTML, ".", func(path string, html_entry fs.DirEntry, err error) error { // walk the html directory
if err != nil {
return err
}
if !html_entry.IsDir() { // if it is an html file, parse with all the template files
v, err := fs.ReadFile(web.HTML, path)
if err != nil {
log.Fatalf("error reading html file %s: %s", path, err.Error())
}
t := template.New(html_entry.Name()) // parse the html file
t, err = t.Parse(string(v))
if err != nil {
log.Fatalf("error parsing html file %s: %s", path, err.Error())
}
// parse the html with every template file
fs.WalkDir(web.Templates, ".", func(path string, templates_entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !templates_entry.IsDir() { // if it is a template file, parse it
v, err = fs.ReadFile(web.Templates, path)
if err != nil {
log.Fatalf("error reading template file %s: %s", path, err.Error())
}
t, err = t.Parse(string(v)) // parse the template file
if err != nil {
log.Fatalf("error parsing template file %s: %s", path, err.Error())
}
}
return nil
})
html[html_entry.Name()] = t
}
return nil
})
return html
}
func ServeStatic(m *minify.M) {
func ServeStatic(router *gin.Engine, m *minify.M) {
css := MinifyStatic(m, web.CSS_fs)
http.HandleFunc("/css/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
data := css[path]
w.Header().Add("Content-Type", data.MimeType.Type)
w.Write([]byte(data.Data))
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)
http.HandleFunc("/images/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
data := images[path]
w.Header().Add("Content-Type", data.MimeType.Type)
w.Write([]byte(data.Data))
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)
http.HandleFunc("/modules/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
data := modules[path]
w.Header().Add("Content-Type", data.MimeType.Type)
w.Write([]byte(data.Data))
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)
http.HandleFunc("/scripts/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
data := scripts[path]
w.Header().Add("Content-Type", data.MimeType.Type)
w.Write([]byte(data.Data))
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 Run() {
gin.SetMode(gin.ReleaseMode)
configPath := flag.String("config", "config.json", "path to config.json file")
flag.Parse()
global := GetConfig(*configPath)
router := gin.Default()
m := InitMinify()
ServeStatic(m)
ServeStatic(router, m)
html := ParseTemplates()
html := MinifyStatic(m, web.Templates)
LoadHTMLToGin(router, html)
http.HandleFunc("/account.html", func(w http.ResponseWriter, r *http.Request) {
global.Page = "account"
page := bytes.Buffer{}
err := html["account.html"].Execute(&page, global)
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
}
w.Write(minified.Bytes())
router.GET("/account.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "html/account.html", gin.H{
"global": global,
"page": "account",
})
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
global.Page = "index"
page := bytes.Buffer{}
err := html["index.html"].Execute(&page, global)
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
}
w.Write(minified.Bytes())
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "html/index.html", gin.H{
"global": global,
"page": "index",
})
})
http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
global.Page = "index"
page := bytes.Buffer{}
err := html["index.html"].Execute(&page, global)
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
}
w.Write(minified.Bytes())
router.GET("/index.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "html/index.html", gin.H{
"global": global,
"page": "index",
})
})
http.HandleFunc("/instance.html", func(w http.ResponseWriter, r *http.Request) {
global.Page = "instance"
page := bytes.Buffer{}
err := html["instance.html"].Execute(&page, global)
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
}
w.Write(minified.Bytes())
router.GET("/instance.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "html/instance.html", gin.H{
"global": global,
"page": "instance",
})
})
http.HandleFunc("/login.html", func(w http.ResponseWriter, r *http.Request) {
global.Page = "login"
page := bytes.Buffer{}
err := html["login.html"].Execute(&page, global)
router.GET("/login.html", func(c *gin.Context) {
response, err := http.Get(global.API + "/proxmox/access/domains")
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
data, err := io.ReadAll(response.Body)
response.Body.Close()
body := GetRealmsBody{}
json.Unmarshal(data, &body)
realms := Select{
ID: "realm",
Name: "realm",
}
w.Write(minified.Bytes())
for _, realm := range body.Data {
realms.Options = append(realms.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": realms,
})
})
http.HandleFunc("/settings.html", func(w http.ResponseWriter, r *http.Request) {
global.Page = "settings"
page := bytes.Buffer{}
err := html["settings.html"].Execute(&page, global)
if err != nil {
log.Fatal(err.Error())
}
minified := bytes.Buffer{}
err = m.Minify("text/html", &minified, &page)
if err != nil {
log.Fatal(err.Error())
}
w.Write(minified.Bytes())
router.GET("/settings.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "html/settings.html", gin.H{
"global": global,
"page": "settings",
})
})
log.Printf("Starting HTTP server at port: %d\n", global.Port)
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", global.Port), nil)
if err != nil {
log.Fatal(err)
}
router.Run(fmt.Sprintf("0.0.0.0:%d", global.Port))
}

73
app/types.go Normal file
View File

@@ -0,0 +1,73 @@
package app
import (
"io"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
type MimeType struct {
Type string
Minifier func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error
}
var PlainTextMimeType = MimeType{
Type: "text/plain",
Minifier: nil,
}
var MimeTypes = map[string]MimeType{
"css": {
Type: "text/css",
Minifier: css.Minify,
},
"html": {
Type: "text/html",
Minifier: html.Minify,
},
"tmpl": {
Type: "text/plain",
Minifier: TemplateMinifier,
},
"svg": {
Type: "image/svg+xml",
Minifier: nil,
},
"js": {
Type: "application/javascript",
Minifier: js.Minify,
},
"wasm": {
Type: "application/wasm",
Minifier: nil,
},
}
// 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
}

View File

@@ -1,18 +1,18 @@
package app
import (
"bufio"
"embed"
"encoding/json"
"html/template"
"io"
"io/fs"
"log"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
type Config struct {
@@ -20,7 +20,6 @@ type Config struct {
Organization string `json:"organization"`
PVE string `json:"pveurl"`
API string `json:"apiurl"`
Page string
}
func GetConfig(configPath string) Config {
@@ -36,39 +35,6 @@ func GetConfig(configPath string) Config {
return config
}
type MimeType struct {
Type string
Minifier func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error
}
var PlainTextMimeType = MimeType{
Type: "text/plain",
Minifier: nil,
}
var MimeTypes = map[string]MimeType{
"css": {
Type: "text/css",
Minifier: css.Minify,
},
"html": {
Type: "text/html",
Minifier: html.Minify,
},
"svg": {
Type: "image/svg+xml",
Minifier: nil,
},
"js": {
Type: "application/javascript",
Minifier: js.Minify,
},
"wasm": {
Type: "application/wasm",
Minifier: nil,
},
}
func InitMinify() *minify.M {
m := minify.New()
for _, v := range MimeTypes {
@@ -119,8 +85,48 @@ func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile {
MimeType: PlainTextMimeType,
}
}
log.Printf("%s: %s \n\n", path, minified[path].Data)
}
return nil
})
return minified
}
func LoadHTMLToGin(engine *gin.Engine, html map[string]StaticFile) {
root := template.New("")
tmpl := template.Must(root, LoadAndAddToRoot(engine.FuncMap, root, html))
engine.SetHTMLTemplate(tmpl)
}
func LoadAndAddToRoot(FuncMap template.FuncMap, root *template.Template, html map[string]StaticFile) error {
for name, file := range html {
t := root.New(name).Funcs(FuncMap)
_, err := t.Parse(file.Data)
if err != nil {
return err
}
}
return nil
}
func TemplateMinifier(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
// remove newlines and tabs
rb := bufio.NewReader(r)
for {
line, err := rb.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
line = strings.Replace(line, "\n", "", -1)
line = strings.Replace(line, "\t", "", -1)
line = strings.Replace(line, " ", "", -1)
if _, errws := io.WriteString(w, line); errws != nil {
return errws
}
if err == io.EOF {
break
}
}
return nil
}