Back to

Package violetear

Latest Go to latest

The latest major version is .

Published: Nov 5, 2019 | License: BSD-3-Clause | Module:


Package violetear - HTTP router

Basic example:

package main

import (

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,



const (
	ParamsKey key = 0

ParamsKey used for the context

func GetParam

func GetParam(name string, r *http.Request, index 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

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

GetParams returns param or params in a []string

func GetRouteName

func GetRouteName(r *http.Request) string

GetRouteName return the name of the route

type MethodHandler

type MethodHandler struct {
	Method  string
	Handler http.Handler

MethodHandler keeps HTTP Method and http.handler

type Params

type Params map[string]interface{}

Params string/interface map used with context

func (Params) Add

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

Add param to Params

type ResponseWriter

type ResponseWriter struct {
	// contains filtered or unexported fields

ResponseWriter wraps the standard http.ResponseWriter

func NewResponseWriter

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

NewResponseWriter returns ResponseWriter

func (*ResponseWriter) RequestID

func (w *ResponseWriter) RequestID() string

RequestID retrieve the Request ID

func (*ResponseWriter) RequestTime

func (w *ResponseWriter) RequestTime() string

RequestTime return the request time

func (*ResponseWriter) Size

func (w *ResponseWriter) Size() int

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

func (*ResponseWriter) Status

func (w *ResponseWriter) Status() int

Status provides an easy way to retrieve the status code

func (*ResponseWriter) Write

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

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

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

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

type Router

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

func New() *Router

New returns a new initialized router.

func (*Router) AddRegex

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

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

func (*Router) GetError

func (r *Router) GetError() error

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

func (*Router) Handle

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

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

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

MethodNotAllowed default handler for 405

func (*Router) ServeHTTP

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

ServeHTTP dispatches the handler registered in the matched path

type Trie

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

Trie data structure

func (*Trie) Get

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

Get returns a node

func (*Trie) Name

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

Name add custom name to node

func (*Trie) Set

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

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

SplitPath returns first element of path and remaining path

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier