mux

package
v0.0.0-...-c5cf874 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package mux provides Mux

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizePath

func NormalizePath(value string) string

NormalizePath normalizes a path sent to a web server.

- Any "." and ".." in path are removed lexicographically - If any path does not start with "/", it is prepended to the string - If any path does not end with "/", it is appended. - Paths that start with ".." or "." are turned in paths starting at the root.

Types

type Mux

type Mux struct {

	// NotFound is called when no prefix is matched.
	NotFound http.Handler
	// contains filtered or unexported fields
}

Mux routes requests to different handlers. See the [Add] for how requests are matched to their handler.

Example
package main

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

	"github.com/tkw1536/pkglib/httpx"
	"github.com/tkw1536/pkglib/httpx/mux"
)

func main() {
	var mux mux.Mux
	mux.NotFound = httpx.ErrNotFound

	// handle the "/something" route
	mux.Add("/something", nil, false, httpx.Response{Body: []byte("/something")})

	// handle the post route only for post calls
	mux.Add("/something/post", func(r *http.Request) bool { return r.Method == http.MethodPost }, false, httpx.Response{Body: []byte("/post")})

	// handle /something/exact route only on exact
	mux.Add("/something/exact", nil, true, httpx.Response{Body: []byte("/exact")})

	// a function to make a request to a specific method
	makeRequest := func(method, path string) {
		req, err := http.NewRequest(method, path, nil)
		if err != nil {
			panic(err)
		}

		rr := httptest.NewRecorder()
		mux.ServeHTTP(rr, req)

		rrr := rr.Result()
		result, _ := io.ReadAll(rrr.Body)
		fmt.Printf("%s %q returned code %d with %s %q\n", method, path, rrr.StatusCode, rrr.Header.Get("Content-Type"), string(result))
	}

	makeRequest(http.MethodGet, "/something")
	makeRequest(http.MethodGet, "/something/post")
	makeRequest(http.MethodPost, "/something/post")
	makeRequest(http.MethodGet, "/something/exact")
	makeRequest(http.MethodGet, "/something/exact/sub")
	makeRequest(http.MethodGet, "/notfound")

}
Output:

GET "/something" returned code 200 with text/plain; charset=utf-8 "/something"
GET "/something/post" returned code 200 with text/plain; charset=utf-8 "/something"
POST "/something/post" returned code 200 with text/plain; charset=utf-8 "/post"
GET "/something/exact" returned code 200 with text/plain; charset=utf-8 "/exact"
GET "/something/exact/sub" returned code 200 with text/plain; charset=utf-8 "/something"
GET "/notfound" returned code 404 with text/plain; charset=utf-8 "Not Found"

func (*Mux) Add

func (mux *Mux) Add(path string, predicate Predicate, exact bool, h http.Handler)

Add adds a new handler to this Mux.

path is the path or prefix to match for the given handler. exact determines if only the exact path is to be matched or the entire prefix. predicate is an additional predicate that must return true on any incoming request for the path to match.

func (*Mux) Match

func (mux *Mux) Match(r *http.Request) (http.Handler, bool)

Match returns the handler to be applied for the given request.

func (*Mux) ServeHTTP

func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves requests to this mux

type Predicate

type Predicate func(*http.Request) bool

A Predicate is a function that matches a request. The nil predicate always matches.

func (Predicate) Call

func (p Predicate) Call(r *http.Request) bool

Call checks if this predicate matches the given request.

Jump to

Keyboard shortcuts

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