parent
10de2f216f
commit
01176c845b
14 changed files with 259 additions and 183 deletions
|
|
@ -27,8 +27,7 @@ type Partial struct {
|
|||
}
|
||||
|
||||
const GeneratedDirName = "__htmgo"
|
||||
const HttpModuleName = "net/http"
|
||||
const ChiModuleName = "github.com/go-chi/chi/v5"
|
||||
const EchoModuleName = "github.com/labstack/echo/v4"
|
||||
const ModuleName = "github.com/maddalax/htmgo/framework/h"
|
||||
|
||||
var PackageName = fmt.Sprintf("package %s", GeneratedDirName)
|
||||
|
|
@ -199,7 +198,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
fName := "GetPartialFromContext"
|
||||
|
||||
body := `
|
||||
path := r.URL.Path
|
||||
path := ctx.Request().URL.Path
|
||||
`
|
||||
|
||||
if len(partials) == 0 {
|
||||
|
|
@ -216,7 +215,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
|
||||
body += fmt.Sprintf(`
|
||||
if path == "%s" || path == "%s" {
|
||||
cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext)
|
||||
cc := ctx.(*h.RequestContext)
|
||||
return %s(cc)
|
||||
}
|
||||
`, f.FuncName, path, caller)
|
||||
|
|
@ -227,7 +226,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
f := Function{
|
||||
Name: fName,
|
||||
Parameters: []NameType{
|
||||
{Name: "r", Type: "*http.Request"},
|
||||
{Name: "ctx", Type: "echo.Context"},
|
||||
},
|
||||
Return: []ReturnType{
|
||||
{Type: "*h.Partial"},
|
||||
|
|
@ -238,15 +237,14 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
builder.Append(builder.BuildFunction(f))
|
||||
|
||||
registerFunction := fmt.Sprintf(`
|
||||
func RegisterPartials(router *chi.Mux) {
|
||||
router.Handle("/%s/partials*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
partial := GetPartialFromContext(r)
|
||||
if partial == nil {
|
||||
w.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
h.PartialView(w, partial)
|
||||
}))
|
||||
func RegisterPartials(f *echo.Echo) {
|
||||
f.Any("%s/partials*", func(ctx echo.Context) error {
|
||||
partial := GetPartialFromContext(ctx)
|
||||
if partial == nil {
|
||||
return ctx.NoContent(404)
|
||||
}
|
||||
return h.PartialView(ctx, partial)
|
||||
})
|
||||
}
|
||||
`, moduleName)
|
||||
|
||||
|
|
@ -269,8 +267,7 @@ func writePartialsFile() {
|
|||
builder.AppendLine(GeneratedFileLine)
|
||||
builder.AppendLine(PackageName)
|
||||
builder.AddImport(ModuleName)
|
||||
builder.AddImport(HttpModuleName)
|
||||
builder.AddImport(ChiModuleName)
|
||||
builder.AddImport(EchoModuleName)
|
||||
|
||||
moduleName := GetModuleName()
|
||||
for _, partial := range partials {
|
||||
|
|
@ -310,8 +307,7 @@ func writePagesFile() {
|
|||
builder := NewCodeBuilder(nil)
|
||||
builder.AppendLine(GeneratedFileLine)
|
||||
builder.AppendLine(PackageName)
|
||||
builder.AddImport(HttpModuleName)
|
||||
builder.AddImport(ChiModuleName)
|
||||
builder.AddImport(EchoModuleName)
|
||||
|
||||
pages, _ := findPublicFuncsReturningHPage("pages")
|
||||
|
||||
|
|
@ -335,20 +331,18 @@ func writePagesFile() {
|
|||
for _, page := range pages {
|
||||
call := fmt.Sprintf("%s.%s", page.Package, page.FuncName)
|
||||
|
||||
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))
|
||||
body += fmt.Sprintf(`
|
||||
f.GET("%s", func(ctx echo.Context) error {
|
||||
cc := ctx.(*h.RequestContext)
|
||||
return h.HtmlView(ctx, %s(cc))
|
||||
})
|
||||
`, formatRoute(page.Path), call,
|
||||
)
|
||||
`, formatRoute(page.Path), call)
|
||||
}
|
||||
|
||||
f := Function{
|
||||
Name: fName,
|
||||
Parameters: []NameType{
|
||||
{Name: "router", Type: "*chi.Mux"},
|
||||
{Name: "f", Type: "*echo.Echo"},
|
||||
},
|
||||
Body: body,
|
||||
}
|
||||
|
|
@ -383,20 +377,19 @@ func GenAst(flags ...process.RunFlag) error {
|
|||
writePagesFile()
|
||||
|
||||
WriteFile("__htmgo/setup-generated.go", func(content *ast.File) string {
|
||||
|
||||
return fmt.Sprintf(`
|
||||
return `
|
||||
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
|
||||
package __htmgo
|
||||
|
||||
import (
|
||||
"%s"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Register(r *chi.Mux) {
|
||||
RegisterPartials(r)
|
||||
RegisterPages(r)
|
||||
func Register(e *echo.Echo) {
|
||||
RegisterPartials(e)
|
||||
RegisterPages(e)
|
||||
}
|
||||
`, ChiModuleName)
|
||||
`
|
||||
})
|
||||
|
||||
return nil
|
||||
|
|
|
|||
13
examples/todo-list/__htmgo/pages-generated.go
Normal file
13
examples/todo-list/__htmgo/pages-generated.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// 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))
|
||||
})
|
||||
}
|
||||
49
examples/todo-list/__htmgo/partials-generated.go
Normal file
49
examples/todo-list/__htmgo/partials-generated.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// 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)
|
||||
})
|
||||
}
|
||||
11
examples/todo-list/__htmgo/setup-generated.go
Normal file
11
examples/todo-list/__htmgo/setup-generated.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// 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)
|
||||
}
|
||||
4
framework/assets/dist/htmgo.js
vendored
4
framework/assets/dist/htmgo.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -5,6 +5,7 @@ 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]"))) {
|
||||
|
|
@ -20,25 +21,25 @@ htmx.defineExtension("livereload", {
|
|||
}
|
||||
|
||||
console.log('livereload extension initialized.');
|
||||
// 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;
|
||||
}
|
||||
if(lastVersion !== message) {
|
||||
lastVersion = message;
|
||||
reload()
|
||||
}
|
||||
};
|
||||
// Handle errors (e.g., when the connection is closed)
|
||||
eventSource.onerror = function(error) {
|
||||
console.error('EventSource error:', error);
|
||||
};
|
||||
|
||||
createWebSocketClient({
|
||||
url: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${host}/dev/livereload`,
|
||||
onOpen: () => {
|
||||
},
|
||||
onMessage: (message) => {
|
||||
if(lastVersion === "") {
|
||||
lastVersion = message;
|
||||
}
|
||||
if(lastVersion !== message) {
|
||||
lastVersion = message;
|
||||
reload()
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
},
|
||||
onClose: () => {
|
||||
}
|
||||
})
|
||||
},
|
||||
// @ts-ignore
|
||||
onEvent: function (name, evt) {
|
||||
|
|
@ -48,4 +49,14 @@ 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)
|
||||
// })
|
||||
}
|
||||
|
|
@ -3,13 +3,22 @@ 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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,34 @@
|
|||
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=
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
package h
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/maddalax/htmgo/framework/hx"
|
||||
"github.com/maddalax/htmgo/framework/service"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestContext struct {
|
||||
*http.Request
|
||||
echo.Context
|
||||
locator *service.Locator
|
||||
isBoosted bool
|
||||
currentBrowserUrl string
|
||||
|
|
@ -23,21 +21,6 @@ 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 {
|
||||
|
|
@ -47,72 +30,65 @@ func (c *RequestContext) ServiceLocator() *service.Locator {
|
|||
type AppOpts struct {
|
||||
LiveReload bool
|
||||
ServiceLocator *service.Locator
|
||||
Register func(app *App)
|
||||
Register func(echo *echo.Echo)
|
||||
}
|
||||
|
||||
type App struct {
|
||||
Opts AppOpts
|
||||
Router *chi.Mux
|
||||
Opts AppOpts
|
||||
Echo *echo.Echo
|
||||
}
|
||||
|
||||
var instance *App
|
||||
|
||||
func GetApp() *App {
|
||||
if instance == nil {
|
||||
panic("App instance not initialized")
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
func Start(opts AppOpts) {
|
||||
router := chi.NewRouter()
|
||||
e := echo.New()
|
||||
instance := App{
|
||||
Opts: opts,
|
||||
Router: router,
|
||||
Opts: opts,
|
||||
Echo: e,
|
||||
}
|
||||
instance.start()
|
||||
}
|
||||
|
||||
const RequestContextKey = "htmgo.request.context"
|
||||
|
||||
func populateHxFields(cc *RequestContext) {
|
||||
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)
|
||||
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 (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)
|
||||
})
|
||||
})
|
||||
}
|
||||
func (a App) start() {
|
||||
|
||||
func (app *App) start() {
|
||||
|
||||
app.Router.Use(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cc := &RequestContext{
|
||||
locator: app.Opts.ServiceLocator,
|
||||
Request: r,
|
||||
kv: make(map[string]interface{}),
|
||||
}
|
||||
populateHxFields(cc)
|
||||
ctx := context.WithValue(r.Context(), RequestContextKey, cc)
|
||||
h.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
})
|
||||
|
||||
if app.Opts.Register != nil {
|
||||
app.Opts.Register(app)
|
||||
if a.Opts.Register != nil {
|
||||
a.Opts.Register(a.Echo)
|
||||
}
|
||||
|
||||
if app.Opts.LiveReload && IsDevelopment() {
|
||||
app.AddLiveReloadHandler("/dev/livereload")
|
||||
a.Echo.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
cc := &RequestContext{
|
||||
Context: c,
|
||||
locator: a.Opts.ServiceLocator,
|
||||
}
|
||||
populateHxFields(cc)
|
||||
return next(cc)
|
||||
}
|
||||
})
|
||||
|
||||
if a.Opts.LiveReload && IsDevelopment() {
|
||||
AddLiveReloadHandler("/dev/livereload", a.Echo)
|
||||
}
|
||||
|
||||
port := ":3000"
|
||||
slog.Info(fmt.Sprintf("Server started on port %s", port))
|
||||
err := http.ListenAndServe(port, app.Router)
|
||||
err := a.Echo.Start(port)
|
||||
|
||||
if err != nil {
|
||||
// If we are in watch mode, just try to kill any processes holding that port
|
||||
|
|
@ -127,7 +103,7 @@ func (app *App) start() {
|
|||
cmd.Run()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
err = http.ListenAndServe(":3000", app.Router)
|
||||
err = a.Echo.Start(port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
@ -138,38 +114,46 @@ func (app *App) start() {
|
|||
}
|
||||
}
|
||||
|
||||
func writeHtml(w http.ResponseWriter, element Ren) error {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
_, err := fmt.Fprint(w, Render(element))
|
||||
return err
|
||||
func HtmlView(c echo.Context, page *Page) error {
|
||||
root := page.Root
|
||||
return c.HTML(200,
|
||||
Render(
|
||||
root,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func HtmlView(w http.ResponseWriter, page *Page) error {
|
||||
return writeHtml(w, page.Root)
|
||||
}
|
||||
|
||||
func PartialViewWithHeaders(w http.ResponseWriter, headers *Headers, partial *Partial) error {
|
||||
func PartialViewWithHeaders(c echo.Context, headers *Headers, partial *Partial) error {
|
||||
if partial.Headers != nil {
|
||||
for s, a := range *partial.Headers {
|
||||
w.Header().Set(s, a)
|
||||
c.Set(s, a)
|
||||
}
|
||||
}
|
||||
|
||||
if headers != nil {
|
||||
for s, a := range *headers {
|
||||
w.Header().Set(s, a)
|
||||
c.Response().Header().Set(s, a)
|
||||
}
|
||||
}
|
||||
|
||||
return writeHtml(w, partial.Root)
|
||||
return c.HTML(200,
|
||||
Render(
|
||||
partial,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func PartialView(w http.ResponseWriter, partial *Partial) error {
|
||||
func PartialView(c echo.Context, partial *Partial) error {
|
||||
c.Set(echo.HeaderContentType, echo.MIMETextHTML)
|
||||
if partial.Headers != nil {
|
||||
for s, a := range *partial.Headers {
|
||||
w.Header().Set(s, a)
|
||||
c.Response().Header().Set(s, a)
|
||||
}
|
||||
}
|
||||
|
||||
return writeHtml(w, partial.Root)
|
||||
return c.HTML(200,
|
||||
Render(
|
||||
partial,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.isHxRequest {
|
||||
if ctx.Get("HX-Request") != "" {
|
||||
return node
|
||||
}
|
||||
return Empty()
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func CombineHeaders(headers ...*Headers) *Headers {
|
|||
}
|
||||
|
||||
func CurrentPath(ctx *RequestContext) string {
|
||||
current := ctx.Header.Get(hx.CurrentUrlHeader)
|
||||
current := ctx.Request().Header.Get(hx.CurrentUrlHeader)
|
||||
parsed, err := url.Parse(current)
|
||||
if err != nil {
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -1,41 +1,30 @@
|
|||
package h
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"net/http"
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/net/websocket"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Version = uuid.NewString()
|
||||
|
||||
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
|
||||
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 {
|
||||
return
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
// 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 (app *App) AddLiveReloadHandler(path string) {
|
||||
app.Router.Get(path, sseHandler)
|
||||
func AddLiveReloadHandler(path string, app *echo.Echo) {
|
||||
app.GET(path, handler)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,8 +49,8 @@ func (q *Qs) ToString() string {
|
|||
}
|
||||
|
||||
func GetQueryParam(ctx *RequestContext, key string) string {
|
||||
value, ok := ctx.URL.Query()[key]
|
||||
if !ok {
|
||||
value := ctx.QueryParam(key)
|
||||
if value == "" {
|
||||
current := ctx.currentBrowserUrl
|
||||
if current != "" {
|
||||
u, err := url.Parse(current)
|
||||
|
|
@ -59,7 +59,7 @@ func GetQueryParam(ctx *RequestContext, key string) string {
|
|||
}
|
||||
}
|
||||
}
|
||||
return value[0]
|
||||
return value
|
||||
}
|
||||
|
||||
func SetQueryParams(href string, qs *Qs) string {
|
||||
|
|
|
|||
|
|
@ -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,23 +19,21 @@ func main() {
|
|||
h.Start(h.AppOpts{
|
||||
ServiceLocator: locator,
|
||||
LiveReload: true,
|
||||
Register: func(app *h.App) {
|
||||
|
||||
app.UseWithContext(func(w http.ResponseWriter, r *http.Request, context map[string]any) {
|
||||
context["embeddedMarkdown"] = markdownAssets
|
||||
})
|
||||
|
||||
Register: func(e *echo.Echo) {
|
||||
sub, err := fs.Sub(staticAssets, "assets/dist")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
e.StaticFS("/public", sub)
|
||||
|
||||
http.FileServerFS(sub)
|
||||
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Set("embeddedMarkdown", markdownAssets)
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
|
||||
app.Router.Handle("/public/*", http.StripPrefix("/public", http.FileServerFS(sub)))
|
||||
|
||||
__htmgo.Register(app.Router)
|
||||
__htmgo.Register(e)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue