1
0

Templates are moved off to root/web directory.

This commit is contained in:
Alexander Andreev 2022-05-25 00:27:30 +04:00
parent 0125a1f588
commit d142bbcd7d
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
2 changed files with 86 additions and 75 deletions

View File

@ -6,31 +6,19 @@ import (
"dwelling-upload/pkg/logging"
"dwelling-upload/pkg/server"
"dwelling-upload/pkg/utils"
"embed"
"dwelling-upload/web"
"encoding/base64"
"encoding/hex"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/Joker/jade"
)
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 {
MainSite string
}
@ -63,7 +51,7 @@ type UploadHandlers struct {
}
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers {
compileTemplates(lErr)
web.CompileTemplates(lErr)
return &UploadHandlers{
conf: conf,
@ -74,8 +62,7 @@ func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logg
}
func (*UploadHandlers) AssetsFS() http.FileSystem {
f, _ := fs.Sub(assetsDir, "web/assets")
return http.FS(f)
return web.Assets()
}
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
@ -87,7 +74,7 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
if err := compiledTemplates["index"].Execute(w, &IndexData{
if err := web.Template("index").Execute(w, &IndexData{
MainSite: utils.MainSite(r.Host),
FileMaxSz: fMaxSzStr,
StorageCapacity: storCapacity,
@ -132,7 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if leftSpace < fHandler.Size {
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
w.WriteHeader(http.StatusInternalServerError)
if err := compiledTemplates["nospace"].Execute(w, &NotFoundData{
if err := web.Template("nospace").Execute(w, &NotFoundData{
MainSite: utils.MainSite(r.Host),
}); err != nil {
h.logErr.Fatalln("failed to execute NoSpace template:", err)
@ -200,7 +187,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
return
}
if err := compiledTemplates["uploaded"].Execute(w, &UploadedData{
if err := web.Template("uploaded").Execute(w, &UploadedData{
MainSite: utils.MainSite(r.Host),
DownloadURL: downloadURLParsed.String(),
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
@ -242,65 +229,10 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
if err := compiledTemplates["404"].Execute(w, NotFoundData{
if err := web.Template("404").Execute(w, NotFoundData{
MainSite: utils.MainSite(r.Host),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute 404 template:", err)
}
}
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
uploadedStr, err := jade.ParseFileFromFileSystem("uploaded.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
uploadedTpl, err := template.New("uploaded").Parse(uploadedStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["uploaded"] = uploadedTpl
notfoundStr, err := jade.ParseFileFromFileSystem("404.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
notfoundTpl, err := template.New("404").Parse(notfoundStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["404"] = notfoundTpl
nospaceStr, err := jade.ParseFileFromFileSystem("nospace.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
nospaceTpl, err := template.New("nospace").Parse(nospaceStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["nospace"] = nospaceTpl
}

79
web/web.go Normal file
View File

@ -0,0 +1,79 @@
package web
import (
"dwelling-upload/pkg/logging"
"embed"
"html/template"
"io/fs"
"net/http"
"github.com/Joker/jade"
)
var compiledTemplates map[string]*template.Template
//go:embed assets
var assetsDir embed.FS
func Assets() http.FileSystem {
f, _ := fs.Sub(assetsDir, "assets")
return http.FS(f)
}
func AssetsGetFile(path string) ([]byte, error) {
return assetsDir.ReadFile("assets/" + path)
}
//go:embed templates
var templatesDir embed.FS
func Template(name string) *template.Template {
return compiledTemplates[name]
}
func CompileTemplates(lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template)
t, _ := fs.Sub(templatesDir, "templates")
templatesFS := http.FS(t)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["index"], err = template.New("index").Parse(indexStr)
if err != nil {
lErr.Fatalln(err)
}
uploadedStr, err := jade.ParseFileFromFileSystem("uploaded.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["uploaded"], err = template.New("uploaded").Parse(uploadedStr)
if err != nil {
lErr.Fatalln(err)
}
notfoundStr, err := jade.ParseFileFromFileSystem("404.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["404"], err = template.New("404").Parse(notfoundStr)
if err != nil {
lErr.Fatalln(err)
}
nospaceStr, err := jade.ParseFileFromFileSystem("nospace.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["nospace"], err = template.New("nospace").Parse(nospaceStr)
if err != nil {
lErr.Fatalln(err)
}
}