dont embed assets in dev to speed up live reload

This commit is contained in:
maddalax 2024-09-25 10:44:43 -05:00
parent 4f0d4a6b3c
commit d7b0576269
9 changed files with 75 additions and 21 deletions

View file

@ -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"))

17
htmgo-site/assets.go Normal file
View file

@ -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()
}

23
htmgo-site/assets_prod.go Normal file
View file

@ -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
}

View file

@ -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{}
}

View file

@ -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 {

View file

@ -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)
}
})

View file

@ -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(

View file

@ -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(

View file

@ -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)),