[chore] embed filesystem
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-28 22:01:49 -05:00
parent 756db7a493
commit a34906c266
7 changed files with 43 additions and 37 deletions

View File

@@ -2,8 +2,10 @@ package api
import (
"crypto/rand"
"embed"
"fmt"
"html/template"
"io/ioutil"
"io/fs"
"net/http"
"path/filepath"
"strings"
@@ -23,18 +25,21 @@ type API struct {
Config *config.Config
DB *database.DBManager
HTMLPolicy *bluemonday.Policy
Assets *embed.FS
}
func NewApi(db *database.DBManager, c *config.Config) *API {
func NewApi(db *database.DBManager, c *config.Config, assets embed.FS) *API {
api := &API{
HTMLPolicy: bluemonday.StrictPolicy(),
Router: gin.Default(),
Config: c,
DB: db,
Assets: &assets,
}
// Assets & Web App Templates
api.Router.Static("/assets", "./assets")
assetsDir, _ := fs.Sub(assets, "assets")
api.Router.StaticFS("/assets", http.FS(assetsDir))
// Generate Secure Token
var newToken []byte
@@ -172,37 +177,40 @@ func (api *API) generateTemplates() *multitemplate.Renderer {
}
// Load Base
b, _ := ioutil.ReadFile("./templates/base.html")
b, _ := api.Assets.ReadFile("templates/base.html")
baseTemplate := template.Must(template.New("base").Funcs(helperFuncs).Parse(string(b)))
// Load SVGs
svgs, _ := filepath.Glob("./templates/svgs/*")
for _, path := range svgs {
basename := filepath.Base(path)
svgs, _ := api.Assets.ReadDir("templates/svgs")
for _, item := range svgs {
basename := item.Name()
path := fmt.Sprintf("templates/svgs/%s", basename)
name := strings.TrimSuffix(basename, filepath.Ext(basename))
b, _ := ioutil.ReadFile(path)
b, _ := api.Assets.ReadFile(path)
baseTemplate = template.Must(baseTemplate.New("svg/" + name).Parse(string(b)))
}
// Load Components
components, _ := filepath.Glob("./templates/components/*")
for _, path := range components {
basename := filepath.Base(path)
components, _ := api.Assets.ReadDir("templates/components")
for _, item := range components {
basename := item.Name()
path := fmt.Sprintf("templates/components/%s", basename)
name := strings.TrimSuffix(basename, filepath.Ext(basename))
b, _ := ioutil.ReadFile(path)
b, _ := api.Assets.ReadFile(path)
baseTemplate = template.Must(baseTemplate.New("component/" + name).Parse(string(b)))
}
// Load Pages
pages, _ := filepath.Glob("./templates/pages/*")
for _, path := range pages {
basename := filepath.Base(path)
pages, _ := api.Assets.ReadDir("templates/pages")
for _, item := range pages {
basename := item.Name()
path := fmt.Sprintf("templates/pages/%s", basename)
name := strings.TrimSuffix(basename, filepath.Ext(basename))
// Clone Base Template
b, _ := ioutil.ReadFile(path)
b, _ := api.Assets.ReadFile(path)
pageTemplate, _ := template.Must(baseTemplate.Clone()).New("page/" + name).Parse(string(b))
render.Add("page/"+name, pageTemplate)
}

View File

@@ -72,23 +72,23 @@ type requestDocumentAdd struct {
func (api *API) webManifest(c *gin.Context) {
c.Header("Content-Type", "application/manifest+json")
c.File("./assets/manifest.json")
c.FileFromFS("./assets/manifest.json", http.FS(api.Assets))
}
func (api *API) serviceWorker(c *gin.Context) {
c.File("./assets/sw.js")
c.FileFromFS("./assets/sw.js", http.FS(api.Assets))
}
func (api *API) faviconIcon(c *gin.Context) {
c.File("./assets/icons/favicon.ico")
c.FileFromFS("./assets/icons/favicon.ico", http.FS(api.Assets))
}
func (api *API) localDocuments(c *gin.Context) {
c.File("./assets/local/index.html")
c.FileFromFS("./assets/local/index.html", http.FS(api.Assets))
}
func (api *API) documentReader(c *gin.Context) {
c.File("./assets/reader/index.html")
c.FileFromFS("./assets/reader/index.html", http.FS(api.Assets))
}
func (api *API) createAppResourcesRoute(routeName string, args ...map[string]any) func(*gin.Context) {
@@ -284,7 +284,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
// Handle Identified Document
if document.Coverfile != nil {
if *document.Coverfile == "UNKNOWN" {
c.File("./assets/images/no-cover.jpg")
c.FileFromFS("./assets/images/no-cover.jpg", http.FS(api.Assets))
return
}
@@ -295,7 +295,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
_, err = os.Stat(safePath)
if err != nil {
log.Error("[getDocumentCover] File Should But Doesn't Exist:", err)
c.File("./assets/images/no-cover.jpg")
c.FileFromFS("./assets/images/no-cover.jpg", http.FS(api.Assets))
return
}
@@ -348,7 +348,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
// Return Unknown Cover
if coverFile == "UNKNOWN" {
c.File("./assets/images/no-cover.jpg")
c.FileFromFS("./assets/images/no-cover.jpg", http.FS(api.Assets))
return
}