Compare commits
1 commit
master
...
minimal-ht
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f49241873e |
7 changed files with 136 additions and 0 deletions
6
examples/minimal-htmgo/.gitignore
vendored
Normal file
6
examples/minimal-htmgo/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/assets/dist
|
||||
tmp
|
||||
node_modules
|
||||
.idea
|
||||
__htmgo
|
||||
dist
|
||||
7
examples/minimal-htmgo/README.md
Normal file
7
examples/minimal-htmgo/README.md
Normal file
|
|
@ -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
examples/minimal-htmgo/go.mod
Normal file
11
examples/minimal-htmgo/go.mod
Normal file
|
|
@ -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
examples/minimal-htmgo/go.sum
Normal file
18
examples/minimal-htmgo/go.sum
Normal file
|
|
@ -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
examples/minimal-htmgo/main.go
Normal file
24
examples/minimal-htmgo/main.go
Normal file
|
|
@ -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
examples/minimal-htmgo/pages/index.go
Normal file
44
examples/minimal-htmgo/pages/index.go
Normal file
|
|
@ -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
examples/minimal-htmgo/render.go
Normal file
26
examples/minimal-htmgo/render.go
Normal file
|
|
@ -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…
Reference in a new issue