Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal htmgo #84

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/minimal-htmgo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/assets/dist
tmp
node_modules
.idea
__htmgo
dist
7 changes: 7 additions & 0 deletions examples/minimal-htmgo/README.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions examples/minimal-htmgo/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
18 changes: 18 additions & 0 deletions examples/minimal-htmgo/go.sum
Original file line number Diff line number Diff line change
@@ -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=
24 changes: 24 additions & 0 deletions examples/minimal-htmgo/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions examples/minimal-htmgo/pages/index.go
Original file line number Diff line number Diff line change
@@ -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()),
)
}
26 changes: 26 additions & 0 deletions examples/minimal-htmgo/render.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading