red

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2020 License: MIT Imports: 12 Imported by: 0

README

red

Build Status Go Reference Go Report Card

A lightweight, high performance and FastHTTP based micro web framework.

It's heavily inspired by atreugo

Features

  • Routing:
    • Based on router
    • Multiple handlers to single path (like express.js)
  • High performance:
    • Uses same stack with atreugo so the performance is almost same. (atreugo's benchmars is availabile in here)
  • Middleware support:
    • Normal middlewares
    • Defer middlewares (literally)
  • Responses:
    • MarshalJSON interface support (it's very useful if you're using a marshaler other than standart encoding/json)
    • Blob JSON response

Documentation

Index

Constants

View Source
const (
	XRequestIDHeader = "X-Request-ID"
)

Variables

This section is empty.

Functions

func AcquireURL added in v1.0.0

func AcquireURL(uri *fasthttp.URI) *stdUrl.URL

AcquirURL returns an url instance from pool

The returned URL may be passed to ReleaseURL when it is no longer needed. It is forbidden accessing url after releasing it

func B2S added in v1.0.0

func B2S(b []byte) string

B2S converts byte slice to a string without memory allocation. See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .

func ConvertToFastHTTPHandler

func ConvertToFastHTTPHandler(handler RequestHandler) fasthttp.RequestHandler

func ReleaseCtx

func ReleaseCtx(ctx *Ctx)

ReleaseCtx returns ctx acquired via AcquireCtx to context pool

It is forbidden accessing ctx after releasing it

func ReleaseURL added in v1.0.0

func ReleaseURL(url *stdUrl.URL)

ReleaseURL returns URL acquired via AcquireURL to pool.

It is forbidden accessing url after releasing it

func S2B added in v1.0.0

func S2B(s string) (b []byte)

S2B converts string to a byte slice without memory allocation.

Note it may break if string and/or slice header will change in the future go versions.

Types

type Config

type Config struct {
	//FastHTTP Settings
	ErrorHandler                       func(*Ctx, error)
	Name                               string
	Concurrency                        int
	DisableKeepalive                   bool
	ReadBufferSize                     int
	WriteBufferSize                    int
	ReadTimeout                        time.Duration
	WriteTimeout                       time.Duration
	IdleTimeout                        time.Duration
	MaxConnsPerIP                      int
	MaxRequestsPerConn                 int
	MaxKeepaliveDuration               time.Duration
	TCPKeepalive                       bool
	TCPKeepalivePeriod                 time.Duration
	MaxRequestBodySize                 int
	ReduceMemoryUsage                  bool
	GetOnly                            bool
	DisablePreParseMultipartForm       bool
	LogAllErrors                       bool
	DisableHeaderNamesNormalizing      bool
	SleepWhenConcurrencyLimitsExceeded time.Duration
	NoDefaultServerHeader              bool
	NoDefaultDate                      bool
	NoDefaultContentType               bool
	ConnState                          func(net.Conn, fasthttp.ConnState)
	Logger                             fasthttp.Logger
	KeepHijackedConns                  bool

	//Router Settigs
	SaveMatchedRoutePath bool
	GlobalOPTIONS        RequestHandler
	NotFound             RequestHandler
	MethodNotAllowed     RequestHandler
	PanicHandler         func(*Ctx, interface{})
}

type Ctx

type Ctx struct {
	*fasthttp.RequestCtx
	// contains filtered or unexported fields
}

Ctx context wrapper of fasthttp.RequestCtx to adds extra funtionality

func AcquireCtx

func AcquireCtx(ctx *fasthttp.RequestCtx) *Ctx

AcquireCtx returns an empty Ctx instance from context pool

The returned Ctx instance may be passed to ReleaseCtx when it is no longer needed. It is forbidden accessing ctx after releasing it

func (*Ctx) Defer added in v1.0.0

func (ctx *Ctx) Defer(deferFunc func())

Defer appends given function to defer functions list

defer functions will be executed after defer middlewares's executions.

func (*Ctx) JSONBlobResponse

func (ctx *Ctx) JSONBlobResponse(body []byte, statusCode ...int)

func (*Ctx) JSONInterfaceResponse

func (ctx *Ctx) JSONInterfaceResponse(data JSONInterface, statusCode ...int)

func (*Ctx) Next

func (ctx *Ctx) Next()

When next used, the next handler will be executed after the current handler's execution.

func (*Ctx) PathName added in v1.0.0

func (ctx *Ctx) PathName() string

func (*Ctx) RequestID added in v1.0.0

func (ctx *Ctx) RequestID() []byte

RequestID returns the request id associated with Ctx

func (*Ctx) URL added in v1.0.0

func (ctx *Ctx) URL() *stdUrl.URL

URL returns the net.URL instance associated with Ctx

type JSONInterface

type JSONInterface interface {
	MarshalJSON() ([]byte, error)
}

type Red

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

func New

func New(cfg Config) *Red

New creates a new instance of Red server.

func (Red) DELETE added in v1.0.0

func (r Red) DELETE(path string, handlers ...RequestHandler)

DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handlers)

func (Red) Defer added in v1.0.0

func (r Red) Defer(handlers ...RequestHandler)

Defer registers given handlers to router Given handlers will be executed after middlewares and handlers by given order

func (Red) GET added in v1.0.0

func (r Red) GET(path string, handlers ...RequestHandler)

GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handlers)

func (Red) HEAD added in v1.0.0

func (r Red) HEAD(path string, handlers ...RequestHandler)

HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handlers)

func (Red) Handle added in v1.0.0

func (r Red) Handle(method, path string, handlers ...RequestHandler)

Handle registers given request handlers with the given path and method There are shortcuts for some methods you can use them.

func (Red) Handler added in v1.0.0

func (r Red) Handler() fasthttp.RequestHandler

Handler gives routers request handler. Returns un-nil function only if the router is a virtual host router.

func (Red) NewGroup added in v1.0.0

func (r Red) NewGroup(path string) Router

NewGroup creates a subrouter for given path.

func (*Red) NewVirtualHost added in v1.0.0

func (r *Red) NewVirtualHost(hostName string) Router

NewVirtualHost creates a virtual host for given hostName.

func (Red) PATCH added in v1.0.0

func (r Red) PATCH(path string, handlers ...RequestHandler)

PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handlers)

func (Red) POST added in v1.0.0

func (r Red) POST(path string, handlers ...RequestHandler)

POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handlers)

func (Red) PUT added in v1.0.0

func (r Red) PUT(path string, handlers ...RequestHandler)

PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handlers)

func (*Red) Serve

func (r *Red) Serve(ln net.Listener) error

Serve serves the server with given listener.

func (*Red) Shutdown

func (r *Red) Shutdown()

Shutdown shuts the server.

func (Red) TRACE added in v1.0.0

func (r Red) TRACE(path string, handlers ...RequestHandler)

TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handlers)

func (Red) Use added in v1.0.0

func (r Red) Use(handlers ...RequestHandler)

Use registers given middleware handlers to router Given handlers will be executed by given order

type RequestHandler

type RequestHandler func(*Ctx)

func ConvertFastHTTPHandler added in v1.0.0

func ConvertFastHTTPHandler(handler fasthttp.RequestHandler) RequestHandler

func ConvertStdHTTPHandler added in v1.0.0

func ConvertStdHTTPHandler(handler http.HandlerFunc) RequestHandler

type Router

type Router interface {
	// Handle registers given request handlers with the given path and method
	// There are shortcuts for some methods you can use them.
	Handle(method string, path string, handlers ...RequestHandler)

	// Use registers given middleware handlers to router
	// Given handlers will be executed by given order
	Use(handlers ...RequestHandler)

	// Defer registers given handlers to router
	// Given handlers will be executed after middlewares and handlers by given order
	Defer(handlers ...RequestHandler)

	// GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handlers)
	GET(path string, handlers ...RequestHandler)

	// POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handlers)
	POST(path string, handlers ...RequestHandler)

	// PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handlers)
	PUT(path string, handlers ...RequestHandler)

	// PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handlers)
	PATCH(path string, handlers ...RequestHandler)

	// DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handlers)
	DELETE(path string, handlers ...RequestHandler)

	// HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handlers)
	HEAD(path string, handlers ...RequestHandler)

	// TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handlers)
	TRACE(path string, handlers ...RequestHandler)

	// Handler gives routers request handler.
	// Returns un-nil function only if the router is a virtual host router.
	Handler() fasthttp.RequestHandler

	// NewGroup creates a subrouter for given path.
	NewGroup(path string) Router
}

Directories

Path Synopsis
middleware
nr

Jump to

Keyboard shortcuts

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