lloyd

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 12 Imported by: 2

README

Lloyd

Build Status GoDoc 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:
    • Before middlewares
    • After middlewares
  • Responses:
    • MarshalJSON interface support (it'd be 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

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

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

func ReleaseURL(url *stdUrl.URL)

ReleaseURL returns URL acquired via AcquireURL to pool.

It is forbidden accessing url after releasing it

func S2B

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

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

func (ctx *Ctx) PathName() string

func (*Ctx) RequestID

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

RequestID returns the request id associated with Ctx

func (*Ctx) ResponseWriter added in v1.2.0

func (ctx *Ctx) ResponseWriter() http.ResponseWriter

ResponseWriter returns the http.ResponseWriter instance associated with Ctx

func (*Ctx) URL

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

URL returns the net.URL instance associated with Ctx

type JSONInterface

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

type Lloyd

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

func New

func New(cfg Config) *Lloyd

New creates a new instance of Lloyd server.

func (Lloyd) DELETE

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

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

func (Lloyd) Defer

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

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

func (Lloyd) GET

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

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

func (Lloyd) HEAD

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

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

func (Lloyd) Handle

func (r Lloyd) 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 (Lloyd) Handler

func (r Lloyd) Handler() fasthttp.RequestHandler

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

func (Lloyd) NewGroup

func (r Lloyd) NewGroup(path string) Router

NewGroup creates a subrouter for given path.

func (*Lloyd) NewVirtualHost

func (l *Lloyd) NewVirtualHost(hostName string) Router

NewVirtualHost creates a virtual host for given hostName.

func (Lloyd) PATCH

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

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

func (Lloyd) POST

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

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

func (Lloyd) PUT

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

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

func (*Lloyd) Serve

func (l *Lloyd) Serve(ln net.Listener) error

Serve serves the server with given listener.

func (*Lloyd) Shutdown

func (l *Lloyd) Shutdown()

Shutdown shuts the server.

func (Lloyd) TRACE

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

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

func (Lloyd) Use

func (r Lloyd) 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

func ConvertFastHTTPHandler(handler fasthttp.RequestHandler) RequestHandler

func ConvertStdHTTPHandler

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