cmd/ursrv: Embed static assets

This commit is contained in:
Jakob Borg 2023-07-10 08:33:09 +02:00
parent bf61e485a6
commit 2fcf7006e6

View File

@ -10,6 +10,7 @@ import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"database/sql" "database/sql"
"embed"
"encoding/json" "encoding/json"
"html/template" "html/template"
"io" "io"
@ -45,6 +46,9 @@ type CLI struct {
GeoIPPath string `env:"UR_GEOIP" default:"GeoLite2-City.mmdb"` GeoIPPath string `env:"UR_GEOIP" default:"GeoLite2-City.mmdb"`
} }
//go:embed static
var statics embed.FS
var ( var (
tpl *template.Template tpl *template.Template
compilerRe = regexp.MustCompile(`\(([A-Za-z0-9()., -]+) \w+-\w+(?:| android| default)\) ([\w@.-]+)`) compilerRe = regexp.MustCompile(`\(([A-Za-z0-9()., -]+) \w+-\w+(?:| android| default)\) ([\w@.-]+)`)
@ -168,7 +172,7 @@ func main() {
// Template // Template
fd, err := os.Open("static/index.html") fd, err := statics.Open("static/index.html")
if err != nil { if err != nil {
log.Fatalln("template:", err) log.Fatalln("template:", err)
} }
@ -224,7 +228,7 @@ func main() {
http.HandleFunc("/performance.json", srv.performanceHandler) http.HandleFunc("/performance.json", srv.performanceHandler)
http.HandleFunc("/blockstats.json", srv.blockStatsHandler) http.HandleFunc("/blockstats.json", srv.blockStatsHandler)
http.HandleFunc("/locations.json", srv.locationsHandler) http.HandleFunc("/locations.json", srv.locationsHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.FileServer(http.FS(statics)))
go srv.cacheRefresher() go srv.cacheRefresher()