router

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 5 Imported by: 1

README

fasthttp-staticrouter

Simple fasthttp static router not support params in url but fast

Features

  • Static http routing which simple fast

Usage

go get -u github.com/vinhjaxt/fasthttp-staticrouter

	r := router.New()

	api := r.Group("/api")
	api.Use(func(ctx *fasthttp.RequestCtx) (b bool) {
		b = true

		ctx.Response.Header.SetContentType("application/json;charset=utf-8")
		// Do authorization
		auth := true // Do something
		if !auth {
			// Abort request
			return
		}

		// Next router
		b = false
		return
	})

	apiv1 := api.Group("/v1.0")
	apiv1.Use(func(ctx *fasthttp.RequestCtx) (b bool) {
		ctx.Response.Header.Set("X-API-Version", "1.0")
		return
	})
	apiv1.Get("/", func(ctx *fasthttp.RequestCtx) (_ bool) {
		ctx.SetBodyString(`"Hello world"`)
		return
	})

Checkout Example

Available api:

  • r.New : Create a new router
  • r.Use : Use a Middleware
  • r.Get, r.Post, r.Put, r.Patch, r.Delete, r.Options, r.Head, r.Method, r.Any : HTTP methods
  • r.Group : Create group of routers
  • r.NotFound : Set not found handler
  • r.MethodNotAllowed : Set MethodNotAllowed handler
  • r.Recover or r.OnError : Set panic handler

Performance on example

Serve

./example -bind-tcp :8080 -static-dir ./public_web/

Test

wrk -t20 -c1000 -d30s -R1000000 http://127.0.0.1:8080/api/v1.0/

/index.html

Running 30s test @ http://127.0.0.1:8080/
  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    18.65s     5.45s   28.62s    57.66%
    Req/Sec     2.68k   216.40     3.21k    70.00%
  1614174 requests in 29.99s, 300.18MB read
Requests/sec:  53814.91
Transfer/sec:     10.01MB

/api/v1.0/

Running 30s test @ http://127.0.0.1:8080/api/v1.0/
  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    18.56s     5.35s   28.44s    57.98%
    Req/Sec     3.01k   178.65     3.49k    70.00%
  1805231 requests in 30.00s, 266.85MB read
Requests/sec:  60183.75
Transfer/sec:      8.90MB

Test Machine

OS: Parrot GNU/Linux 4.10 x86_64 
Host: 4290CTO ThinkPad X220 
Kernel: 5.6.0-2parrot1-amd64 
CPU: Intel i5-2410M (4) @ 2.900GHz 
GPU: Intel 2nd Generation Core Proces 
Memory: 4407MiB / 9856MiB 

Mem usage

31.9 MB

License

  • MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StrRecoverPanic = "Recovered from panic:"

StrRecoverPanic string

Functions

func MethodNotAllowedHandler added in v1.0.1

func MethodNotAllowedHandler(c *fasthttp.RequestCtx)

MethodNotAllowedHandler is default MethodNotAllowedHandler

func NotFoundHandler added in v1.0.1

func NotFoundHandler(c *fasthttp.RequestCtx)

NotFoundHandler is default NotFoundHandler

func RecoverHanlder added in v1.0.1

func RecoverHanlder(c *fasthttp.RequestCtx)

RecoverHanlder is default RecoverHanlder

Types

type GroupRouter

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

GroupRouter struct

func (*GroupRouter) Any

func (g *GroupRouter) Any(path string, h func(*fasthttp.RequestCtx) (_ bool))

Any method

func (*GroupRouter) Delete

func (g *GroupRouter) Delete(path string, h func(*fasthttp.RequestCtx) (_ bool))

Delete method

func (*GroupRouter) Get

func (g *GroupRouter) Get(path string, h func(*fasthttp.RequestCtx) (_ bool))

Get method

func (*GroupRouter) Group

func (g *GroupRouter) Group(path string) (cg *GroupRouter)

Group create another group

func (*GroupRouter) Head

func (g *GroupRouter) Head(path string, h func(*fasthttp.RequestCtx) (_ bool))

Head method

func (*GroupRouter) Method added in v1.0.1

func (g *GroupRouter) Method(path, method string, h func(*fasthttp.RequestCtx) (_ bool))

Method specific

func (*GroupRouter) Options

func (g *GroupRouter) Options(path string, h func(*fasthttp.RequestCtx) (_ bool))

Options method

func (*GroupRouter) Patch

func (g *GroupRouter) Patch(path string, h func(*fasthttp.RequestCtx) (_ bool))

Patch method

func (*GroupRouter) Post

func (g *GroupRouter) Post(path string, h func(*fasthttp.RequestCtx) (_ bool))

Post method

func (*GroupRouter) Put

func (g *GroupRouter) Put(path string, h func(*fasthttp.RequestCtx) (_ bool))

Put method

func (*GroupRouter) Use

func (g *GroupRouter) Use(h func(*fasthttp.RequestCtx) (abort bool))

Use middlewares

type Router

type Router struct {
	RecoverHanlder          func(*fasthttp.RequestCtx)
	NotFoundHandler         func(*fasthttp.RequestCtx)
	MethodNotAllowedHandler func(*fasthttp.RequestCtx)
	// contains filtered or unexported fields
}

Router struct

func New

func New() (r *Router)

New create a router

func (*Router) Any

func (r *Router) Any(path string, h func(*fasthttp.RequestCtx) (_ bool))

Any method

func (*Router) BuildHandler

func (r *Router) BuildHandler() func(ctx *fasthttp.RequestCtx)

BuildHandler return the request handler

func (*Router) Delete

func (r *Router) Delete(path string, h func(*fasthttp.RequestCtx) (_ bool))

Delete method

func (*Router) Get

func (r *Router) Get(path string, h func(*fasthttp.RequestCtx) (_ bool))

Get method

func (*Router) Group

func (r *Router) Group(path string) (g *GroupRouter)

Group make a group of routers

func (*Router) Handler added in v1.0.1

func (r *Router) Handler(ctx *fasthttp.RequestCtx)

Handler ate request :v

func (*Router) Head

func (r *Router) Head(path string, h func(*fasthttp.RequestCtx) (_ bool))

Head method

func (*Router) Method added in v1.0.1

func (r *Router) Method(path, method string, h func(*fasthttp.RequestCtx) (_ bool))

Method specific

func (*Router) MethodNotAllowed

func (r *Router) MethodNotAllowed(h func(*fasthttp.RequestCtx))

MethodNotAllowed handler

func (*Router) NotFound

func (r *Router) NotFound(h func(*fasthttp.RequestCtx))

NotFound handler

func (*Router) OnError

func (r *Router) OnError(h func(*fasthttp.RequestCtx))

OnError is an alias of r.Recover()

func (*Router) Options

func (r *Router) Options(path string, h func(*fasthttp.RequestCtx) (_ bool))

Options method

func (*Router) Patch

func (r *Router) Patch(path string, h func(*fasthttp.RequestCtx) (_ bool))

Patch method

func (*Router) Post

func (r *Router) Post(path string, h func(*fasthttp.RequestCtx) (_ bool))

Post method

func (*Router) Put

func (r *Router) Put(path string, h func(*fasthttp.RequestCtx) (_ bool))

Put method

func (*Router) Recover added in v1.0.1

func (r *Router) Recover(h func(*fasthttp.RequestCtx))

Recover set RecoverHanlder

func (*Router) Use

func (r *Router) Use(h func(*fasthttp.RequestCtx) (abort bool))

Use middlewares

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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