Skip to content

Commit

Permalink
Merge pull request #8 from maddalax/remove-echo
Browse files Browse the repository at this point in the history
wip - swap out echo with std lib + chi router
  • Loading branch information
maddalax authored Sep 26, 2024
2 parents 01176c8 + fefcc6a commit 4d003b6
Show file tree
Hide file tree
Showing 29 changed files with 244 additions and 480 deletions.
1 change: 1 addition & 0 deletions cli/htmgo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.0
require (
github.com/dave/jennifer v1.7.1
github.com/fsnotify/fsnotify v1.7.0
github.com/google/uuid v1.6.0
golang.org/x/mod v0.21.0
golang.org/x/net v0.29.0
golang.org/x/sys v0.25.0
Expand Down
59 changes: 33 additions & 26 deletions cli/htmgo/tasks/astgen/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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"},
Expand All @@ -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)
if partial == nil {
return ctx.NoContent(404)
}
return h.PartialView(ctx, partial)
})
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)
}))
}
`, moduleName)

Expand All @@ -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 {
Expand Down Expand Up @@ -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")

Expand All @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions examples/todo-list/__htmgo/pages-generated.go

This file was deleted.

49 changes: 0 additions & 49 deletions examples/todo-list/__htmgo/partials-generated.go

This file was deleted.

11 changes: 0 additions & 11 deletions examples/todo-list/__htmgo/setup-generated.go

This file was deleted.

12 changes: 2 additions & 10 deletions examples/todo-list/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.23.0

require (
entgo.io/ent v0.14.1
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/maddalax/htmgo/framework v0.0.0-20240924182609-b2c6b5207510
github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f
github.com/mattn/go-sqlite3 v1.14.16
)

Expand All @@ -17,16 +17,8 @@ require (
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // 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/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
29 changes: 4 additions & 25 deletions examples/todo-list/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
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/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
Expand All @@ -31,19 +33,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
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/maddalax/htmgo/framework v0.0.0-20240923034654-e82ed0497052 h1:iEtAIL0zLDI3hp56VzHAccC4c6Iuem6dMARr9/aki9I=
github.com/maddalax/htmgo/framework v0.0.0-20240923034654-e82ed0497052/go.mod h1:TA7KCaKhurpXceQrzClJOHqRsUSd5NL64ZngSg+I3oc=
github.com/maddalax/htmgo/framework v0.0.0-20240924182609-b2c6b5207510 h1:Gl9QRbIr008Qgvcsnsnkn1Q7Fusw3Hn6dPEbiV8M8wk=
github.com/maddalax/htmgo/framework v0.0.0-20240924182609-b2c6b5207510/go.mod h1:TA7KCaKhurpXceQrzClJOHqRsUSd5NL64ZngSg+I3oc=
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/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f h1:A4MEslfWXKvqsZt1DbvZpguzVAbyjCRclEZNt6TjSRw=
github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
Expand All @@ -54,28 +45,16 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
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=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
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/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
24 changes: 14 additions & 10 deletions examples/todo-list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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"
"io/fs"
"net/http"
"todolist/__htmgo"
"todolist/ent"
"todolist/infrastructure/db"
Expand All @@ -22,18 +22,22 @@ func main() {
return db.Provide()
})

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) {
e.StaticFS("/public", sub)
__htmgo.Register(e)
Register: func(app *h.App) {

sub, err := fs.Sub(StaticAssets, "assets/dist")

if err != nil {
panic(err)
}

http.FileServerFS(sub)

app.Router.Handle("/public/*", http.StripPrefix("/public", http.FileServerFS(sub)))

__htmgo.Register(app.Router)
},
})
}
4 changes: 4 additions & 0 deletions examples/todo-list/pages/base/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ func RootPage(children ...h.Ren) h.Ren {
return h.Html(
h.HxExtension(h.BaseExtensions()),
h.Head(
h.Meta("viewport", "width=device-width, initial-scale=1"),
h.Meta("title", "htmgo todo mvc"),
h.Meta("description", "an example of how to build a todo mvc app with htmgo"),
h.Meta("charset", "utf-8"),
h.Link("/public/main.css", "stylesheet"),
h.Script("/public/htmgo.js"),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/todo-list/partials/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func CompleteAllIcon(list []*ent.Task) *h.Element {
}))

return h.Div(
h.ClassX("absolute top-0 left-0 p-4 rotate-90 text-2xl cursor-pointer", map[string]bool{
h.ClassX("absolute top-1 left-5 p-2 rotate-90 text-3xl cursor-pointer", map[string]bool{
"text-slate-400": notCompletedCount > 0,
}), h.Text(""),
}), h.Text("›"),
h.PostPartialWithQs(CompleteAll, h.NewQs("complete", h.Ternary(notCompletedCount > 0, "true", "false"))),
)
}
Expand Down
13 changes: 2 additions & 11 deletions framework-ui/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ module github.com/maddalax/htmgo/framework-ui

go 1.23.0

require github.com/maddalax/htmgo/framework v0.1.0
require github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f

require (
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/labstack/echo/v4 v4.12.0 // 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/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/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
4 changes: 3 additions & 1 deletion framework/assets/dist/htmgo.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4d003b6

Please sign in to comment.