router

package module
v0.0.0-...-96c18d3 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2019 License: MIT Imports: 3 Imported by: 0

README

HTTP ROUTER

A very lean implementation of http router that supports path param parsing, suitable for most REST implementations. Inspired by httprouter

Usage

package main

import (
	"fmt"
	"github.com/shyamz-22/router"
	"log"
	"net/http"
)

func main() {
	
	rtr := router.New()
	
	rtr.Add("/ping", http.MethodGet, func(w http.ResponseWriter, r *http.Request, params router.PathParams) {
		w.Write([]byte("Pong!"))
	})
	
	rtr.Add("/pings/:id", http.MethodGet, func(w http.ResponseWriter, r *http.Request, params router.PathParams) {
    	id := params.ByName("id")	
    	response := fmt.Sprintf("Pong: %s", id)
		w.Write([]byte(response))
    	})
	
    log.Fatal(http.ListenAndServe(":8080", rtr))
}

Running tests

> go test -v ./...

Running parallel tests

> go test -parallel 8 -v ./...

Running Benchmarks

> go test -run none -bench Benchmark -benchmem -benchtime 3s -memprofile mem.out

Memory profiling

> go test -run none -bench BenchmarkGithub -benchmem -benchtime 20s -memprofile mem.out
> go tool pprof -alloc_space router.test mem.out
 
Output
File: router.test
Type: alloc_space
Time: Sep 2, 2018 at 9:32pm (CEST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
(pprof) list findRoute

Setup

> go get github.com/shyamz-22/router
> cd $GOPATH/github.com/shyamz-22/router
> dep ensure -update
> go test ./...

What is not supported yet

  • Support for http.Handler
  • Behavior for trailing slashes
  • Configurable NotFound and MethodNotAllowed Handlers
  • Panic Handling
  • Regexp validation for path parameters

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFuncWithParam

type HandlerFuncWithParam func(w http.ResponseWriter, request *http.Request, param PathParams)

type Param

type Param struct {
	Key   string
	Value string
}

type PathParams

type PathParams []Param

func (PathParams) ByName

func (params PathParams) ByName(name string) string

type Router

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

func New

func New() *Router

func (*Router) Add

func (rtr *Router) Add(path string, method string, handler HandlerFuncWithParam)

func (*Router) AddDelete

func (rtr *Router) AddDelete(path string, handler HandlerFuncWithParam)

AddDelete registers a new request handle with the given path and Delete-method.

func (*Router) AddGet

func (rtr *Router) AddGet(path string, handler HandlerFuncWithParam)

AddGet registers a new request handle with the given path and Get-method.

func (*Router) AddHead

func (rtr *Router) AddHead(path string, handler HandlerFuncWithParam)

AddHead registers a new request handle with the given path and Head-method.

func (*Router) AddOptions

func (rtr *Router) AddOptions(path string, handler HandlerFuncWithParam)

AddOptions registers a new request handle with the given path and Options-method.

func (*Router) AddPatch

func (rtr *Router) AddPatch(path string, handler HandlerFuncWithParam)

AddPatch registers a new request handle with the given path and Patch-method.

func (*Router) AddPost

func (rtr *Router) AddPost(path string, handler HandlerFuncWithParam)

AddPost registers a new request handle with the given path and Post-method.

func (*Router) AddPut

func (rtr *Router) AddPut(path string, handler HandlerFuncWithParam)

AddPut registers a new request handle with the given path and Put-method.

func (*Router) ServeHTTP

func (rtr *Router) ServeHTTP(w http.ResponseWriter, request *http.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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