1
0

Web dir with a template and assets was moved off to web directory in root.

This commit is contained in:
Alexander Andreev 2022-05-24 18:29:15 +04:00
parent 8b34c1e935
commit a335ae4803
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -5,26 +5,14 @@ import (
"dwelling-radio/internal/radio" "dwelling-radio/internal/radio"
"dwelling-radio/pkg/logging" "dwelling-radio/pkg/logging"
"dwelling-radio/pkg/utils" "dwelling-radio/pkg/utils"
"embed" "dwelling-radio/web"
"encoding/json" "encoding/json"
"html/template"
"io/fs"
"net/http" "net/http"
"time" "time"
"github.com/Joker/jade"
) )
const FormatISO8601 = "2006-01-02T15:04:05-0700" const FormatISO8601 = "2006-01-02T15:04:05-0700"
var compiledTemplates map[string]*template.Template
//go:embed web/assets
var assetsDir embed.FS
//go:embed web/templates
var templatesDir embed.FS
type NotFoundData struct { type NotFoundData struct {
MainSite string MainSite string
} }
@ -41,7 +29,7 @@ type RadioHandlers struct {
} }
func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers { func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers {
compileTemplates(lErr) web.CompileTemplates(lErr)
return &RadioHandlers{ return &RadioHandlers{
conf: conf, conf: conf,
@ -49,12 +37,11 @@ func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *
} }
func (h *RadioHandlers) AssetsFS() http.FileSystem { func (h *RadioHandlers) AssetsFS() http.FileSystem {
f, _ := fs.Sub(assetsDir, "web/assets") return web.Assets()
return http.FS(f)
} }
func (h *RadioHandlers) Robots(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) Robots(w http.ResponseWriter, r *http.Request) {
fc, _ := assetsDir.ReadFile("web/assets/robots.txt") fc, _ := web.AssetsGetFile("web/assets/robots.txt")
w.Write(fc) w.Write(fc)
} }
@ -135,25 +122,6 @@ func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) { func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"") w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"")
fc, _ := assetsDir.ReadFile("web/assets/radio.arav.top.m3u") fc, _ := web.AssetsGetFile("web/assets/radio.arav.top.m3u")
w.Write(fc) w.Write(fc)
} }
func compileTemplates(lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template)
t, _ := fs.Sub(templatesDir, "web/templates")
templatesFS := http.FS(t)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
indexTpl, err := template.New("index").Parse(indexStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["index"] = indexTpl
}