diff --git a/go.mod b/go.mod index c47b5a0..44e42bc 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,8 @@ go 1.21 require ( github.com/gin-contrib/gzip v0.0.6 github.com/gin-gonic/gin v1.9.1 - github.com/gobuffalo/packd v1.0.2 github.com/gosimple/slug v1.13.1 github.com/sirupsen/logrus v1.9.3 - github.com/tdewolff/minify/v2 v2.20.14 github.com/tidwall/gjson v1.17.0 github.com/xujiajun/nutsdb v0.11.1 gopkg.in/yaml.v2 v2.4.0 @@ -35,7 +33,6 @@ require ( github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect - github.com/tdewolff/parse/v2 v2.7.9 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/go.sum b/go.sum index 720ffce..81d8d27 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,6 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91 github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= -github.com/gobuffalo/packd v1.0.2 h1:Yg523YqnOxGIWCp69W12yYBKsoChwI7mtu6ceM9Bwfw= -github.com/gobuffalo/packd v1.0.2/go.mod h1:sUc61tDqGMXON80zpKGp92lDb86Km28jfvX7IAyxFT8= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= @@ -101,13 +99,6 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tdewolff/minify/v2 v2.20.14 h1:sktSuVixRwk0ryQjqvKBu/uYS+MWmkwEFMEWtFZ+TdE= -github.com/tdewolff/minify/v2 v2.20.14/go.mod h1:qnIJbnG2dSzk7LIa/UUwgN2OjS8ir6RRlqc0T/1q2xY= -github.com/tdewolff/parse/v2 v2.7.9 h1:4u8nNXNmEGCRVd/slZmZHFL1mv/EVEpHMhSinxdDCqw= -github.com/tdewolff/parse/v2 v2.7.9/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= -github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= -github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo= -github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8= github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/kiosk/assets/static/favicon.ico b/kiosk/assets/favicon.ico similarity index 100% rename from kiosk/assets/static/favicon.ico rename to kiosk/assets/favicon.ico diff --git a/kiosk/assets/templates/index.html b/kiosk/assets/index.html similarity index 100% rename from kiosk/assets/templates/index.html rename to kiosk/assets/index.html diff --git a/kiosk/assets/static/index.js b/kiosk/assets/index.js similarity index 100% rename from kiosk/assets/static/index.js rename to kiosk/assets/index.js diff --git a/kiosk/assets/static/peephole.png b/kiosk/assets/peephole.png similarity index 100% rename from kiosk/assets/static/peephole.png rename to kiosk/assets/peephole.png diff --git a/kiosk/assets/static/style.css b/kiosk/assets/style.css similarity index 100% rename from kiosk/assets/static/style.css rename to kiosk/assets/style.css diff --git a/kiosk/index.go b/kiosk/index.go deleted file mode 100644 index d05af8c..0000000 --- a/kiosk/index.go +++ /dev/null @@ -1,31 +0,0 @@ -package kiosk - -import ( - "net/http" - - "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" -) - -func (k *Kiosk) indexHandler(c *gin.Context) { - // TODO: - // - render html as template (https://github.com/gin-gonic/gin/issues/2855) - const ( - template = "index.html" - mimetype = "text/html" - ) - - html, err := templates.ReadFile(template) - if err != nil { - logrus.WithError(err).WithField("jid", c.Param("jid")).Errorln("Unable to query event") - c.Status(http.StatusNotFound) - } - - min, err := k.minifier.Minify(template, html) - if err != nil { - logrus.WithError(err).WithField("jid", c.Param("jid")).Errorln("Unable to minify template") - c.Status(http.StatusNotFound) - } - - c.Data(http.StatusOK, mimetype, min) -} diff --git a/kiosk/minifier/filesystem.go b/kiosk/minifier/filesystem.go deleted file mode 100644 index cb138a8..0000000 --- a/kiosk/minifier/filesystem.go +++ /dev/null @@ -1,100 +0,0 @@ -package minifier - -import ( - "bytes" - "io" - "net/http" - "path/filepath" - - "github.com/gobuffalo/packd" - "github.com/sirupsen/logrus" - "github.com/tdewolff/minify/v2" - "github.com/tdewolff/minify/v2/css" - "github.com/tdewolff/minify/v2/html" - "github.com/tdewolff/minify/v2/js" -) - -var ( - mimetypes = map[string]string{ - ".html": "text/html", - ".css": "text/css", - ".js": "text/javascript", - } - minifiers = map[string]func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error{ - "text/html": html.Minify, - "text/css": css.Minify, - "text/javascript": js.Minify, - "application/javascript": js.Minify, - } -) - -type Minifier FS - -type FS struct { - http.FileSystem - minifier *minify.M - proxy http.FileSystem - cache map[string][]byte -} - -func Init(proxy http.FileSystem) *FS { - fs := &FS{proxy: proxy} - fs.cache = make(map[string][]byte) - fs.minifier = minify.New() - for k, v := range minifiers { - fs.minifier.AddFunc(k, v) - } - return fs -} - -func (fs FS) Open(name string) (http.File, error) { - if min, ok := fs.cache[name]; ok { - logrus.WithField("name", name).Debugln("Reusing cached minified static resource") - return packd.NewFile(name, bytes.NewReader(min)) - } - - f, err := fs.proxy.Open(name) - if err != nil { - return nil, err - } - - mimetype, ok := mimetypes[filepath.Ext(name)] - if !ok { - logrus.WithField("name", name).Debugln("No minifier available") - return f, nil - } - - logrus.WithField("name", name).Println("Minifying asset") - min, err := fs.minify(filepath.Base(name), mimetype, f) - if err != nil { - return f, nil - } - - return packd.NewFile(name, bytes.NewReader(min)) -} - -func (fs FS) Minify(name string, data []byte) ([]byte, error) { - if min, ok := fs.cache["/"+name]; ok { - logrus.WithField("name", name).Debugln("Reusing cached minified asset") - return min, nil - } - - mimetype, ok := mimetypes[filepath.Ext(name)] - if !ok { - logrus.WithField("name", name).Debugln("No minifier available") - return data, nil - } - - return fs.minify(name, mimetype, bytes.NewReader(data)) -} - -func (fs FS) minify(name, mediatype string, r io.Reader) ([]byte, error) { - buf := &bytes.Buffer{} - err := fs.minifier.Minify(mediatype, buf, r) - if err != nil { - return nil, err - } - - fs.cache["/"+name] = buf.Bytes() - return buf.Bytes(), nil -} diff --git a/kiosk/server.go b/kiosk/server.go index 563e5d0..c0ec064 100644 --- a/kiosk/server.go +++ b/kiosk/server.go @@ -2,30 +2,24 @@ package kiosk import ( "embed" - "net/http" "regexp" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" - _min "github.com/immobiliare/peephole/kiosk/minifier" _mold "github.com/immobiliare/peephole/mold" _event "github.com/immobiliare/peephole/mold/event" _util "github.com/immobiliare/peephole/util" "github.com/sirupsen/logrus" ) -// go:embed assets/templates/** -var templates embed.FS - -// go:embed assets/static/** -var static embed.FS +//go:embed assets +var assets embed.FS type Kiosk struct { mold *_mold.Mold router *gin.Engine eventChan chan *_event.Event config *Config - minifier *_min.FS } func init() { @@ -41,7 +35,6 @@ func Init(db *_mold.Mold, eventChan chan *_event.Event, config *Config) *Kiosk { k.mold = db k.config = config k.eventChan = eventChan - k.minifier = _min.Init(http.FS(static)) k.router = gin.Default() k.router.Use(gzip.Gzip(gzip.DefaultCompression)) @@ -60,8 +53,9 @@ func Init(db *_mold.Mold, eventChan chan *_event.Event, config *Config) *Kiosk { if len(config.BasicAuth) > 0 { group = k.router.Group("/", gin.BasicAuth(gin.Accounts(config.BasicAuth))) } - group.StaticFS("/assets", k.minifier) - group.GET("/", k.indexHandler) + + group.GET("/assets/:filename", k.staticHandler) + group.GET("/", k.staticHandler) group.GET("/events", k.eventsHandler) group.GET("/events/:jid", k.eventHandler) diff --git a/kiosk/static.go b/kiosk/static.go new file mode 100644 index 0000000..9545e37 --- /dev/null +++ b/kiosk/static.go @@ -0,0 +1,28 @@ +package kiosk + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (k *Kiosk) staticHandler(c *gin.Context) { + filename := c.Param("filename") + if filename == "" { + filename = "assets/index.html" + } else { + filename = "assets/" + filename + } + + blob, err := assets.ReadFile(filename) + if err != nil { + c.Status(http.StatusNotFound) + } + + contentType := http.DetectContentType(blob) + if filename == "assets/style.css" { + contentType = "text/css; charset=utf-8" + } + + c.Data(http.StatusOK, contentType, blob) +} diff --git a/salt/login.go b/salt/login.go index c82a71f..e0c9197 100644 --- a/salt/login.go +++ b/salt/login.go @@ -3,12 +3,11 @@ package salt import ( "encoding/json" "fmt" + "io" "net/http" "net/url" "strings" - "io" - _util "github.com/immobiliare/peephole/util" "github.com/sirupsen/logrus" )