From f49241873e8529b111de066b4b8e78f7e34f815a Mon Sep 17 00:00:00 2001 From: maddalax Date: Thu, 21 Nov 2024 07:26:51 -0600 Subject: [PATCH] minimal htmgo --- examples/minimal-htmgo/.gitignore | 6 ++++ examples/minimal-htmgo/README.md | 7 +++++ examples/minimal-htmgo/go.mod | 11 +++++++ examples/minimal-htmgo/go.sum | 18 +++++++++++ examples/minimal-htmgo/main.go | 24 +++++++++++++++ examples/minimal-htmgo/pages/index.go | 44 +++++++++++++++++++++++++++ examples/minimal-htmgo/render.go | 26 ++++++++++++++++ 7 files changed, 136 insertions(+) create mode 100644 examples/minimal-htmgo/.gitignore create mode 100644 examples/minimal-htmgo/README.md create mode 100644 examples/minimal-htmgo/go.mod create mode 100644 examples/minimal-htmgo/go.sum create mode 100644 examples/minimal-htmgo/main.go create mode 100644 examples/minimal-htmgo/pages/index.go create mode 100644 examples/minimal-htmgo/render.go diff --git a/examples/minimal-htmgo/.gitignore b/examples/minimal-htmgo/.gitignore new file mode 100644 index 0000000..3d6a979 --- /dev/null +++ b/examples/minimal-htmgo/.gitignore @@ -0,0 +1,6 @@ +/assets/dist +tmp +node_modules +.idea +__htmgo +dist \ No newline at end of file diff --git a/examples/minimal-htmgo/README.md b/examples/minimal-htmgo/README.md new file mode 100644 index 0000000..118197a --- /dev/null +++ b/examples/minimal-htmgo/README.md @@ -0,0 +1,7 @@ +Minimal example that just uses htmgo for html rendering / js support and nothing else. + +Removes automatic support for: + +1. live reloading +2. tailwind recompilation +3. page/partial route registration diff --git a/examples/minimal-htmgo/go.mod b/examples/minimal-htmgo/go.mod new file mode 100644 index 0000000..1d39f6b --- /dev/null +++ b/examples/minimal-htmgo/go.mod @@ -0,0 +1,11 @@ +module minimal-htmgo + +go 1.23.0 + +require github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca + +require ( + github.com/go-chi/chi/v5 v5.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/examples/minimal-htmgo/go.sum b/examples/minimal-htmgo/go.sum new file mode 100644 index 0000000..2d58c3e --- /dev/null +++ b/examples/minimal-htmgo/go.sum @@ -0,0 +1,18 @@ +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/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +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= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +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= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/minimal-htmgo/main.go b/examples/minimal-htmgo/main.go new file mode 100644 index 0000000..8a65edd --- /dev/null +++ b/examples/minimal-htmgo/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/go-chi/chi/v5" + "minimal-htmgo/pages" + "net/http" +) + +func main() { + router := chi.NewRouter() + + fileServer := http.StripPrefix("/public", http.FileServer(http.Dir("./assets/dist"))) + router.Handle("/public/*", fileServer) + + router.Get("/", func(writer http.ResponseWriter, request *http.Request) { + RenderPage(request, writer, pages.Index) + }) + + router.Get("/current-time", func(writer http.ResponseWriter, request *http.Request) { + RenderPartial(request, writer, pages.CurrentTime) + }) + + http.ListenAndServe(":3000", router) +} diff --git a/examples/minimal-htmgo/pages/index.go b/examples/minimal-htmgo/pages/index.go new file mode 100644 index 0000000..a98048d --- /dev/null +++ b/examples/minimal-htmgo/pages/index.go @@ -0,0 +1,44 @@ +package pages + +import ( + "github.com/maddalax/htmgo/framework/h" + "github.com/maddalax/htmgo/framework/js" + "time" +) + +func Index(ctx *h.RequestContext) *h.Page { + return h.NewPage( + h.Html( + h.HxExtensions( + h.BaseExtensions(), + ), + h.Head( + h.Meta("viewport", "width=device-width, initial-scale=1"), + h.Script("/public/htmgo.js"), + ), + h.Body( + h.Pf("hello htmgo"), + h.Div( + h.Get("/current-time", "load, every 1s"), + ), + h.Div( + h.Button( + h.Text("Click me"), + h.OnClick( + js.EvalJs(` + console.log("you evalulated javascript"); + alert("you clicked me"); + `), + ), + ), + ), + ), + ), + ) +} + +func CurrentTime(ctx *h.RequestContext) *h.Partial { + return h.NewPartial( + h.Pf("It is %s", time.Now().String()), + ) +} diff --git a/examples/minimal-htmgo/render.go b/examples/minimal-htmgo/render.go new file mode 100644 index 0000000..2019e20 --- /dev/null +++ b/examples/minimal-htmgo/render.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/maddalax/htmgo/framework/h" + "net/http" +) + +func RenderToString(element *h.Element) string { + return h.Render(element) +} + +func RenderPage(req *http.Request, w http.ResponseWriter, page func(ctx *h.RequestContext) *h.Page) { + ctx := h.RequestContext{ + Request: req, + Response: w, + } + h.HtmlView(w, page(&ctx)) +} + +func RenderPartial(req *http.Request, w http.ResponseWriter, partial func(ctx *h.RequestContext) *h.Partial) { + ctx := h.RequestContext{ + Request: req, + Response: w, + } + h.PartialView(w, partial(&ctx)) +}