Skip to content

Commit

Permalink
feat: use embededfs
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Jul 17, 2024
1 parent aea556c commit acae0ec
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 81 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions lib/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package lib

import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"os"
"sort"
"strings"
"time"
Expand All @@ -17,17 +18,20 @@ import (
"github.com/yuin/goldmark/renderer/html"
)

//go:embed content/*
var content embed.FS

func GetBlogPosts() Posts {
var posts Posts

files, err := os.ReadDir(GetPath("/content/"))
files, err := fs.ReadDir(content, "content")
if err != nil {
log.Fatalf("failed to open file: %v", err)
}

for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
content, err := os.ReadFile(GetPath("/content/") + file.Name())
content, err := fs.ReadFile(content, "content/"+file.Name())
if err != nil {
log.Fatal(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions lib/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
_ "github.com/joho/godotenv/autoload"
)

// Default settings
var (
RootDir string = "."
)

func GetPath(path string) string {
return RootDir + path
}

func GetServePath(path string) string {
return os.Getenv("SERVE_DIR") + path
}
38 changes: 0 additions & 38 deletions lib/templates.go

This file was deleted.

12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"embed"
"fmt"
"io/fs"
"net/http"
"strings"

Expand Down Expand Up @@ -30,6 +32,9 @@ func customHTTPErrorHandler(err error, c echo.Context) {
}
}

//go:embed public/*
var PubFS embed.FS

func main() {
e := echo.New()

Expand Down Expand Up @@ -75,7 +80,12 @@ func main() {
return c.JSON(http.StatusOK, json)
})

e.Static("/*", lib.GetPath("/public"))
strippedFS, err := fs.Sub(PubFS, "public")
if err != nil {
panic(err)
}
fs := http.FS(strippedFS)
e.GET("/*", echo.WrapHandler(http.FileServer(fs)))

e.Logger.Fatal(e.Start(":3000"))
}
20 changes: 2 additions & 18 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ buildGoModule {
../go.mod
../go.sum
../main.go
../lib
../content
../api
../lib
../pages
../public
../templates
../pages
]
);
};
Expand All @@ -27,21 +26,6 @@ buildGoModule {
"-w"
];

# we change the rootdir so that the templates and other files are loaded from the right place
# TODO: we should change this so that it uses an emebeded filesystem at some point
preBuild = ''
substituteInPlace lib/settings.go \
--replace-fail 'RootDir string = "."' 'RootDir string = "'$out/share'"'
'';

postInstall = ''
mkdir -p $out/share
cp -r content $out/share/content
cp -r public $out/share/public
cp -r templates $out/share/templates
'';

meta = {
description = "isabelroses.com";
homepage = "https://isabelroses.com";
Expand Down
3 changes: 2 additions & 1 deletion pages/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/labstack/echo/v4"

"isabelroses.com/lib"
"isabelroses.com/templates"
)

type BlogProps struct {
Expand All @@ -29,5 +30,5 @@ func Blog(c echo.Context) error {
}

components := []string{"header", "blogpreview"}
return lib.RenderTemplate(c.Response().Writer, "base", components, props)
return templates.RenderTemplate(c.Response().Writer, "base", components, props)
}
4 changes: 3 additions & 1 deletion pages/donos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"strings"

"github.com/labstack/echo/v4"

"isabelroses.com/lib"
"isabelroses.com/templates"
)

type DonoProps struct {
Expand All @@ -28,7 +30,7 @@ func Donos(c echo.Context) error {
}

components := []string{"header", "usercard"}
return lib.RenderTemplate(c.Response().Writer, "base", components, props)
return templates.RenderTemplate(c.Response().Writer, "base", components, props)
}

func handleAvatar(dono lib.Donor) lib.Donor {
Expand Down
5 changes: 2 additions & 3 deletions pages/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/labstack/echo/v4"

"isabelroses.com/lib"
"isabelroses.com/templates"
)

type ErrorProps struct {
Expand All @@ -19,6 +19,5 @@ func ErrorPage(c echo.Context, code int) error {

c.Response().WriteHeader(code)
components := []string{"header"}
return lib.RenderTemplate(c.Response().Writer, "base", components, props)

return templates.RenderTemplate(c.Response().Writer, "base", components, props)
}
4 changes: 2 additions & 2 deletions pages/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package pages
import (
"github.com/labstack/echo/v4"

"isabelroses.com/lib"
"isabelroses.com/templates"
)

func Home(c echo.Context) error {
components := []string{"header"}
return lib.RenderTemplate(c.Response().Writer, "base", components, nil)
return templates.RenderTemplate(c.Response().Writer, "base", components, nil)
}
3 changes: 2 additions & 1 deletion pages/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/labstack/echo/v4"
"isabelroses.com/lib"
"isabelroses.com/templates"
)

func Post(c echo.Context) error {
Expand Down Expand Up @@ -45,5 +46,5 @@ func Post(c echo.Context) error {
}

components := []string{"header"}
return lib.RenderTemplate(c.Response().Writer, "base", components, props)
return templates.RenderTemplate(c.Response().Writer, "base", components, props)
}
3 changes: 2 additions & 1 deletion pages/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pages
import (
"github.com/labstack/echo/v4"
"isabelroses.com/lib"
"isabelroses.com/templates"
)

type ProjectProps struct {
Expand Down Expand Up @@ -54,5 +55,5 @@ func Projects(c echo.Context) error {
}

components := []string{"header", "project"}
return lib.RenderTemplate(c.Response().Writer, "base", components, props)
return templates.RenderTemplate(c.Response().Writer, "base", components, props)
}
55 changes: 55 additions & 0 deletions templates/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package templates

import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"path/filepath"
"runtime"
)

//go:embed layouts/* pages/* components/*
var templatesFS embed.FS

// RenderTemplate renders a template with the given layout, components, and properties.
func RenderTemplate(w http.ResponseWriter, layout string, components []string, props interface{}) error {
_, filename, _, _ := runtime.Caller(1)
page := filepath.Base(filename)
page = page[:len(page)-len(filepath.Ext(page))]

// Collect the template paths
templatePaths := []string{
"layouts/" + layout + ".tmpl",
"pages/" + page + ".tmpl",
}

for _, component := range components {
templatePaths = append(templatePaths, "components/"+component+".tmpl")
}

// Create a new template and parse the files from the embedded file system
ts := template.New("")
for _, path := range templatePaths {
content, err := fs.ReadFile(templatesFS, path)
if err != nil {
log.Print(err.Error())
return err
}
ts, err = ts.Parse(string(content))
if err != nil {
log.Print(err.Error())
return err
}
}

// Execute the template
err := ts.ExecuteTemplate(w, layout, props)
if err != nil {
log.Print(err.Error())
return err
}

return nil
}

0 comments on commit acae0ec

Please sign in to comment.