[chore] embed filesystem
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
756db7a493
commit
a34906c266
@ -13,9 +13,7 @@ COPY . .
|
|||||||
RUN mkdir -p /opt/antholume
|
RUN mkdir -p /opt/antholume
|
||||||
|
|
||||||
# Compile
|
# Compile
|
||||||
RUN go build -o /opt/antholume/server; \
|
RUN go build -o /opt/antholume/server
|
||||||
cp -a ./templates /opt/antholume/templates; \
|
|
||||||
cp -a ./assets /opt/antholume/assets;
|
|
||||||
|
|
||||||
# Create Image
|
# Create Image
|
||||||
FROM busybox:1.36
|
FROM busybox:1.36
|
||||||
|
@ -15,9 +15,7 @@ ARG TARGETARCH
|
|||||||
RUN --mount=target=. \
|
RUN --mount=target=. \
|
||||||
--mount=type=cache,target=/root/.cache/go-build \
|
--mount=type=cache,target=/root/.cache/go-build \
|
||||||
--mount=type=cache,target=/go/pkg \
|
--mount=type=cache,target=/go/pkg \
|
||||||
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /opt/antholume/server; \
|
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /opt/antholume/server
|
||||||
cp -a ./templates /opt/antholume/templates; \
|
|
||||||
cp -a ./assets /opt/antholume/assets;
|
|
||||||
|
|
||||||
# Create Image
|
# Create Image
|
||||||
FROM busybox:1.36
|
FROM busybox:1.36
|
||||||
|
5
Makefile
5
Makefile
@ -1,9 +1,7 @@
|
|||||||
build_local: build_tailwind
|
build_local: build_tailwind
|
||||||
go mod download
|
go mod download
|
||||||
rm -r ./build
|
rm -r ./build || true
|
||||||
mkdir -p ./build
|
mkdir -p ./build
|
||||||
cp -a ./templates ./build/templates
|
|
||||||
cp -a ./assets ./build/assets
|
|
||||||
|
|
||||||
env GOOS=linux GOARCH=amd64 go build -o ./build/server_linux_amd64
|
env GOOS=linux GOARCH=amd64 go build -o ./build/server_linux_amd64
|
||||||
env GOOS=linux GOARCH=arm64 go build -o ./build/server_linux_arm64
|
env GOOS=linux GOARCH=arm64 go build -o ./build/server_linux_arm64
|
||||||
@ -31,7 +29,6 @@ docker_build_release_latest: build_tailwind
|
|||||||
build_tailwind:
|
build_tailwind:
|
||||||
tailwind build -o ./assets/style.css --minify
|
tailwind build -o ./assets/style.css --minify
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf ./build
|
rm -rf ./build
|
||||||
|
|
||||||
|
40
api/api.go
40
api/api.go
@ -2,8 +2,10 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -23,18 +25,21 @@ type API struct {
|
|||||||
Config *config.Config
|
Config *config.Config
|
||||||
DB *database.DBManager
|
DB *database.DBManager
|
||||||
HTMLPolicy *bluemonday.Policy
|
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{
|
api := &API{
|
||||||
HTMLPolicy: bluemonday.StrictPolicy(),
|
HTMLPolicy: bluemonday.StrictPolicy(),
|
||||||
Router: gin.Default(),
|
Router: gin.Default(),
|
||||||
Config: c,
|
Config: c,
|
||||||
DB: db,
|
DB: db,
|
||||||
|
Assets: &assets,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assets & Web App Templates
|
// Assets & Web App Templates
|
||||||
api.Router.Static("/assets", "./assets")
|
assetsDir, _ := fs.Sub(assets, "assets")
|
||||||
|
api.Router.StaticFS("/assets", http.FS(assetsDir))
|
||||||
|
|
||||||
// Generate Secure Token
|
// Generate Secure Token
|
||||||
var newToken []byte
|
var newToken []byte
|
||||||
@ -172,37 +177,40 @@ func (api *API) generateTemplates() *multitemplate.Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load Base
|
// 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)))
|
baseTemplate := template.Must(template.New("base").Funcs(helperFuncs).Parse(string(b)))
|
||||||
|
|
||||||
// Load SVGs
|
// Load SVGs
|
||||||
svgs, _ := filepath.Glob("./templates/svgs/*")
|
svgs, _ := api.Assets.ReadDir("templates/svgs")
|
||||||
for _, path := range svgs {
|
for _, item := range svgs {
|
||||||
basename := filepath.Base(path)
|
basename := item.Name()
|
||||||
|
path := fmt.Sprintf("templates/svgs/%s", basename)
|
||||||
name := strings.TrimSuffix(basename, filepath.Ext(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)))
|
baseTemplate = template.Must(baseTemplate.New("svg/" + name).Parse(string(b)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Components
|
// Load Components
|
||||||
components, _ := filepath.Glob("./templates/components/*")
|
components, _ := api.Assets.ReadDir("templates/components")
|
||||||
for _, path := range components {
|
for _, item := range components {
|
||||||
basename := filepath.Base(path)
|
basename := item.Name()
|
||||||
|
path := fmt.Sprintf("templates/components/%s", basename)
|
||||||
name := strings.TrimSuffix(basename, filepath.Ext(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)))
|
baseTemplate = template.Must(baseTemplate.New("component/" + name).Parse(string(b)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Pages
|
// Load Pages
|
||||||
pages, _ := filepath.Glob("./templates/pages/*")
|
pages, _ := api.Assets.ReadDir("templates/pages")
|
||||||
for _, path := range pages {
|
for _, item := range pages {
|
||||||
basename := filepath.Base(path)
|
basename := item.Name()
|
||||||
|
path := fmt.Sprintf("templates/pages/%s", basename)
|
||||||
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
||||||
|
|
||||||
// Clone Base Template
|
// Clone Base Template
|
||||||
b, _ := ioutil.ReadFile(path)
|
b, _ := api.Assets.ReadFile(path)
|
||||||
pageTemplate, _ := template.Must(baseTemplate.Clone()).New("page/" + name).Parse(string(b))
|
pageTemplate, _ := template.Must(baseTemplate.Clone()).New("page/" + name).Parse(string(b))
|
||||||
render.Add("page/"+name, pageTemplate)
|
render.Add("page/"+name, pageTemplate)
|
||||||
}
|
}
|
||||||
|
@ -72,23 +72,23 @@ type requestDocumentAdd struct {
|
|||||||
|
|
||||||
func (api *API) webManifest(c *gin.Context) {
|
func (api *API) webManifest(c *gin.Context) {
|
||||||
c.Header("Content-Type", "application/manifest+json")
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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
|
// Handle Identified Document
|
||||||
if document.Coverfile != nil {
|
if document.Coverfile != nil {
|
||||||
if *document.Coverfile == "UNKNOWN" {
|
if *document.Coverfile == "UNKNOWN" {
|
||||||
c.File("./assets/images/no-cover.jpg")
|
c.FileFromFS("./assets/images/no-cover.jpg", http.FS(api.Assets))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,7 +295,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
_, err = os.Stat(safePath)
|
_, err = os.Stat(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[getDocumentCover] File Should But Doesn't Exist:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,7 +348,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
|
|
||||||
// Return Unknown Cover
|
// Return Unknown Cover
|
||||||
if coverFile == "UNKNOWN" {
|
if coverFile == "UNKNOWN" {
|
||||||
c.File("./assets/images/no-cover.jpg")
|
c.FileFromFS("./assets/images/no-cover.jpg", http.FS(api.Assets))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
main.go
6
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
@ -11,6 +12,9 @@ import (
|
|||||||
"reichard.io/bbank/server"
|
"reichard.io/bbank/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/* assets/*
|
||||||
|
var assets embed.FS
|
||||||
|
|
||||||
type UTCFormatter struct {
|
type UTCFormatter struct {
|
||||||
log.Formatter
|
log.Formatter
|
||||||
}
|
}
|
||||||
@ -51,7 +55,7 @@ func cmdServer(ctx *cli.Context) error {
|
|||||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
// Start Server
|
// Start Server
|
||||||
server := server.NewServer()
|
server := server.NewServer(assets)
|
||||||
server.StartServer(&wg, done)
|
server.StartServer(&wg, done)
|
||||||
|
|
||||||
// Wait & Close
|
// Wait & Close
|
||||||
|
@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -22,10 +23,10 @@ type Server struct {
|
|||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() *Server {
|
func NewServer(assets embed.FS) *Server {
|
||||||
c := config.Load()
|
c := config.Load()
|
||||||
db := database.NewMgr(c)
|
db := database.NewMgr(c)
|
||||||
api := api.NewApi(db, c)
|
api := api.NewApi(db, c, assets)
|
||||||
|
|
||||||
// Create Paths
|
// Create Paths
|
||||||
docDir := filepath.Join(c.DataPath, "documents")
|
docDir := filepath.Join(c.DataPath, "documents")
|
||||||
|
Loading…
Reference in New Issue
Block a user