1
0

Jade can work with embed FS, so we do that.

This commit is contained in:
Alexander Andreev 2022-02-08 17:46:44 +04:00
parent 94437e3bab
commit 381a0dcc98
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -6,11 +6,13 @@ 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"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -19,10 +21,14 @@ import (
"github.com/Joker/jade" "github.com/Joker/jade"
) )
//var defaultAmberOptions = amber.Options{PrettyPrint: false, LineNumbers: false}
var compiledTemplates map[string]*template.Template 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
} }
@ -64,6 +70,11 @@ func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logg
uploadDirSize: uploadDirSize} uploadDirSize: uploadDirSize}
} }
func (h *UploadHandlers) AssetsFS() http.FileSystem {
f, _ := fs.Sub(assetsDir, "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) {
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20 var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20 var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20
@ -216,7 +227,10 @@ func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) {
func compileTemplates(dir string, lErr *logging.Logger) { func compileTemplates(dir string, lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template) compiledTemplates = make(map[string]*template.Template)
indexStr, err := jade.ParseFile(path.Join(dir, "index.jade")) t, _ := fs.Sub(templatesDir, "web/templates")
templatesFS := http.FS(t)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS)
if err != nil { if err != nil {
lErr.Fatalln(err) lErr.Fatalln(err)
} }
@ -228,7 +242,7 @@ func compileTemplates(dir string, lErr *logging.Logger) {
compiledTemplates["index"] = indexTpl compiledTemplates["index"] = indexTpl
uploadedStr, err := jade.ParseFile(path.Join(dir, "uploaded.jade")) uploadedStr, err := jade.ParseFileFromFileSystem("uploaded.jade", templatesFS)
if err != nil { if err != nil {
lErr.Fatalln(err) lErr.Fatalln(err)
} }
@ -240,7 +254,7 @@ func compileTemplates(dir string, lErr *logging.Logger) {
compiledTemplates["uploaded"] = uploadedTpl compiledTemplates["uploaded"] = uploadedTpl
notfoundStr, err := jade.ParseFile(path.Join(dir, "404.jade")) notfoundStr, err := jade.ParseFileFromFileSystem("404.jade", templatesFS)
if err != nil { if err != nil {
lErr.Fatalln(err) lErr.Fatalln(err)
} }