swap out echo with std lib

(cherry picked from commit 6ec3dfa790)
This commit is contained in:
maddalax 2024-09-26 14:15:57 -05:00
parent 01176c845b
commit 76ff3fba23
14 changed files with 179 additions and 255 deletions

View file

@ -27,7 +27,8 @@ type Partial struct {
}
const GeneratedDirName = "__htmgo"
const EchoModuleName = "github.com/labstack/echo/v4"
const HttpModuleName = "net/http"
const ChiModuleName = "github.com/go-chi/chi/v5"
const ModuleName = "github.com/maddalax/htmgo/framework/h"
var PackageName = fmt.Sprintf("package %s", GeneratedDirName)
@ -198,7 +199,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
fName := "GetPartialFromContext"
body := `
path := ctx.Request().URL.Path
path := r.URL.Path
`
if len(partials) == 0 {
@ -215,7 +216,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
body += fmt.Sprintf(`
if path == "%s" || path == "%s" {
cc := ctx.(*h.RequestContext)
cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext)
return %s(cc)
}
`, f.FuncName, path, caller)
@ -226,7 +227,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
f := Function{
Name: fName,
Parameters: []NameType{
{Name: "ctx", Type: "echo.Context"},
{Name: "r", Type: "*http.Request"},
},
Return: []ReturnType{
{Type: "*h.Partial"},
@ -237,14 +238,15 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
builder.Append(builder.BuildFunction(f))
registerFunction := fmt.Sprintf(`
func RegisterPartials(f *echo.Echo) {
f.Any("%s/partials*", func(ctx echo.Context) error {
partial := GetPartialFromContext(ctx)
func RegisterPartials(router *chi.Mux) {
router.Handle("/%s/partials*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
partial := GetPartialFromContext(r)
if partial == nil {
return ctx.NoContent(404)
w.WriteHeader(404)
return
}
return h.PartialView(ctx, partial)
})
h.PartialView(w, partial)
}))
}
`, moduleName)
@ -267,7 +269,8 @@ func writePartialsFile() {
builder.AppendLine(GeneratedFileLine)
builder.AppendLine(PackageName)
builder.AddImport(ModuleName)
builder.AddImport(EchoModuleName)
builder.AddImport(HttpModuleName)
builder.AddImport(ChiModuleName)
moduleName := GetModuleName()
for _, partial := range partials {
@ -307,7 +310,8 @@ func writePagesFile() {
builder := NewCodeBuilder(nil)
builder.AppendLine(GeneratedFileLine)
builder.AppendLine(PackageName)
builder.AddImport(EchoModuleName)
builder.AddImport(HttpModuleName)
builder.AddImport(ChiModuleName)
pages, _ := findPublicFuncsReturningHPage("pages")
@ -331,18 +335,20 @@ func writePagesFile() {
for _, page := range pages {
call := fmt.Sprintf("%s.%s", page.Package, page.FuncName)
body += fmt.Sprintf(`
f.GET("%s", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, %s(cc))
body += fmt.Sprintf(
`
router.Get("%s", func(writer http.ResponseWriter, request *http.Request) {
cc := request.Context().Value(h.RequestContextKey).(*h.RequestContext)
h.HtmlView(writer, %s(cc))
})
`, formatRoute(page.Path), call)
`, formatRoute(page.Path), call,
)
}
f := Function{
Name: fName,
Parameters: []NameType{
{Name: "f", Type: "*echo.Echo"},
{Name: "router", Type: "*chi.Mux"},
},
Body: body,
}
@ -377,19 +383,20 @@ func GenAst(flags ...process.RunFlag) error {
writePagesFile()
WriteFile("__htmgo/setup-generated.go", func(content *ast.File) string {
return `
return fmt.Sprintf(`
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import (
"github.com/labstack/echo/v4"
"%s"
)
func Register(e *echo.Echo) {
RegisterPartials(e)
RegisterPages(e)
func Register(r *chi.Mux) {
RegisterPartials(r)
RegisterPages(r)
}
`
`, ChiModuleName)
})
return nil

View file

@ -1,13 +0,0 @@
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import "github.com/labstack/echo/v4"
import "github.com/maddalax/htmgo/framework/h"
import "todolist/pages"
func RegisterPages(f *echo.Echo) {
f.GET("/", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, pages.TaskListPage(cc))
})
}

View file

@ -1,49 +0,0 @@
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import "github.com/maddalax/htmgo/framework/h"
import "github.com/labstack/echo/v4"
import "todolist/partials/task"
func GetPartialFromContext(ctx echo.Context) *h.Partial {
path := ctx.Request().URL.Path
if path == "UpdateName" || path == "/todolist/partials/task.UpdateName" {
cc := ctx.(*h.RequestContext)
return task.UpdateName(cc)
}
if path == "EditNameForm" || path == "/todolist/partials/task.EditNameForm" {
cc := ctx.(*h.RequestContext)
return task.EditNameForm(cc)
}
if path == "ToggleCompleted" || path == "/todolist/partials/task.ToggleCompleted" {
cc := ctx.(*h.RequestContext)
return task.ToggleCompleted(cc)
}
if path == "CompleteAll" || path == "/todolist/partials/task.CompleteAll" {
cc := ctx.(*h.RequestContext)
return task.CompleteAll(cc)
}
if path == "ClearCompleted" || path == "/todolist/partials/task.ClearCompleted" {
cc := ctx.(*h.RequestContext)
return task.ClearCompleted(cc)
}
if path == "Create" || path == "/todolist/partials/task.Create" {
cc := ctx.(*h.RequestContext)
return task.Create(cc)
}
if path == "ChangeTab" || path == "/todolist/partials/task.ChangeTab" {
cc := ctx.(*h.RequestContext)
return task.ChangeTab(cc)
}
return nil
}
func RegisterPartials(f *echo.Echo) {
f.Any("todolist/partials*", func(ctx echo.Context) error {
partial := GetPartialFromContext(ctx)
if partial == nil {
return ctx.NoContent(404)
}
return h.PartialView(ctx, partial)
})
}

View file

@ -1,11 +0,0 @@
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import (
"github.com/labstack/echo/v4"
)
func Register(e *echo.Echo) {
RegisterPartials(e)
RegisterPages(e)
}

File diff suppressed because one or more lines are too long

View file

@ -5,7 +5,6 @@ let lastVersion = "";
htmx.defineExtension("livereload", {
init: function () {
const host = window.location.host;
let enabled = false
for (const element of Array.from(htmx.findAll("[hx-ext]"))) {
@ -21,12 +20,12 @@ htmx.defineExtension("livereload", {
}
console.log('livereload extension initialized.');
createWebSocketClient({
url: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${host}/dev/livereload`,
onOpen: () => {
},
onMessage: (message) => {
// Create a new EventSource object and point it to your SSE endpoint
const eventSource = new EventSource('/dev/livereload');
// Listen for messages from the server
eventSource.onmessage = function(event) {
const message = event.data
// Log the message data received from the server
if(lastVersion === "") {
lastVersion = message;
}
@ -34,12 +33,12 @@ htmx.defineExtension("livereload", {
lastVersion = message;
reload()
}
},
onError: (error) => {
},
onClose: () => {
}
})
};
// Handle errors (e.g., when the connection is closed)
eventSource.onerror = function(error) {
console.error('EventSource error:', error);
};
},
// @ts-ignore
onEvent: function (name, evt) {
@ -49,14 +48,4 @@ htmx.defineExtension("livereload", {
function reload() {
window.location.reload()
// fetch(window.location.href).then(response => {
// return response.text();
// }).then(html => {
// document.open();
// document.write(html);
// document.close();
// }).catch(err => {
// console.log('failed to fetch live reload', err)
// setTimeout(reload, 100)
// })
}

View file

@ -3,22 +3,13 @@ module github.com/maddalax/htmgo/framework
go 1.23.0
require (
github.com/go-chi/chi/v5 v5.1.0
github.com/google/uuid v1.6.0
github.com/labstack/echo/v4 v4.12.0
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.29.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View file

@ -1,34 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -1,18 +1,20 @@
package h
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/go-chi/chi/v5"
"github.com/maddalax/htmgo/framework/hx"
"github.com/maddalax/htmgo/framework/service"
"log/slog"
"net/http"
"os/exec"
"runtime"
"time"
)
type RequestContext struct {
echo.Context
*http.Request
locator *service.Locator
isBoosted bool
currentBrowserUrl string
@ -21,6 +23,21 @@ type RequestContext struct {
hxTargetId string
hxTriggerName string
hxTriggerId string
kv map[string]interface{}
}
func (c *RequestContext) Set(key string, value interface{}) {
if c.kv == nil {
c.kv = make(map[string]interface{})
}
c.kv[key] = value
}
func (c *RequestContext) Get(key string) interface{} {
if c.kv == nil {
return nil
}
return c.kv[key]
}
func (c *RequestContext) ServiceLocator() *service.Locator {
@ -30,65 +47,72 @@ func (c *RequestContext) ServiceLocator() *service.Locator {
type AppOpts struct {
LiveReload bool
ServiceLocator *service.Locator
Register func(echo *echo.Echo)
Register func(app *App)
}
type App struct {
Opts AppOpts
Echo *echo.Echo
}
var instance *App
func GetApp() *App {
if instance == nil {
panic("App instance not initialized")
}
return instance
Router *chi.Mux
}
func Start(opts AppOpts) {
e := echo.New()
router := chi.NewRouter()
instance := App{
Opts: opts,
Echo: e,
Router: router,
}
instance.start()
}
const RequestContextKey = "htmgo.request.context"
func populateHxFields(cc *RequestContext) {
cc.isBoosted = cc.Request().Header.Get(hx.BoostedHeader) == "true"
cc.currentBrowserUrl = cc.Request().Header.Get(hx.CurrentUrlHeader)
cc.hxPromptResponse = cc.Request().Header.Get(hx.PromptResponseHeader)
cc.isHxRequest = cc.Request().Header.Get(hx.RequestHeader) == "true"
cc.hxTargetId = cc.Request().Header.Get(hx.TargetIdHeader)
cc.hxTriggerName = cc.Request().Header.Get(hx.TriggerNameHeader)
cc.hxTriggerId = cc.Request().Header.Get(hx.TriggerIdHeader)
cc.isBoosted = cc.Request.Header.Get(hx.BoostedHeader) == "true"
cc.isBoosted = cc.Request.Header.Get(hx.BoostedHeader) == "true"
cc.currentBrowserUrl = cc.Request.Header.Get(hx.CurrentUrlHeader)
cc.hxPromptResponse = cc.Request.Header.Get(hx.PromptResponseHeader)
cc.isHxRequest = cc.Request.Header.Get(hx.RequestHeader) == "true"
cc.hxTargetId = cc.Request.Header.Get(hx.TargetIdHeader)
cc.hxTriggerName = cc.Request.Header.Get(hx.TriggerNameHeader)
cc.hxTriggerId = cc.Request.Header.Get(hx.TriggerIdHeader)
}
func (a App) start() {
func (app *App) UseWithContext(h func(w http.ResponseWriter, r *http.Request, context map[string]any)) {
app.Router.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cc := r.Context().Value(RequestContextKey).(*RequestContext)
h(w, r, cc.kv)
handler.ServeHTTP(w, r)
})
})
}
if a.Opts.Register != nil {
a.Opts.Register(a.Echo)
}
func (app *App) start() {
a.Echo.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
app.Router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cc := &RequestContext{
Context: c,
locator: a.Opts.ServiceLocator,
locator: app.Opts.ServiceLocator,
Request: r,
kv: make(map[string]interface{}),
}
populateHxFields(cc)
return next(cc)
}
ctx := context.WithValue(r.Context(), RequestContextKey, cc)
h.ServeHTTP(w, r.WithContext(ctx))
})
})
if a.Opts.LiveReload && IsDevelopment() {
AddLiveReloadHandler("/dev/livereload", a.Echo)
if app.Opts.Register != nil {
app.Opts.Register(app)
}
if app.Opts.LiveReload && IsDevelopment() {
app.AddLiveReloadHandler("/dev/livereload")
}
port := ":3000"
err := a.Echo.Start(port)
slog.Info(fmt.Sprintf("Server started on port %s", port))
err := http.ListenAndServe(port, app.Router)
if err != nil {
// If we are in watch mode, just try to kill any processes holding that port
@ -103,7 +127,7 @@ func (a App) start() {
cmd.Run()
}
time.Sleep(time.Millisecond * 50)
err = a.Echo.Start(port)
err = http.ListenAndServe(":3000", app.Router)
if err != nil {
panic(err)
}
@ -114,46 +138,38 @@ func (a App) start() {
}
}
func HtmlView(c echo.Context, page *Page) error {
root := page.Root
return c.HTML(200,
Render(
root,
),
)
func writeHtml(w http.ResponseWriter, element Ren) error {
w.Header().Set("Content-Type", "text/html")
_, err := fmt.Fprint(w, Render(element))
return err
}
func PartialViewWithHeaders(c echo.Context, headers *Headers, partial *Partial) error {
func HtmlView(w http.ResponseWriter, page *Page) error {
return writeHtml(w, page.Root)
}
func PartialViewWithHeaders(w http.ResponseWriter, headers *Headers, partial *Partial) error {
if partial.Headers != nil {
for s, a := range *partial.Headers {
c.Set(s, a)
w.Header().Set(s, a)
}
}
if headers != nil {
for s, a := range *headers {
c.Response().Header().Set(s, a)
w.Header().Set(s, a)
}
}
return c.HTML(200,
Render(
partial,
),
)
return writeHtml(w, partial.Root)
}
func PartialView(c echo.Context, partial *Partial) error {
c.Set(echo.HeaderContentType, echo.MIMETextHTML)
func PartialView(w http.ResponseWriter, partial *Partial) error {
if partial.Headers != nil {
for s, a := range *partial.Headers {
c.Response().Header().Set(s, a)
w.Header().Set(s, a)
}
}
return c.HTML(200,
Render(
partial,
),
)
return writeHtml(w, partial.Root)
}

View file

@ -37,7 +37,7 @@ func IfElseLazy[T any](condition bool, cb1 func() T, cb2 func() T) T {
}
func IfHtmxRequest(ctx *RequestContext, node Ren) Ren {
if ctx.Get("HX-Request") != "" {
if ctx.isHxRequest {
return node
}
return Empty()

View file

@ -34,7 +34,7 @@ func CombineHeaders(headers ...*Headers) *Headers {
}
func CurrentPath(ctx *RequestContext) string {
current := ctx.Request().Header.Get(hx.CurrentUrlHeader)
current := ctx.Header.Get(hx.CurrentUrlHeader)
parsed, err := url.Parse(current)
if err != nil {
return ""

View file

@ -1,30 +1,41 @@
package h
import (
"fmt"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/net/websocket"
"net/http"
"time"
)
var Version = uuid.NewString()
func handler(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
_ = websocket.Message.Send(ws, Version)
// keep ws alive
for {
err := websocket.Message.Send(ws, Version)
if err != nil {
func sseHandler(w http.ResponseWriter, r *http.Request) {
// Set the necessary headers
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*") // Optional for CORS
// Flush the headers immediately
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
return
}
for {
// Write an event to the stream
_, err := fmt.Fprintf(w, "data: %s\n\n", Version)
if err != nil {
break
}
// Flush the response to ensure the client gets it immediately
flusher.Flush()
time.Sleep(500 * time.Millisecond)
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func AddLiveReloadHandler(path string, app *echo.Echo) {
app.GET(path, handler)
func (app *App) AddLiveReloadHandler(path string) {
app.Router.Get(path, sseHandler)
}

View file

@ -49,8 +49,8 @@ func (q *Qs) ToString() string {
}
func GetQueryParam(ctx *RequestContext, key string) string {
value := ctx.QueryParam(key)
if value == "" {
value, ok := ctx.URL.Query()[key]
if !ok {
current := ctx.currentBrowserUrl
if current != "" {
u, err := url.Parse(current)
@ -59,7 +59,7 @@ func GetQueryParam(ctx *RequestContext, key string) string {
}
}
}
return value
return value[0]
}
func SetQueryParams(href string, qs *Qs) string {

View file

@ -1,12 +1,12 @@
package main
import (
"github.com/labstack/echo/v4"
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
"htmgo-site/__htmgo"
"htmgo-site/internal/markdown"
"io/fs"
"net/http"
)
func main() {
@ -19,21 +19,23 @@ func main() {
h.Start(h.AppOpts{
ServiceLocator: locator,
LiveReload: true,
Register: func(e *echo.Echo) {
Register: func(app *h.App) {
app.UseWithContext(func(w http.ResponseWriter, r *http.Request, context map[string]any) {
context["embeddedMarkdown"] = markdownAssets
})
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)
return next(c)
}
})
http.FileServerFS(sub)
__htmgo.Register(e)
app.Router.Handle("/public/*", http.StripPrefix("/public", http.FileServerFS(sub)))
__htmgo.Register(app.Router)
},
})
}