violetear: github.com/nbari/violetear Index | Files | Directories

package violetear

import "github.com/nbari/violetear"

Package violetear - HTTP router

Basic example:

package main

import (
   "fmt"
   "github.com/nbari/violetear"
   "log"
   "net/http"
)

func catchAll(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, r.URL.Path[1:])
}

func helloWorld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, r.URL.Path[1:])
}

func handleUUID(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, r.URL.Path[1:])
}

func main() {
    router := violetear.New()
    router.LogRequests = true
    router.RequestID = "REQUEST_LOG_ID"

    router.AddRegex(":uuid", `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`)

    router.HandleFunc("*", catchAll)
    router.HandleFunc("/hello", helloWorld, "GET,HEAD")
    router.HandleFunc("/root/:uuid/item", handleUUID, "POST,PUT")

    srv := &http.Server{
        Addr:           ":8080",
        Handler:        router,
        ReadTimeout:    5 * time.Second,
        WriteTimeout:   7 * time.Second,
        MaxHeaderBytes: 1 << 20,
    }
    log.Fatal(srv.ListenAndServe())
}

Index

Package Files

dynamic.go logger.go params.go responsewriter.go trie.go violetear.go

Constants

const (
    ParamsKey key = 0
)

ParamsKey used for the context

func GetParam Uses

func GetParam(name string, r *http.Request, index ...int) string

GetParam returns a value for the parameter set in path When having duplicate params pass the index as the last argument to retrieve the desired value.

func GetParams Uses

func GetParams(name string, r *http.Request) []string

GetParams returns param or params in a []string

func GetRouteName Uses

func GetRouteName(r *http.Request) string

GetRouteName return the name of the route

type MethodHandler Uses

type MethodHandler struct {
    Method  string
    Handler http.Handler
}

MethodHandler keeps HTTP Method and http.handler

type Params Uses

type Params map[string]interface{}

Params string/interface map used with context

func (Params) Add Uses

func (p Params) Add(k, v string)

Add param to Params

type ResponseWriter Uses

type ResponseWriter struct {
    http.ResponseWriter
    // contains filtered or unexported fields
}

ResponseWriter wraps the standard http.ResponseWriter

func NewResponseWriter Uses

func NewResponseWriter(w http.ResponseWriter, rid string) *ResponseWriter

NewResponseWriter returns ResponseWriter

func (*ResponseWriter) RequestID Uses

func (w *ResponseWriter) RequestID() string

RequestID retrieve the Request ID

func (*ResponseWriter) RequestTime Uses

func (w *ResponseWriter) RequestTime() string

RequestTime return the request time

func (*ResponseWriter) Size Uses

func (w *ResponseWriter) Size() int

Size provides an easy way to retrieve the response size in bytes

func (*ResponseWriter) Status Uses

func (w *ResponseWriter) Status() int

Status provides an easy way to retrieve the status code

func (*ResponseWriter) Write Uses

func (w *ResponseWriter) Write(data []byte) (int, error)

Write satisfies the http.ResponseWriter interface and captures data written, in bytes

func (*ResponseWriter) WriteHeader Uses

func (w *ResponseWriter) WriteHeader(statusCode int)

WriteHeader satisfies the http.ResponseWriter interface and allows us to catch the status code

type Router Uses

type Router struct {

    // Logger
    Logger func(*ResponseWriter, *http.Request)

    // LogRequests yes or no
    LogRequests bool

    // NotFoundHandler configurable http.Handler which is called when no matching
    // route is found. If it is not set, http.NotFound is used.
    NotFoundHandler http.Handler

    // NotAllowedHandler configurable http.Handler which is called when method not allowed.
    NotAllowedHandler http.Handler

    // PanicHandler function to handle panics.
    PanicHandler http.HandlerFunc

    // RequestID name of the header to use or create.
    RequestID string

    // Verbose
    Verbose bool
    // contains filtered or unexported fields
}

Router struct

func New Uses

func New() *Router

New returns a new initialized router.

func (*Router) AddRegex Uses

func (r *Router) AddRegex(name, regex string) error

AddRegex adds a ":named" regular expression to the dynamicRoutes

func (*Router) GetError Uses

func (r *Router) GetError() error

GetError returns an error resulted from building a route, if any.

func (*Router) Handle Uses

func (r *Router) Handle(path string, handler http.Handler, httpMethods ...string) *Trie

Handle registers the handler for the given pattern (path, http.Handler, methods).

func (*Router) HandleFunc Uses

func (r *Router) HandleFunc(path string, handler http.HandlerFunc, httpMethods ...string) *Trie

HandleFunc add a route to the router (path, http.HandlerFunc, methods)

func (*Router) MethodNotAllowed Uses

func (r *Router) MethodNotAllowed() http.HandlerFunc

MethodNotAllowed default handler for 405

func (*Router) ServeHTTP Uses

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

ServeHTTP dispatches the handler registered in the matched path

type Trie Uses

type Trie struct {
    Handler     []MethodHandler
    HasCatchall bool
    HasRegex    bool
    Node        []*Trie
    // contains filtered or unexported fields
}

Trie data structure

func (*Trie) Get Uses

func (t *Trie) Get(path, version string) (*Trie, string, string, bool)

Get returns a node

func (*Trie) Name Uses

func (t *Trie) Name(name string) *Trie

Name add custom name to node

func (*Trie) Set Uses

func (t *Trie) Set(path []string, handler http.Handler, method, version string) (*Trie, error)

Set adds a node (url part) to the Trie

func (*Trie) SplitPath Uses

func (t *Trie) SplitPath(path string) (string, string)

SplitPath returns first element of path and remaining path

Directories

PathSynopsis
middlewarePackage middleware - HTTP middleware

Package violetear imports 8 packages (graph) and is imported by 32 packages. Updated 2018-09-20. Refresh now. Tools for package owners.