raptor

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: MIT Imports: 23 Imported by: 0

README

Raptor Web Framework

Inspired by Laravel and Iris

Why Raptor web framework? We love fasthttp, we love speed. Basically Raptor is using fasthttp, fasthttprouter, ffjson packages under the hood.

Installation

The only requirement is the Go Programming Language

$ go get -u github.com/si3nloong/raptor

Quick Start

package main

import (
  "github.com/si3nloong/raptor"
)

func main() {
  r := raptor.New()
  r.GET("/", func(c *raptor.Context) error {
    return c.Response().Custom(raptor.Map{"message":"hello world"})
  })
  r.Start(":8080")
}

Multiple Hosts

import (
    "github.com/si3nloong/raptor"
)

type host map[string]raptor.HandlerFunc

// Routing is to route to specific domain
func (hs host) Routing(ctx *raptor.Context) error {
  if cb := hosts[string(ctx.Host())]; cb != nil {
    cb = middleware.CORS(corsConfig)(cb)
    return cb(ctx)
  }
  return ctx.Response().NotFound(fmt.Errorf("page not found"))
}

func main() {
  api := raptor.New()
  api.GET("/", func(c *raptor.Context) error {
    return c.Response().Custom(raptor.Map{"message":"hello world"})
  })

  open := raptor.New()
  open.GET("/", func(c *raptor.Context) error {
    return c.Response().Custom(raptor.Map{"message":"hello world"})
  })

  hosts["api.domain.com"] = api.Handler()
  hosts["open.domain.com"] = open.Handler()

  r := raptor.New()
  r.Start(":8080", hosts.Routing)
}

Variable Binding

  api := raptor.New()
  api.GET("/", func(c *raptor.Context) error {
    var i struct {
	    Name string `json:"name" xml:"name" query:"name"`
	  }

    if err := c.Bind(&i); err != nil {
      return c.Response().BadRequest(c.NewAPIError(err))
    }

	  return c.Response().Custom(raptor.Map{"message":"hello world"})
  })
  api.Start(":8080")

Validation

  api := raptor.New()
  api.GET("/", func(c *raptor.Context) error {
    var i struct {
	    Name string `json:"name" xml:"name" query:"name"`
    }

    if err := c.Bind(&i); err != nil {
      return c.Response().BadRequest(c.NewAPIError(err))
    }

    if message, err := c.Validate(&i); err != nil {
      return c.Response().UnprocessableEntity(c.NewAPIError(err, "", message))
    }

	  return c.Response().Custom(raptor.Map{"message":"hello world"})
  })
  api.Start(":8080")

Error Handling

Custom Error

MIT License

Documentation

Index

Constants

View Source
const (
	MIMEApplicationJSON       = "application/json"
	MIMEApplicationJavaScript = "application/javascript"
	MIMEApplicationXML        = "application/xml"
	MIMETextXML               = "text/xml"
	MIMEApplicationForm       = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf   = "application/protobuf"
	MIMEApplicationMsgpack    = "application/msgpack"
	MIMETextHTML              = "text/html"
	MIMETextPlain             = "text/plain"
	MIMEMultipartForm         = "multipart/form-data"
	MIMEOctetStream           = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAccept              = "Accept"
	HeaderAcceptCharset       = "Accept-Charset"
	HeaderAcceptEncoding      = "Accept-Encoding"
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderCacheControl        = "Cache-Control"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderConnection          = "Connection"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "Set-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastEventID         = "Last-Event-ID"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXAccelBuffering     = "X-Accel-Buffering"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderXForwardedProto     = "X-Forwarded-Proto"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSsl       = "X-Forwarded-Ssl"
	HeaderXUrlScheme          = "X-Url-Scheme"
	HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
	HeaderXRealIP             = "X-Real-IP"
	HeaderXRequestID          = "X-Request-ID"
	HeaderXRequestedWith      = "X-Requested-With"
	HeaderServer              = "Server"
	HeaderOrigin              = "Origin"
	HeaderUserAgent           = "User-Agent"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// Security
	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderXContentTypeOptions     = "X-Content-Type-Options"
	HeaderXXSSProtection          = "X-XSS-Protection"
	HeaderXFrameOptions           = "X-Frame-Options"
	HeaderContentSecurityPolicy   = "Content-Security-Policy"
	HeaderXCSRFToken              = "X-CSRF-Token"
	HeaderXPoweredBy              = "X-Powered-By"
)

HTTP Headers

View Source
const (
	CONNECT  = "CONNECT"
	DELETE   = "DELETE"
	GET      = "GET"
	HEAD     = "HEAD"
	OPTIONS  = "OPTIONS"
	PATCH    = "PATCH"
	POST     = "POST"
	PROPFIND = "PROPFIND"
	PUT      = "PUT"
	TRACE    = "TRACE"
)

HTTP REQUEST METHODS :

Variables

This section is empty.

Functions

func DefaultErrorHandler added in v0.1.0

func DefaultErrorHandler(ctx *Context, err error)

DefaultErrorHandler :

Types

type APIError

type APIError struct {
	Inner   error
	Code    string
	Message string
	Detail  interface{}
	// contains filtered or unexported fields
}

APIError :

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) MarshalJSON

func (e *APIError) MarshalJSON() (b []byte, err error)

MarshalJSON :

type Context

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

Context :

func (*Context) Bind

func (c *Context) Bind(dst interface{}) error

Bind :

func (*Context) HTML

func (c *Context) HTML(html string, statusCode ...int) error

HTML :

func (*Context) HTMLBlob

func (c *Context) HTMLBlob(b []byte, statusCode ...int) error

HTMLBlob :

func (*Context) IsMethod

func (c *Context) IsMethod(method string) bool

IsMethod :

func (*Context) JSON

func (c *Context) JSON(value interface{}, statusCode ...int) error

JSON :

func (*Context) NewAPIError

func (c *Context) NewAPIError(err error, params ...interface{}) error

NewAPIError :

func (*Context) NoContent

func (c *Context) NoContent(statusCode ...int) error

NoContent :

func (*Context) Param

func (c *Context) Param(key string) (str string)

Param :

func (*Context) QueryString

func (c *Context) QueryString() string

QueryString :

func (*Context) Redirect

func (c *Context) Redirect(uri string, statusCode ...int) error

Redirect :

func (*Context) Render

func (c *Context) Render(b []byte) error

Render :

func (*Context) Response

func (c *Context) Response() *Response

Response :

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie :

func (*Context) Validate

func (c *Context) Validate(i interface{}) error

Validate :

type ErrorResponse

type ErrorResponse interface {
	error
	json.Marshaler
	xml.Marshaler
}

ErrorResponse :

type Group

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

Group :

func (*Group) Any

func (g *Group) Any(path string, h HandlerFunc, m ...MiddlewareFunc)

Any :

func (*Group) CONNECT

func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc)

CONNECT :

func (*Group) DELETE

func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc)

DELETE :

func (*Group) GET

func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc)

GET :

func (*Group) Group

func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group

Group :

func (*Group) HEAD

func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc)

HEAD :

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc)

OPTIONS :

func (*Group) PATCH

func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc)

PATCH :

func (*Group) POST

func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc)

POST :

func (*Group) PUT

func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc)

PUT :

func (*Group) TRACE

func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc)

TRACE :

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    interface{}
	Inner      error
}

HTTPError :

func (*HTTPError) Error

func (e *HTTPError) Error() string

type HandlerFunc

type HandlerFunc func(*Context) error

HandlerFunc :

type Logger

type Logger func(error)

Logger :

type Map

type Map map[string]interface{}

Map :

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc :

type Paginator

type Paginator interface {
	Count() uint
	NextCursor() string
}

Paginator :

type Raptor

type Raptor struct {
	ErrorHandler func(c *Context, err error)
	IsDebug      bool
	// contains filtered or unexported fields
}

Raptor :

func New

func New() *Raptor

New :

func (*Raptor) DELETE

func (r *Raptor) DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

DELETE :

func (*Raptor) GET

func (r *Raptor) GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

GET :

func (*Raptor) Group

func (r *Raptor) Group(path string, middleware ...MiddlewareFunc) *Group

Group :

func (*Raptor) HEAD

func (r *Raptor) HEAD(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

HEAD :

func (*Raptor) Handler

func (r *Raptor) Handler() HandlerFunc

Handler :

func (*Raptor) OPTIONS

func (r *Raptor) OPTIONS(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

OPTIONS :

func (*Raptor) PATCH

func (r *Raptor) PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

PATCH :

func (*Raptor) POST

func (r *Raptor) POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

POST :

func (*Raptor) PUT

func (r *Raptor) PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

PUT :

func (*Raptor) Start

func (r *Raptor) Start(port string, handler ...HandlerFunc) error

Start :

func (*Raptor) StartTLS

func (r *Raptor) StartTLS(port string, certFile, keyFile string, handler ...HandlerFunc) error

StartTLS :

func (*Raptor) Static

func (r *Raptor) Static(prefix, path string) *Raptor

Static :

func (*Raptor) StaticGzip

func (r *Raptor) StaticGzip(prefix, path string) *Raptor

StaticGzip :

func (*Raptor) Use

func (r *Raptor) Use(middleware ...MiddlewareFunc)

Use :

type Response

type Response struct {
	*fasthttp.RequestCtx
}

Response :

func (*Response) BadGateway

func (r *Response) BadGateway(err error) error

BadGateway : 501

func (*Response) BadRequest

func (r *Response) BadRequest(err error) error

BadRequest : 400

func (*Response) Blob

func (r *Response) Blob(contentType string, b []byte, statusCode ...int) error

Blob :

func (*Response) Collection

func (r *Response) Collection(data interface{}) error

Collection :

func (*Response) Conflict

func (r *Response) Conflict(err error) error

Conflict : 409

func (*Response) Custom

func (r *Response) Custom(data interface{}, statusCode ...int) error

Custom :

func (*Response) ExpectationFailed

func (r *Response) ExpectationFailed(err error) error

ExpectationFailed : 417

func (*Response) Forbidden

func (r *Response) Forbidden(err error) error

Forbidden : 403

func (*Response) GatewayTimeout

func (r *Response) GatewayTimeout(err error) error

GatewayTimeout : 504

func (*Response) Gone

func (r *Response) Gone(err error) error

Gone : 410

func (*Response) InternalServerError

func (r *Response) InternalServerError(err error) error

InternalServerError : 500

func (*Response) Item

func (r *Response) Item(data interface{}) error

Item :

func (*Response) JSON

func (r *Response) JSON(data interface{}, statusCode ...int) error

JSON :

func (*Response) LengthRequired

func (r *Response) LengthRequired(err error) error

LengthRequired : 411

func (*Response) Locked

func (r *Response) Locked(err error) error

Locked : 423

func (*Response) MethodNotAllowed

func (r *Response) MethodNotAllowed(err error) error

MethodNotAllowed : 405

func (*Response) NoContent

func (r *Response) NoContent() error

NoContent : 204 responding with no content

func (*Response) NotAcceptable

func (r *Response) NotAcceptable(err error) error

NotAcceptable : 406

func (*Response) NotFound

func (r *Response) NotFound(err error) error

NotFound : 404

func (*Response) Paginate

func (r *Response) Paginate(p Paginator, data interface{}) error

Paginate :

func (*Response) PayloadTooLarge

func (r *Response) PayloadTooLarge(err error) error

PayloadTooLarge : 413

func (*Response) PreconditionFailed

func (r *Response) PreconditionFailed(err error) error

PreconditionFailed : 412

func (*Response) RequestTimeout

func (r *Response) RequestTimeout(err error) error

RequestTimeout : 408

func (*Response) ServiceUnavailable

func (r *Response) ServiceUnavailable(err error) error

ServiceUnavailable : 503

func (*Response) SetStatusCode

func (r *Response) SetStatusCode(statusCode int) *Response

SetStatusCode :

func (*Response) Success

func (r *Response) Success(data interface{}) error

Success :

func (*Response) Unauthorized

func (r *Response) Unauthorized(err error) error

Unauthorized : 401

func (*Response) UnprocessableEntity

func (r *Response) UnprocessableEntity(err error) error

UnprocessableEntity : 422

func (*Response) UnsupportedMediaType

func (r *Response) UnsupportedMediaType(err error) error

UnsupportedMediaType : 415

func (*Response) XML

func (r *Response) XML(data interface{}, statusCode ...int) error

XML :

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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