Embed static files and HTML templates into the binary
This commit is contained in:
@@ -8,20 +8,28 @@ import (
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
"vkane.cz/tinyquiz/pkg/model"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent"
|
||||
"vkane.cz/tinyquiz/pkg/rtcomm"
|
||||
"vkane.cz/tinyquiz/ui"
|
||||
)
|
||||
|
||||
func newTemplateCache(dir string) (map[string]*template.Template, error) {
|
||||
func newTemplateCache() (map[string]*template.Template, error) {
|
||||
cache := map[string]*template.Template{}
|
||||
|
||||
pages, err := filepath.Glob(filepath.Join(dir, "*.page.tmpl.html"))
|
||||
templates, err := fs.Sub(ui.HTMLTemplates, "html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pages, err := fs.Glob(templates, "*.page.tmpl.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -29,27 +37,23 @@ func newTemplateCache(dir string) (map[string]*template.Template, error) {
|
||||
for _, page := range pages {
|
||||
name := filepath.Base(page)
|
||||
|
||||
ts, err := template.ParseFiles(page)
|
||||
ts, err := template.ParseFS(templates, page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if layouts, err := filepath.Glob(filepath.Join(dir, "*.layout.tmpl.html")); err == nil && len(layouts) > 0 {
|
||||
ts, err = ts.ParseFiles(layouts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
tsL, err := ts.ParseFS(templates, "*.layout.tmpl.html")
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "template: pattern matches no files") {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
ts = tsL
|
||||
}
|
||||
|
||||
if partials, err := filepath.Glob(filepath.Join(dir, "*.partial.tmpl.html")); err == nil && len(partials) > 0 {
|
||||
ts, err = ts.ParseFiles(partials...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
tsP, err := ts.ParseFS(templates, "*.partial.tmpl.html")
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "template: pattern matches no files") {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
ts = tsP
|
||||
}
|
||||
|
||||
cache[name] = ts
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"vkane.cz/tinyquiz/pkg/model/ent"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/migrate"
|
||||
rtcomm "vkane.cz/tinyquiz/pkg/rtcomm"
|
||||
"vkane.cz/tinyquiz/ui"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
_ "github.com/lib/pq"
|
||||
@@ -75,7 +77,7 @@ func main() {
|
||||
rtClients: rtcomm.NewClients(),
|
||||
}
|
||||
|
||||
if tc, err := newTemplateCache("./ui/html/"); err == nil {
|
||||
if tc, err := newTemplateCache(); err == nil {
|
||||
app.templateCache = tc
|
||||
} else {
|
||||
errorLog.Fatal(err)
|
||||
@@ -109,7 +111,11 @@ func main() {
|
||||
|
||||
mux.GET("/ws/:playerUid", app.processWebSocket)
|
||||
|
||||
mux.ServeFiles("/static/*filepath", http.Dir("./ui/static/"))
|
||||
if static, err := fs.Sub(ui.StaticFiles, "static"); err == nil {
|
||||
mux.ServeFiles("/static/*filepath", http.FS(static))
|
||||
} else {
|
||||
errorLog.Fatal(err)
|
||||
}
|
||||
|
||||
var srv = &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
Reference in New Issue
Block a user