Skip to content

crhntr/muxt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Muxt Go Reference Go

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]

Route Registration Example

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}}

Tiny Examples

muxt routes HTML templates to Go methods and handles common web plumbing:

  • {{define "GET /{id} F(id)"}}{{end}} — Parses {id} as int and passes to F(int).
  • {{define "GET / F(ctx)"}}{{end}} — Injects request.Context() if ctx is used.
  • {{define "GET / F(request)"}}{{end}} — Injects the *http.Request when request is named.
  • {{define "GET / F(response)"}}{{end}} — Injects http.ResponseWriter if response is used.
  • {{define "POST / F(form)"}}{{end}} — Parses form data into a struct from url.Values if the form parameter is a struct.
  • {{define "POST / F(form)"}}{{end}} — Parses form data into a struct if the form parameter is a url.Values.

The result of the call is wrapped in a TemplateData[T] struct and passed to ExecuteTemplate.

Bigger Examples

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:

License

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.

Documentation

Introduction

Reference

Testing

Philosophy & Vision

Prompting