Using embedded fs for static

main
Drew Bednar 2 months ago
parent c5aab74159
commit 83ac76795c

@ -6,6 +6,7 @@ import (
"net/http"
"git.runcible.io/learning/ratchet/internal/model"
"git.runcible.io/learning/ratchet/ui"
"github.com/alexedwards/scs/v2"
"github.com/go-playground/form/v4"
)
@ -31,11 +32,14 @@ func NewRatchetApp(logger *slog.Logger, tc *TemplateCache, db *sql.DB, sm *scs.S
rs.templateCache = tc
rs.sessionManager = sm
// TODO implement middleware that disables directory listings
fileServer := http.FileServer(http.Dir("./ui/static/"))
// This line was superceded by using the embedded filesystem
// fileServer := http.FileServer(http.Dir("./ui/static/"))
router := http.NewServeMux()
// Subtree pattern for static assets
router.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
// This line was superceded by using the embedded filesystem
// router.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
router.Handle("GET /static/", http.FileServerFS(ui.Files))
// Mux Router implements the Handler interface. AKA it has a ServeHTTP receiver.
// SEE we can really clean things up by moving this into routes.go and handlers.go

@ -0,0 +1,14 @@
package ui
import "embed"
// The below is actually a comment directive. Comment directives must be
// placed immediately above the variable used. The path provided is relative
// to the .go file it is in. You can only embed on global variables at a package
// level. Paths cannot contain . or .. or begin with / So you are effectively
// restricted to embedding files within the same directory as the .go file.
// Lastly the embedded file system is always rooted in the directory that contains
// the embed directive. So in this example the root is in our ui dir.
//go:embed "static"
var Files embed.FS
Loading…
Cancel
Save