Go Embed!
This commit is contained in:
parent
74516d3719
commit
014fb3a35c
8 changed files with 63 additions and 36 deletions
|
@ -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" ]
|
||||
|
|
6
content/contentfs.go
Normal file
6
content/contentfs.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package contentfs
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *
|
||||
var FS embed.FS
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
7
main.go
7
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)
|
||||
|
|
|
@ -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,15 +20,18 @@ 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 !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
|
||||
}
|
||||
|
@ -37,6 +41,7 @@ func Blog(c echo.Context) error {
|
|||
|
||||
posts = append(posts, frontMatter)
|
||||
}
|
||||
}
|
||||
|
||||
const layout = "January 2 2006"
|
||||
|
||||
|
|
10
pages/rss.go
10
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,7 +31,9 @@ func RSSFeedHandler(c echo.Context) error {
|
|||
}
|
||||
|
||||
for _, file := range files {
|
||||
frontMatter, err := lib.ExtractFrontMatter(file, "./content/")
|
||||
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
|
||||
|
@ -44,6 +47,7 @@ func RSSFeedHandler(c echo.Context) error {
|
|||
Created: date,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
rss, _ := feed.ToRss()
|
||||
return c.Blob(http.StatusOK, "application/rss+xml", []byte(rss))
|
||||
|
|
6
pages/templates/templatesfs.go
Normal file
6
pages/templates/templatesfs.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package templatefs
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *
|
||||
var FS embed.FS
|
Loading…
Add table
Reference in a new issue