Skip to content

Commit

Permalink
Revert "swap out echo with std lib"
Browse files Browse the repository at this point in the history
This reverts commit 6ec3dfa.
  • Loading branch information
maddalax committed Sep 26, 2024
1 parent 10de2f2 commit 01176c8
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 179 deletions.
59 changes: 26 additions & 33 deletions cli/htmgo/tasks/astgen/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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"},
Expand All @@ -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)

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

Expand All @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions examples/todo-list/__htmgo/pages-generated.go
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions examples/todo-list/__htmgo/partials-generated.go
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions examples/todo-list/__htmgo/setup-generated.go
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 3 deletions framework/assets/dist/htmgo.js

Large diffs are not rendered by default.

47 changes: 29 additions & 18 deletions framework/assets/js/htmxextensions/livereload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]"))) {
Expand All @@ -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) {
Expand All @@ -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)
// })
}
11 changes: 10 additions & 1 deletion framework/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
25 changes: 23 additions & 2 deletions framework/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
Loading

0 comments on commit 01176c8

Please sign in to comment.