Muxt generates and registers HTTP Handler functions specified in HTML templates. It increases locality of behavior when creating server side rendered hypermedia web applications.
Muxt looks for templates with names that match an extended version of the http.ServeMux
pattern syntax.
The standard http.ServeMux
pattern syntax looks like this:
[METHOD ][HOST]/[PATH]
Muxt extends this by adding optional fields for an HTTP status and a call:
[METHOD ][HOST]/[PATH][ HTTP_STATUS][ CALL]
You tell muxt
how to generate the handler functions by defining templates like this {{define "GET / F()" -}}
.
muxt
will generate a handler function that calls F and pass the result to the template.
The template result will then be written to the HTTP response.
{{define "GET / F()" -}}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'/>
<title>Hello!</title>
</head>
<body>
<h1>Number {{.Result}}</h1>
</body>
</html>
{{- end}}
muxt
routes HTML templates to Go methods and handles common web plumbing:
{{define "GET /{id} F(id)"}}{{end}}
— Parses{id}
asint
and passes toF(int)
.{{define "GET / F(ctx)"}}{{end}}
— Injectsrequest.Context()
ifctx
is used.{{define "GET / F(request)"}}{{end}}
— Injects the*http.Request
whenrequest
is named.{{define "GET / F(response)"}}{{end}}
— Injectshttp.ResponseWriter
ifresponse
is used.{{define "POST / F(form)"}}{{end}}
— Parses form data into a struct fromurl.Values
if theform
parameter is a struct.{{define "POST / F(form)"}}{{end}}
— Parses form data into a struct if theform
parameter is aurl.Values
.
The result of the call is wrapped in a TemplateData[T]
struct and passed to ExecuteTemplate
.
For a small runnable, see: ./example/hypertext/index.gohtml
The Go package documentation for the example shows what is generated https://pkg.go.dev/github.com/crhntr/muxt/example/hypertext
.
For larger complete examples, see:
Muxt is licensed under the GNU AGPLv3.
However, the Go code generated by muxt
is not covered by the AGPL.
It is licensed under the MIT license (see https://choosealicense.com/licenses/mit/
).
The code generated by muxt
is provided as-is, without a warranty of any kind.
The muxt
author disclaims all liability for any bugs, regressions, or defects in generated output.