Templates are moved off to root/web directory.
This commit is contained in:
parent
0125a1f588
commit
d142bbcd7d
@ -6,31 +6,19 @@ import (
|
|||||||
"dwelling-upload/pkg/logging"
|
"dwelling-upload/pkg/logging"
|
||||||
"dwelling-upload/pkg/server"
|
"dwelling-upload/pkg/server"
|
||||||
"dwelling-upload/pkg/utils"
|
"dwelling-upload/pkg/utils"
|
||||||
"embed"
|
"dwelling-upload/web"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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 {
|
type NotFoundData struct {
|
||||||
MainSite string
|
MainSite string
|
||||||
}
|
}
|
||||||
@ -63,7 +51,7 @@ type UploadHandlers struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers {
|
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers {
|
||||||
compileTemplates(lErr)
|
web.CompileTemplates(lErr)
|
||||||
|
|
||||||
return &UploadHandlers{
|
return &UploadHandlers{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
@ -74,8 +62,7 @@ func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logg
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (*UploadHandlers) AssetsFS() http.FileSystem {
|
func (*UploadHandlers) AssetsFS() http.FileSystem {
|
||||||
f, _ := fs.Sub(assetsDir, "web/assets")
|
return web.Assets()
|
||||||
return http.FS(f)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
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)
|
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
|
||||||
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
|
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
|
||||||
|
|
||||||
if err := compiledTemplates["index"].Execute(w, &IndexData{
|
if err := web.Template("index").Execute(w, &IndexData{
|
||||||
MainSite: utils.MainSite(r.Host),
|
MainSite: utils.MainSite(r.Host),
|
||||||
FileMaxSz: fMaxSzStr,
|
FileMaxSz: fMaxSzStr,
|
||||||
StorageCapacity: storCapacity,
|
StorageCapacity: storCapacity,
|
||||||
@ -132,7 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
if leftSpace < fHandler.Size {
|
if leftSpace < fHandler.Size {
|
||||||
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
|
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
if err := compiledTemplates["nospace"].Execute(w, &NotFoundData{
|
if err := web.Template("nospace").Execute(w, &NotFoundData{
|
||||||
MainSite: utils.MainSite(r.Host),
|
MainSite: utils.MainSite(r.Host),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
h.logErr.Fatalln("failed to execute NoSpace template:", err)
|
h.logErr.Fatalln("failed to execute NoSpace template:", err)
|
||||||
@ -200,7 +187,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := compiledTemplates["uploaded"].Execute(w, &UploadedData{
|
if err := web.Template("uploaded").Execute(w, &UploadedData{
|
||||||
MainSite: utils.MainSite(r.Host),
|
MainSite: utils.MainSite(r.Host),
|
||||||
DownloadURL: downloadURLParsed.String(),
|
DownloadURL: downloadURLParsed.String(),
|
||||||
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
|
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) {
|
func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
if err := compiledTemplates["404"].Execute(w, NotFoundData{
|
if err := web.Template("404").Execute(w, NotFoundData{
|
||||||
MainSite: utils.MainSite(r.Host),
|
MainSite: utils.MainSite(r.Host),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
h.logErr.Fatalln("failed to execute 404 template:", err)
|
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
79
web/web.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user