Template
1
0
Fork 0

Go Embed!

This commit is contained in:
Atridad Lahiji 2024-02-08 19:30:22 -07:00
parent 74516d3719
commit 014fb3a35c
No known key found for this signature in database
8 changed files with 63 additions and 36 deletions

View file

@ -10,8 +10,5 @@ RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /go/bin/app
FROM gcr.io/distroless/base-debian12 FROM gcr.io/distroless/base-debian12
COPY --from=build /go/bin/app / 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" ] CMD [ "/app" ]

6
content/contentfs.go Normal file
View file

@ -0,0 +1,6 @@
package contentfs
import "embed"
//go:embed *
var FS embed.FS

View file

@ -5,7 +5,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"os" "io/fs"
"strings" "strings"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
@ -18,8 +18,8 @@ type FrontMatter struct {
Tags []string Tags []string
} }
func ExtractFrontMatter(file os.DirEntry, dir string) (CardLink, error) { func ExtractFrontMatter(file fs.DirEntry, contentFS fs.FS) (CardLink, error) {
f, err := os.Open(dir + file.Name()) f, err := contentFS.Open(file.Name())
if err != nil { if err != nil {
return CardLink{}, fmt.Errorf("failed to open file: %w", err) return CardLink{}, fmt.Errorf("failed to open file: %w", err)
} }

View file

@ -6,6 +6,10 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"runtime" "runtime"
templatefs "goth.stack/pages/templates"
) )
func RenderTemplate(w http.ResponseWriter, layout string, partials []string, props interface{}) error { 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 // Build the list of templates
templates := []string{ templates := []string{
"./pages/templates/layouts/" + layout + ".html", "layouts/" + layout + ".html",
"./pages/templates/" + page + ".html", page + ".html",
} }
for _, partial := range partials { for _, partial := range partials {
templates = append(templates, "./pages/templates/partials/"+partial+".html") templates = append(templates, "partials/"+partial+".html")
} }
// Parse the templates // Parse the templates
ts, err := template.ParseFiles(templates...) ts, err := template.ParseFS(templatefs.FS, templates...)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
return err return err

View file

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"embed"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -18,6 +19,9 @@ import (
"goth.stack/pages" "goth.stack/pages"
) )
//go:embed public/*
var PublicFS embed.FS
func main() { func main() {
// Load environment variables // Load environment variables
godotenv.Load(".env") godotenv.Load(".env")
@ -55,7 +59,8 @@ func main() {
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50))) e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
// Static server // Static server
e.Static("/public", "public") fs := http.FS(PublicFS)
e.GET("/public/*", echo.WrapHandler(http.FileServer(fs)))
// Page routes // Page routes
e.GET("/", pages.Home) e.GET("/", pages.Home)

View file

@ -1,14 +1,15 @@
package pages package pages
import ( import (
"io/fs"
"log" "log"
"net/http" "net/http"
"os"
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
contentfs "goth.stack/content"
"goth.stack/lib" "goth.stack/lib"
) )
@ -19,23 +20,27 @@ type BlogProps struct {
func Blog(c echo.Context) error { func Blog(c echo.Context) error {
var posts []lib.CardLink var posts []lib.CardLink
files, err := os.ReadDir("./content/") files, err := fs.ReadDir(contentfs.FS, ".")
if err != nil { if err != nil {
log.Println(err)
http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError) http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError)
return nil return nil
} }
for _, file := range files { for _, file := range files {
frontMatter, err := lib.ExtractFrontMatter(file, "./content/") if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") {
if err != nil { frontMatter, err := lib.ExtractFrontMatter(file, contentfs.FS)
http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) if err != nil {
return 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" const layout = "January 2 2006"

View file

@ -1,18 +1,19 @@
package pages package pages
import ( import (
"io/fs"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
"github.com/gorilla/feeds" "github.com/gorilla/feeds"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
contentfs "goth.stack/content"
"goth.stack/lib" "goth.stack/lib"
) )
func RSSFeedHandler(c echo.Context) error { func RSSFeedHandler(c echo.Context) error {
files, err := os.ReadDir("./content/") files, err := fs.ReadDir(contentfs.FS, ".")
protocol := "http" protocol := "http"
if c.Request().TLS != nil { if c.Request().TLS != nil {
@ -30,19 +31,22 @@ func RSSFeedHandler(c echo.Context) error {
} }
for _, file := range files { for _, file := range files {
frontMatter, err := lib.ExtractFrontMatter(file, "./content/") if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") {
if err != nil {
http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError) frontMatter, err := lib.ExtractFrontMatter(file, contentfs.FS)
return nil 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() rss, _ := feed.ToRss()

View file

@ -0,0 +1,6 @@
package templatefs
import "embed"
//go:embed *
var FS embed.FS