Refactor to use go:embedUnverified
ea7c0ab parent: ff7d85e modified
internal/server/server.go +4 -9 | @@ -8,7 +8,6 @@ import ( | ||
| 8 | 8 | "log/slog" |
| 9 | 9 | "net/http" |
| 10 | 10 | "net/url" |
| 11 | - "path/filepath" | |
| 12 | 11 | "strconv" |
| 13 | 12 | "strings" |
| 14 | 13 | "time" |
| @@ -27,6 +26,8 @@ import ( | ||
| 27 | 26 | "pkg.rbrt.fr/glean/internal/metrics" |
| 28 | 27 | "pkg.rbrt.fr/glean/internal/sanitize" |
| 29 | 28 | "pkg.rbrt.fr/glean/internal/scraper" |
| 29 | + "pkg.rbrt.fr/glean/static" | |
| 30 | + "pkg.rbrt.fr/glean/internal/tmpl" | |
| 30 | 31 | ) |
| 31 | 32 | |
| 32 | 33 | func splitString(s, sep string) []string { |
| @@ -182,7 +183,7 @@ func (s *Server) setupRoutes() { | ||
| 182 | 183 | s.router.Get("/xrpc/at.glean.getRecommendations", xrpc.GetRecommendations) |
| 183 | 184 | s.router.Get("/xrpc/at.glean.listFeedLists", xrpc.ListFeedLists) |
| 184 | 185 | |
| 185 | - s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) | |
| 186 | + s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(static.Files)))) | |
| 186 | 187 | s.router.Handle("/metrics", promhttp.Handler()) |
| 187 | 188 | } |
| 188 | 189 | |
| @@ -325,14 +326,8 @@ func (s *Server) loadTemplates() { | ||
| 325 | 326 | }, |
| 326 | 327 | } |
| 327 | 328 | |
| 328 | - var allFiles []string | |
| 329 | - matches, _ := filepath.Glob("internal/tmpl/*.html") | |
| 330 | - allFiles = append(allFiles, matches...) | |
| 331 | - partials, _ := filepath.Glob("internal/tmpl/partials/*.html") | |
| 332 | - allFiles = append(allFiles, partials...) | |
| 333 | - | |
| 334 | 329 | var err error |
| 335 | - s.templates, err = template.New("").Funcs(fm).ParseFiles(allFiles...) | |
| 330 | + s.templates, err = template.New("").Funcs(fm).ParseFS(tmpl.Files, "*.html", "partials/*.html") | |
| 336 | 331 | if err != nil { |
| 337 | 332 | s.logger.Error("failed to load templates", "error", err) |
| 338 | 333 | } |
| @@ -8,7 +8,6 @@ import ( | |||
| 8 | "log/slog" | 8 | "log/slog" |
| 9 | "net/http" | 9 | "net/http" |
| 10 | "net/url" | 10 | "net/url" |
| 11 | - "path/filepath" | ||
| 12 | "strconv" | 11 | "strconv" |
| 13 | "strings" | 12 | "strings" |
| 14 | "time" | 13 | "time" |
| @@ -27,6 +26,8 @@ import ( | |||
| 27 | "pkg.rbrt.fr/glean/internal/metrics" | 26 | "pkg.rbrt.fr/glean/internal/metrics" |
| 28 | "pkg.rbrt.fr/glean/internal/sanitize" | 27 | "pkg.rbrt.fr/glean/internal/sanitize" |
| 29 | "pkg.rbrt.fr/glean/internal/scraper" | 28 | "pkg.rbrt.fr/glean/internal/scraper" |
| 29 | + "pkg.rbrt.fr/glean/static" | ||
| 30 | + "pkg.rbrt.fr/glean/internal/tmpl" | ||
| 30 | ) | 31 | ) |
| 31 | 32 | ||
| 32 | func splitString(s, sep string) []string { | 33 | func splitString(s, sep string) []string { |
| @@ -182,7 +183,7 @@ func (s *Server) setupRoutes() { | |||
| 182 | s.router.Get("/xrpc/at.glean.getRecommendations", xrpc.GetRecommendations) | 183 | s.router.Get("/xrpc/at.glean.getRecommendations", xrpc.GetRecommendations) |
| 183 | s.router.Get("/xrpc/at.glean.listFeedLists", xrpc.ListFeedLists) | 184 | s.router.Get("/xrpc/at.glean.listFeedLists", xrpc.ListFeedLists) |
| 184 | 185 | ||
| 185 | - s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) | 186 | + s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(static.Files)))) |
| 186 | s.router.Handle("/metrics", promhttp.Handler()) | 187 | s.router.Handle("/metrics", promhttp.Handler()) |
| 187 | } | 188 | } |
| 188 | 189 | ||
| @@ -325,14 +326,8 @@ func (s *Server) loadTemplates() { | |||
| 325 | }, | 326 | }, |
| 326 | } | 327 | } |
| 327 | 328 | ||
| 328 | - var allFiles []string | ||
| 329 | - matches, _ := filepath.Glob("internal/tmpl/*.html") | ||
| 330 | - allFiles = append(allFiles, matches...) | ||
| 331 | - partials, _ := filepath.Glob("internal/tmpl/partials/*.html") | ||
| 332 | - allFiles = append(allFiles, partials...) | ||
| 333 | - | ||
| 334 | var err error | 329 | var err error |
| 335 | - s.templates, err = template.New("").Funcs(fm).ParseFiles(allFiles...) | 330 | + s.templates, err = template.New("").Funcs(fm).ParseFS(tmpl.Files, "*.html", "partials/*.html") |
| 336 | if err != nil { | 331 | if err != nil { |
| 337 | s.logger.Error("failed to load templates", "error", err) | 332 | s.logger.Error("failed to load templates", "error", err) |
| 338 | } | 333 | } |
added
internal/tmpl/embed.go +6 -0 | new file mode 100644 | ||
| @@ -0,0 +1,6 @@ | ||
| 1 | +package tmpl | |
| 2 | + | |
| 3 | +import "embed" | |
| 4 | + | |
| 5 | +//go:embed *.html partials/*.html | |
| 6 | +var Files embed.FS | |
| new file mode 100644 | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | +package tmpl | ||
| 2 | + | ||
| 3 | +import "embed" | ||
| 4 | + | ||
| 5 | +//go:embed *.html partials/*.html | ||
| 6 | +var Files embed.FS | ||
added
static/embed.go +6 -0 | new file mode 100644 | ||
| @@ -0,0 +1,6 @@ | ||
| 1 | +package static | |
| 2 | + | |
| 3 | +import "embed" | |
| 4 | + | |
| 5 | +//go:embed * | |
| 6 | +var Files embed.FS | |
| new file mode 100644 | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | +package static | ||
| 2 | + | ||
| 3 | +import "embed" | ||
| 4 | + | ||
| 5 | +//go:embed * | ||
| 6 | +var Files embed.FS | ||