Skip to content

Repository files navigation

Muxt Go Reference

Muxt is a command-line tool for Go's html/template and net/http. Not a framework. Not a router. No runtime. Two commands do most of the work: generate and check.

A template name defines the contract between the hypermedia and the http.Handler:

{{define "GET /article/{id} GetArticle(ctx, id)"}}
  <h1>{{.Result.Title}}</h1>
{{end}}
func (s Server) GetArticle(ctx context.Context, id int) (Article, error) { ... }

muxt generate reads the name and writes an http.Handler that calls your method.

muxt check type-checks a template against the data it receives. It checks each ExecuteTemplate call on the templates variable whose template name is a literal string and whose data has a statically known type. Generated handlers meet both conditions. Rename a field and the template that still uses it fails a static muxt check.

The simplest web-app architecture is a single binary with an HTTP server. The Go standard library has all the runtime you need for the HTTP endpoints and hypermedia generation. When you need more complicated front-end interaction, reach for fixi, htmx, and Datastar. What it lacks is statically type-checked coupling between your Go code and HTML templates.

Install

go install github.com/typelate/muxt@latest

Or as a project tool: go get -tool github.com/typelate/muxt.

muxt loads Go packages the way the go command does, so it inherits GOWORK, GOFLAGS, and GOROOT from its environment.

Template Name Syntax

This is what muxt uses to map Go <-> HTML.

Standard http.ServeMux pattern, optionally with a status code and method call:

[METHOD ][HOST]/[PATH][ HTTP_STATUS][ CALL]

Example: "POST /user/{id} 201 CreateUser(ctx, id, form)"

Supported parameters: ctx, request, response, path params, form (URL-encoded body), multipart (file uploads, including *multipart.FileHeader fields), body (raw io.Reader), unmarshalJSON(body) (JSON request bodies), execute (render callback), and lastEventID. Returns and errors flow through TemplateData[R, T]. Status codes can come from the template name, return values, or error types.

Wrapping the call changes the response representation: sse(Stream(ctx, execute)) streams Server-Sent Events and marshalJSON(GetUser(ctx)) responds application/json. Generate flags select a frontend library per package — --output-htmx adds HX* helper methods to TemplateData, and --output-datastar frames SSE events with Datastar's patch-elements protocol.

TemplateRoutePaths extends type safety to URLs: {{$.Path.GetArticle 42}} instead of hardcoded href="/article/42". Change the route pattern, the compiler finds every stale reference.

Examples

The command tests double as readable examples of every feature.

Commands

  • muxt generate: generate http.Handler glue (writes template_routes.go)
  • muxt check: type-check templates without generating (use in CI or editor save hooks)
  • muxt list-template-calls / muxt list-template-callers: explore call sites and callers
  • muxt test-template-mutations: vary each template action and report the ones no test catches
  • muxt explore-module / muxt generate-fake-server: survey muxt packages and spin up a fake server to poke at routes

Documentation

See the full documentation index.

Go Standard Library

License

Muxt generator: GNU AGPLv3

Generated code: not covered by AGPL. Muxt asserts no copyright over its output. Treat generated files as your own code, under whatever license your project uses.

No warranty. Muxt and its generated output are provided "as is", without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, and non-infringement. You are responsible for reviewing, testing, and securing any code generated by Muxt before using it. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability arising from the use of Muxt or its output.

Releases

Used by

Contributors

Languages