hmux

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: CC0-1.0 Imports: 3 Imported by: 0

README

package hmux

import "github.com/zeebo/hmux"

go.dev Go Report Card SourceGraph

Usage

See the examples in the documentation for library usage. Or, read the code! It's less than 100 lines. You might enjoy it.

Documentation

Overview

Example
package main

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

	"github.com/zeebo/hmux"
)

func main() {
	handler := http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
		fmt.Printf("method: %q\n", req.Method)
		fmt.Printf("path: %q\n", req.URL.Path)
		fmt.Printf("name: %q\n", hmux.Arg("name").Value(req.Context()))
	})

	resources := hmux.Dir{
		"/foo": hmux.Dir{
			"*": hmux.Arg("name").Capture(
				hmux.Method{
					"GET":  handler,
					"POST": handler,
				},
			),
		},
	}

	resources.ServeHTTP(nil, httptest.NewRequest("POST", "/foo/bar", nil))
	fmt.Println("---")
	resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/baz/bif", nil))

}
Output:

method: "POST"
path: ""
name: "bar"
---
method: "GET"
path: "/bif"
name: "baz"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg

type Arg string

Arg captures path components and attaches them to the request context. It always captures a non-empty component.

Example
package main

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

	"github.com/zeebo/hmux"
)

func main() {
	resources := hmux.Arg("name").Capture(
		http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			fmt.Printf("arg: %q\n", hmux.Arg("name").Value(req.Context()))
			fmt.Printf("path: %q\n", req.URL.Path)
		}),
	)

	resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/bar", nil))

}
Output:

arg: "foo"
path: "/bar"

func (Arg) Capture

func (a Arg) Capture(h http.Handler) http.Handler

Capture consumes a path component and stores it in the request context so that it can be retrieved with Value.

func (Arg) Value

func (a Arg) Value(ctx context.Context) string

Value returns the value associated with the Arg on the context.

type Dir

type Dir map[string]http.Handler

Dir pulls off path components from the front of the path and dispatches. It attempts to dispatch to "*" without consuming a path component if nothing matches. To ensure a path ends, use a key of "". To ensure it ends with a slash, use "/".

Example
package main

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

	"github.com/zeebo/hmux"
)

func main() {
	resources := hmux.Dir{
		"/foo": http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			fmt.Printf("path: %q\n", req.URL.Path)
		}),
	}

	resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/bar", nil))

}
Output:

path: "/bar"

func (Dir) ServeHTTP

func (d Dir) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface.

type Method

type Method map[string]http.Handler

Method checks the request method and dispatches.

Example
package main

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

	"github.com/zeebo/hmux"
)

func main() {
	resources := hmux.Method{
		"POST": http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			fmt.Printf("method: %q\n", req.Method)
			fmt.Printf("path: %q\n", req.URL.Path)
		}),
	}

	resources.ServeHTTP(nil, httptest.NewRequest("POST", "/foo", nil))

}
Output:

method: "POST"
path: "/foo"

func (Method) ServeHTTP

func (m Method) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface.

Jump to

Keyboard shortcuts

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