diff --git a/cli/htmgo/tasks/run/build.go b/cli/htmgo/tasks/run/build.go index 385c0f5..47fb10c 100644 --- a/cli/htmgo/tasks/run/build.go +++ b/cli/htmgo/tasks/run/build.go @@ -17,7 +17,7 @@ func Build() { process.RunOrExit("mkdir -p ./dist") if os.Getenv("SKIP_GO_BUILD") != "1" { - process.RunOrExit(fmt.Sprintf("go build -o ./dist")) + process.RunOrExit(fmt.Sprintf("go build -tags prod -o ./dist")) } fmt.Printf("Executable built at %s\n", process.GetPathRelativeToCwd("dist")) diff --git a/htmgo-site/assets.go b/htmgo-site/assets.go new file mode 100644 index 0000000..123e8f3 --- /dev/null +++ b/htmgo-site/assets.go @@ -0,0 +1,17 @@ +//go:build !prod +// +build !prod + +package main + +import ( + "htmgo-site/internal/embedded" + "io/fs" +) + +func GetStaticAssets() fs.FS { + return embedded.NewOsFs() +} + +func GetMarkdownAssets() fs.FS { + return embedded.NewOsFs() +} diff --git a/htmgo-site/assets_prod.go b/htmgo-site/assets_prod.go new file mode 100644 index 0000000..3c1fc06 --- /dev/null +++ b/htmgo-site/assets_prod.go @@ -0,0 +1,23 @@ +//go:build prod +// +build prod + +package main + +import ( + "embed" + "io/fs" +) + +//go:embed assets/dist/* +var staticAssets embed.FS + +//go:embed md/* +var markdownAssets embed.FS + +func GetStaticAssets() fs.FS { + return staticAssets +} + +func GetMarkdownAssets() fs.FS { + return markdownAssets +} diff --git a/htmgo-site/internal/embedded/os.go b/htmgo-site/internal/embedded/os.go new file mode 100644 index 0000000..ddfd55f --- /dev/null +++ b/htmgo-site/internal/embedded/os.go @@ -0,0 +1,17 @@ +package embedded + +import ( + "io/fs" + "os" +) + +type OsFs struct { +} + +func (receiver OsFs) Open(name string) (fs.File, error) { + return os.Open(name) +} + +func NewOsFs() OsFs { + return OsFs{} +} diff --git a/htmgo-site/internal/markdown/render.go b/htmgo-site/internal/markdown/render.go index 329a849..8677ac9 100644 --- a/htmgo-site/internal/markdown/render.go +++ b/htmgo-site/internal/markdown/render.go @@ -25,6 +25,11 @@ func (r *Renderer) RenderFile(source string, system fs.FS) string { } o, err := system.Open(source) + + if o == nil { + return "" + } + defer o.Close() if err != nil { diff --git a/htmgo-site/main.go b/htmgo-site/main.go index 5ddc257..20788e0 100644 --- a/htmgo-site/main.go +++ b/htmgo-site/main.go @@ -1,42 +1,34 @@ package main import ( - "embed" "github.com/labstack/echo/v4" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" - _ "github.com/mattn/go-sqlite3" "htmgo-site/__htmgo" "htmgo-site/internal/markdown" "io/fs" ) -//go:embed assets/dist/* -var StaticAssets embed.FS - -//go:embed md/* -var MarkdownAssets embed.FS - func main() { locator := service.NewLocator() + staticAssets := GetStaticAssets() + markdownAssets := GetMarkdownAssets() service.Set(locator, service.Singleton, markdown.NewRenderer) - sub, err := fs.Sub(StaticAssets, "assets/dist") - - if err != nil { - panic(err) - } - h.Start(h.AppOpts{ ServiceLocator: locator, LiveReload: true, Register: func(e *echo.Echo) { + sub, err := fs.Sub(staticAssets, "assets/dist") + if err != nil { + panic(err) + } e.StaticFS("/public", sub) e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - c.Set("embeddedMarkdown", &MarkdownAssets) + c.Set("embeddedMarkdown", markdownAssets) return next(c) } }) diff --git a/htmgo-site/md/docs/1_quick-start/1_introduction.md b/htmgo-site/md/docs/1_quick-start/1_introduction.md index 6ba49eb..0b453e8 100644 --- a/htmgo-site/md/docs/1_quick-start/1_introduction.md +++ b/htmgo-site/md/docs/1_quick-start/1_introduction.md @@ -5,7 +5,7 @@ We give you the utilities to build html using pure go code in a reusable way (go ```go func DocsPage(ctx *h.RequestContext) *h.Page { - assets := ctx.Get("embeddedMarkdown").(*embed.FS) + assets := ctx.Get("embeddedMarkdown").(fs.FS) pages := dirwalk.WalkPages("md/docs", assets) return h.NewPage(base.RootPage( diff --git a/htmgo-site/pages/docs.go b/htmgo-site/pages/docs.go index be0cef3..c770336 100644 --- a/htmgo-site/pages/docs.go +++ b/htmgo-site/pages/docs.go @@ -1,15 +1,15 @@ package pages import ( - "embed" "github.com/maddalax/htmgo/framework/h" "htmgo-site/internal/dirwalk" "htmgo-site/pages/base" "htmgo-site/partials" + "io/fs" ) func DocsPage(ctx *h.RequestContext) *h.Page { - assets := ctx.Get("embeddedMarkdown").(*embed.FS) + assets := ctx.Get("embeddedMarkdown").(fs.FS) pages := dirwalk.WalkPages("md/docs", assets) return h.NewPage(base.RootPage( diff --git a/htmgo-site/pages/markdown.go b/htmgo-site/pages/markdown.go index 768d8a7..247b128 100644 --- a/htmgo-site/pages/markdown.go +++ b/htmgo-site/pages/markdown.go @@ -1,10 +1,10 @@ package pages import ( - "embed" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" "htmgo-site/internal/markdown" + "io/fs" ) func MarkdownPage(ctx *h.RequestContext, path string, id string) *h.Element { @@ -17,7 +17,7 @@ func MarkdownPage(ctx *h.RequestContext, path string, id string) *h.Element { } func MarkdownContent(ctx *h.RequestContext, path string, id string) *h.Element { - embeddedMd := ctx.Get("embeddedMarkdown").(*embed.FS) + embeddedMd := ctx.Get("embeddedMarkdown").(fs.FS) renderer := service.Get[markdown.Renderer](ctx.ServiceLocator()) return h.Div( h.If(id != "", h.Id(id)),