httprouter

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

README

httprouter: HTTP router GoDoc Travis CI

Basic HTTP router for Go.

Example

The following code:

package main

import (
        "fmt"
        "log"
        "net/http"

        "batou.dev/httprouter"
)

func main() {
        r := httprouter.NewRouter()

        r.Endpoint("/foo").
                Get(handleFoo).
                Post(handleFoo)
        r.Endpoint("/bar/:baz").
                Get(handleBar)
        r.Endpoint("/*").
                Get(handleDefault)

        s := &http.Server{
                Addr:    ":8080",
                Handler: r,
        }

        err := s.ListenAndServe()
        if err != nil {
                log.Fatal(err)
        }
}

func handleBar(rw http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(rw, "Received %q\n", httprouter.ContextParam(r, "baz").(string))
}

func handleDefault(rw http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(rw, "Default here!")
}

func handleFoo(rw http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(rw, "Received %q request\n", r.Method)
}

will give you:

# curl http://localhost:8080/foo
Received "GET" request

# curl -X POST http://localhost:8080/foo
Received "POST" request

# curl http://localhost:8080/bar/baz
Received "baz"

# curl http://localhost:8080/
Default here!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextParam

func ContextParam(r *http.Request, key string) interface{}

ContextParam returns a request context parameter given its name.

func QueryParam

func QueryParam(r *http.Request, key string) string

QueryParam returns a request query parameter given its name.

func SetContextParam

func SetContextParam(r *http.Request, key string, value interface{}) *http.Request

SetContextParam sets a new request context parameter.

Types

type Endpoint

type Endpoint struct {
	// contains filtered or unexported fields
}

Endpoint represents an HTTP router endpoint.

func (*Endpoint) Any

func (e *Endpoint) Any(f http.HandlerFunc) *Endpoint

Any registers a handler for any method.

func (*Endpoint) Delete

func (e *Endpoint) Delete(f http.HandlerFunc) *Endpoint

Delete registers a DELETE method handler.

func (*Endpoint) Endpoint

func (e *Endpoint) Endpoint(pattern string) *Endpoint

Endpoint creates a new HTTP router endpoint.

func (*Endpoint) Get

func (e *Endpoint) Get(f http.HandlerFunc) *Endpoint

Get registers a GET method handler.

func (*Endpoint) Head

func (e *Endpoint) Head(f http.HandlerFunc) *Endpoint

Head registers a HEAD method handler.

func (*Endpoint) Methods

func (e *Endpoint) Methods() []string

Methods returns the list of methods available from the HTTP router endpoint.

func (*Endpoint) Options

func (e *Endpoint) Options(f http.HandlerFunc) *Endpoint

Options registers a OPTIONS method handler.

func (*Endpoint) Patch

func (e *Endpoint) Patch(f http.HandlerFunc) *Endpoint

Patch registers a PATCH method handler.

func (*Endpoint) Post

func (e *Endpoint) Post(f http.HandlerFunc) *Endpoint

Post registers a POST method handler.

func (*Endpoint) Put

func (e *Endpoint) Put(f http.HandlerFunc) *Endpoint

Put registers a PUT method handler.

func (*Endpoint) Use

func (e *Endpoint) Use(f func(http.Handler) http.Handler) *Endpoint

Use registers a new middleware in the HTTP handlers chain.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router represents an HTTP router instance.

func New

func New() *Router

New creates a new HTTP router instance.

func (*Router) Endpoint

func (r *Router) Endpoint(pattern string) *Endpoint

Endpoint creates a new HTTP router endpoint.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP satisfies the http.Handler interface.

func (*Router) Use

func (r *Router) Use(f func(http.Handler) http.Handler) *Router

Use registers a new middleware in the HTTP handlers chain.

Jump to

Keyboard shortcuts

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