Documentation ¶
Overview ¶
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 ¶
- Constants
- func GetParam(name string, r *http.Request, index ...int) string
- func GetParams(name string, r *http.Request) []string
- func GetRouteName(r *http.Request) string
- type MethodHandler
- type Params
- type ResponseWriter
- type Router
- func (r *Router) AddRegex(name, regex string) error
- func (r *Router) GetError() error
- func (r *Router) Handle(path string, handler http.Handler, httpMethods ...string) *Trie
- func (r *Router) HandleFunc(path string, handler http.HandlerFunc, httpMethods ...string) *Trie
- func (r *Router) MethodNotAllowed() http.HandlerFunc
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type Trie
Constants ¶
const (
ParamsKey key = 0
)
ParamsKey used for the context
Variables ¶
This section is empty.
Functions ¶
func GetParam ¶
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 GetRouteName ¶
GetRouteName return the name of the route
Types ¶
type MethodHandler ¶
MethodHandler keeps HTTP Method and http.handler
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter // 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 (*Router) Handle ¶
Handle registers the handler for the given pattern (path, http.Handler, methods).
func (*Router) HandleFunc ¶
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
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package middleware - HTTP middleware https://github.com/justinas/alice Basic example: package main import ( "github.com/nbari/violetear" "github.com/nbari/violetear/middleware" "log" "net/http" ) func commonHeaders(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-app-Version", "1.0") next.ServeHTTP(w, r) }) } func middlewareOne(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Executing middlewareOne") next.ServeHTTP(w, r) log.Println("Executing middlewareOne again") }) } func main() { router := violetear.New() stdChain := middleware.New(commonHeaders, middlewareOne) router.Handle("/", stdChain.ThenFunc(catchAll), "GET,HEAD") log.Fatal(http.ListenAndServe(":8080", router)) }
|
Package middleware - HTTP middleware https://github.com/justinas/alice Basic example: package main import ( "github.com/nbari/violetear" "github.com/nbari/violetear/middleware" "log" "net/http" ) func commonHeaders(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-app-Version", "1.0") next.ServeHTTP(w, r) }) } func middlewareOne(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Executing middlewareOne") next.ServeHTTP(w, r) log.Println("Executing middlewareOne again") }) } func main() { router := violetear.New() stdChain := middleware.New(commonHeaders, middlewareOne) router.Handle("/", stdChain.ThenFunc(catchAll), "GET,HEAD") log.Fatal(http.ListenAndServe(":8080", router)) } |