From 014fb3a35c7d857f20f5bcaf8ecadab79ea44057 Mon Sep 17 00:00:00 2001 From: atridadl Date: Thu, 8 Feb 2024 19:30:22 -0700 Subject: [PATCH] Go Embed! --- Dockerfile | 3 --- content/contentfs.go | 6 ++++++ lib/markdown.go | 6 +++--- lib/templates.go | 12 ++++++++---- main.go | 7 ++++++- pages/blog.go | 27 ++++++++++++++++----------- pages/rss.go | 32 ++++++++++++++++++-------------- pages/templates/templatesfs.go | 6 ++++++ 8 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 content/contentfs.go create mode 100644 pages/templates/templatesfs.go diff --git a/Dockerfile b/Dockerfile index 680c40f..3f79204 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,8 +10,5 @@ RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /go/bin/app FROM gcr.io/distroless/base-debian12 COPY --from=build /go/bin/app / -COPY --from=build /app/content /content -COPY --from=build /app/pages /pages -COPY --from=build /app/public /public CMD [ "/app" ] diff --git a/content/contentfs.go b/content/contentfs.go new file mode 100644 index 0000000..a306e28 --- /dev/null +++ b/content/contentfs.go @@ -0,0 +1,6 @@ +package contentfs + +import "embed" + +//go:embed * +var FS embed.FS diff --git a/lib/markdown.go b/lib/markdown.go index 9221752..afe4f72 100644 --- a/lib/markdown.go +++ b/lib/markdown.go @@ -5,7 +5,7 @@ import ( "bytes" "errors" "fmt" - "os" + "io/fs" "strings" "github.com/yuin/goldmark" @@ -18,8 +18,8 @@ type FrontMatter struct { Tags []string } -func ExtractFrontMatter(file os.DirEntry, dir string) (CardLink, error) { - f, err := os.Open(dir + file.Name()) +func ExtractFrontMatter(file fs.DirEntry, contentFS fs.FS) (CardLink, error) { + f, err := contentFS.Open(file.Name()) if err != nil { return CardLink{}, fmt.Errorf("failed to open file: %w", err) } diff --git a/lib/templates.go b/lib/templates.go index 677ae87..4d6ea59 100644 --- a/lib/templates.go +++ b/lib/templates.go @@ -6,6 +6,10 @@ import ( "net/http" "path/filepath" "runtime" + + templatefs "goth.stack/pages/templates" + + ) func RenderTemplate(w http.ResponseWriter, layout string, partials []string, props interface{}) error { @@ -16,15 +20,15 @@ func RenderTemplate(w http.ResponseWriter, layout string, partials []string, pro // Build the list of templates templates := []string{ - "./pages/templates/layouts/" + layout + ".html", - "./pages/templates/" + page + ".html", + "layouts/" + layout + ".html", + page + ".html", } for _, partial := range partials { - templates = append(templates, "./pages/templates/partials/"+partial+".html") + templates = append(templates, "partials/"+partial+".html") } // Parse the templates - ts, err := template.ParseFiles(templates...) + ts, err := template.ParseFS(templatefs.FS, templates...) if err != nil { log.Print(err.Error()) return err diff --git a/main.go b/main.go index d0c6dd4..1aef017 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "embed" "flag" "fmt" "log" @@ -18,6 +19,9 @@ import ( "goth.stack/pages" ) +//go:embed public/* +var PublicFS embed.FS + func main() { // Load environment variables godotenv.Load(".env") @@ -55,7 +59,8 @@ func main() { e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50))) // Static server - e.Static("/public", "public") + fs := http.FS(PublicFS) + e.GET("/public/*", echo.WrapHandler(http.FileServer(fs))) // Page routes e.GET("/", pages.Home) diff --git a/pages/blog.go b/pages/blog.go index 2478711..d928ea6 100644 --- a/pages/blog.go +++ b/pages/blog.go @@ -1,14 +1,15 @@ package pages import ( + "io/fs" "log" "net/http" - "os" "sort" "strings" "time" "github.com/labstack/echo/v4" + contentfs "goth.stack/content" "goth.stack/lib" ) @@ -19,23 +20,27 @@ type BlogProps struct { func Blog(c echo.Context) error { var posts []lib.CardLink - files, err := os.ReadDir("./content/") + files, err := fs.ReadDir(contentfs.FS, ".") if err != nil { + log.Println(err) http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError) return nil } for _, file := range files { - frontMatter, err := lib.ExtractFrontMatter(file, "./content/") - if err != nil { - http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) - return nil + if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") { + frontMatter, err := lib.ExtractFrontMatter(file, contentfs.FS) + if err != nil { + log.Println(err) + http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) + return nil + } + + frontMatter.Href = "post/" + strings.TrimSuffix(file.Name(), ".md") + frontMatter.Internal = true + + posts = append(posts, frontMatter) } - - frontMatter.Href = "post/" + strings.TrimSuffix(file.Name(), ".md") - frontMatter.Internal = true - - posts = append(posts, frontMatter) } const layout = "January 2 2006" diff --git a/pages/rss.go b/pages/rss.go index 3075bea..fd9652a 100644 --- a/pages/rss.go +++ b/pages/rss.go @@ -1,18 +1,19 @@ package pages import ( + "io/fs" "net/http" - "os" "strings" "time" "github.com/gorilla/feeds" "github.com/labstack/echo/v4" + contentfs "goth.stack/content" "goth.stack/lib" ) func RSSFeedHandler(c echo.Context) error { - files, err := os.ReadDir("./content/") + files, err := fs.ReadDir(contentfs.FS, ".") protocol := "http" if c.Request().TLS != nil { @@ -30,19 +31,22 @@ func RSSFeedHandler(c echo.Context) error { } for _, file := range files { - frontMatter, err := lib.ExtractFrontMatter(file, "./content/") - if err != nil { - http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) - return nil + if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") { + + frontMatter, err := lib.ExtractFrontMatter(file, contentfs.FS) + if err != nil { + http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) + return nil + } + + date, _ := time.Parse("January 2 2006", frontMatter.Date) + + feed.Add(&feeds.Item{ + Title: frontMatter.Name, + Link: &feeds.Link{Href: protocol + "://" + c.Request().Host + "/post/" + strings.TrimSuffix(file.Name(), ".md")}, + Created: date, + }) } - - date, _ := time.Parse("January 2 2006", frontMatter.Date) - - feed.Add(&feeds.Item{ - Title: frontMatter.Name, - Link: &feeds.Link{Href: protocol + "://" + c.Request().Host + "/post/" + strings.TrimSuffix(file.Name(), ".md")}, - Created: date, - }) } rss, _ := feed.ToRss() diff --git a/pages/templates/templatesfs.go b/pages/templates/templatesfs.go new file mode 100644 index 0000000..59353f0 --- /dev/null +++ b/pages/templates/templatesfs.go @@ -0,0 +1,6 @@ +package templatefs + +import "embed" + +//go:embed * +var FS embed.FS