Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 53df9aa

Browse files
committed
embed static files and templates for CLI to server web interface fix #65
1 parent 1fd11ff commit 53df9aa

File tree

7 files changed

+11801
-10
lines changed

7 files changed

+11801
-10
lines changed

render.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"html/template"
66
"net/http"
7-
"os"
87
"path/filepath"
98
"strconv"
109
"strings"
@@ -19,33 +18,33 @@ var (
1918

2019
func loadTemplates() error {
2120
var partials []string
22-
entries, err := os.ReadDir("./templates/partials")
21+
entries, err := content.ReadDir("templates/partials")
2322
if err != nil {
2423
return err
2524
}
2625

2726
funcs := customFuncs()
2827

2928
for _, e := range entries {
30-
partials = append(partials, fmt.Sprintf("./templates/partials/%s", e.Name()))
29+
partials = append(partials, fmt.Sprintf("templates/partials/%s", e.Name()))
3130
}
3231

3332
views = make(map[string]*template.Template)
3433

35-
tmpls, err := os.ReadDir("./templates")
34+
tmpls, err := content.ReadDir("templates")
3635
if err != nil {
3736
return err
3837
}
3938

4039
for _, tmpl := range tmpls {
41-
name := fmt.Sprintf("./templates/%s", tmpl.Name())
40+
name := fmt.Sprintf("templates/%s", tmpl.Name())
4241
if !strings.HasSuffix(name, ".html") {
4342
continue
4443
}
4544

4645
cur := append([]string{name}, partials...)
4746

48-
t, err := template.New(tmpl.Name()).Funcs(funcs).ParseFiles(cur...)
47+
t, err := template.New(tmpl.Name()).Funcs(funcs).ParseFS(content, cur...)
4948
if err != nil {
5049
return err
5150
}

server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package staticbackend
22

33
import (
44
"context"
5+
"embed"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -30,6 +31,9 @@ const (
3031
AppEnvProd = "prod"
3132
)
3233

34+
//go:embed static templates
35+
var content embed.FS
36+
3337
// Start starts the web server and all dependencies services
3438
func Start(c config.AppConfig, log *logger.Logger) {
3539
log.Info().Str("Addr", c.AppURL).Msg("server started")
@@ -121,6 +125,9 @@ func Start(c config.AppConfig, log *logger.Logger) {
121125
middleware.RequireRoot(backend.DB, backend.Cache),
122126
}
123127

128+
// static assets
129+
http.Handle("/static/", http.StripPrefix("/", http.FileServer(http.FS(content))))
130+
124131
m := &membership{log: log}
125132

126133
http.Handle("/login/magic", middleware.Chain(http.HandlerFunc(m.magicLink), pubWithDB...))

0 commit comments

Comments
 (0)