httpmux

package
v1.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: Apache-2.0, BSD-3-Clause Imports: 1 Imported by: 3

README

httpmux

httpmux exposes the Go 1.22+ net/http.ServeMux routing model with a TinyGo-compatible fallback. It is intended for TinyGo releases whose bundled ServeMux accepts only part of the modern pattern syntax while keeping normal Go builds on the standard library implementation.

import (
    "net/http"

    "github.com/shibukawa/tinygodriver/httpmux"
)

mux := httpmux.NewServeMux()
mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {
    _, _ = w.Write([]byte(r.PathValue("id")))
})
http.ListenAndServe(":8080", mux)

The supported syntax is [METHOD ][HOST]/[PATH], including literal segments, {name}, terminal {name...}, terminal {$}, trailing-slash subtrees, GET to HEAD matching, and segment-wise URL unescaping. Matching is independent of registration order. Invalid, duplicate, and conflicting registrations panic.

The mux also implements the standard path-cleaning and subtree redirects, host matching, CONNECT handling, 404/405 selection, and Allow headers. Handler deliberately does not populate path values; ServeHTTP does.

Implementation selection

  • normal host Go builds make httpmux.ServeMux a type alias of net/http.ServeMux
  • TinyGo builds use this package's compatible implementation
  • go build -tags force_tinygo_logic forces the TinyGo-compatible implementation on host Go

This is a behavioral subset, not a replacement for net/http package-level registration. It does not replace http.DefaultServeMux, implement GODEBUG=httpmuxgo121, or set http.Request.Pattern when the compatible implementation is selected. Normal host Go builds retain all behavior of the aliased standard-library type.

Documentation

Overview

Package httpmux provides an HTTP request multiplexer with the Go 1.22+ net/http ServeMux pattern syntax.

Normal host Go builds expose ServeMux as an alias of net/http.ServeMux. TinyGo builds and builds using the force_tinygo_logic tag select the compatible implementation in this package. Both variants implement net/http.Handler and expose the same constructor and registration methods.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ServeMux

type ServeMux = http.ServeMux

ServeMux is an alias of net/http.ServeMux in normal host Go builds.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/shibukawa/tinygodriver/httpmux"
)

func main() {
	mux := httpmux.NewServeMux()
	mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprint(w, r.PathValue("id"))
	})

	rr := httptest.NewRecorder()
	mux.ServeHTTP(rr, httptest.NewRequest("GET", "/users/42", nil))
	fmt.Println(rr.Body.String())
}
Output:
42

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL