rainbows

package module
v0.0.0-...-051bd16 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: GPL-2.0 Imports: 19 Imported by: 5

README

Rainbows

A work in progress easy to use and batteries included web framework for Go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMiddlewareArgTypeMismatch = errors.New("middleware argument mismatches the type signature in the handler")

ErrMiddlewareArgTypeMismatch is thrown when a middleware argument mismatches the type signature in the handler.

View Source
var ErrNoTransformer = errors.New("there is no transformer for the argument type specified")

ErrNoTransformer is thrown when there is no transformer for the argument type.

View Source
var ErrNotFound = errors.New("route not found")

ErrNotFound is the error sent when the router cannot find a page.

View Source
var ErrTooManyArguments = errors.New("the function signature has too many request arguments")

ErrTooManyArguments is thrown when the function signature has too many request arguments.

View Source
var ErrTooManyMiddlewareArguments = errors.New("too many arguments for the function signature")

ErrTooManyMiddlewareArguments is thrown when middleware generates too many arguments for the function signature.

View Source
var ErrUnknownContentType = errors.New("rainbows doesn't know how to handle the content type specified by the client")

ErrUnknownContentType is thrown when Rainbows doesn't know how to handle the content type specified by the client.

Functions

func AddBinder

func AddBinder(ContentType string, Binder func([]byte, interface{}) error)

AddBinder allows you to set a binder for a request with a custom content type. A blank content type will mean that this will be the default binder if the user does not specify the content type.

func AddTransformer

func AddTransformer(typeOf reflect.Type, transformer Transformer)

AddTransformer is used to add a transformer into Rainbows.

func DELETE

func DELETE(function interface{}, middleware ...interface{})

DELETE is used to register a HTTP DELETE route.

func GET

func GET(function interface{}, middleware ...interface{})

GET is used to register a HTTP GET route.

func Methods

func Methods(Route string) []string

Methods is used to expose all the imported methods for a specific HTTP route.

func PATCH

func PATCH(function interface{}, middleware ...interface{})

PATCH is used to register a HTTP PATCH route.

func POST

func POST(function interface{}, middleware ...interface{})

POST is used to register a HTTP POST route.

func PUT

func PUT(function interface{}, middleware ...interface{})

PUT is used to register a HTTP PUT route.

func Root

func Root(Folder string)

Root is used to define a root folder which Rainbows will use.

func SetCORSRules

func SetCORSRules(rules *CORSRules)

SetCORSRules is used to configure CORS rules for a specific path.

func Start

func Start(Addr string)

Start is used to launch the application.

Types

type CORSRules

type CORSRules struct {
	// Defines which routes are CORS.
	GET, POST, PUT, DELETE bool

	// Defines the origin which can be used.
	OriginRoute string
}

CORSRules are used to define the CORS rules.

func CORSAll

func CORSAll() *CORSRules

CORSAll returns a CORSRules instance in which all methods/origins can use it.

type ErrorHandlerFunc

type ErrorHandlerFunc func(req Request, err error) *Response

ErrorHandlerFunc is the function signature used to handle errors.

var ErrorHandler ErrorHandlerFunc = func(req Request, err error) *Response {
	if err == ErrNotFound {
		r, _ := Text("Route not found.", 404)
		return r
	}
	_ = fmt.Errorf("❌ %s", err)
	r, _ := Text("Internal Server Error", 500)
	return r
}

ErrorHandler is the function which is called on error.

type FunctionCall

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

FunctionCall is used to call the route function.

func (*FunctionCall) Run

func (f *FunctionCall) Run(additionalArgs ...interface{}) (*Response, error)

Run is used to perform the function call with the additional args specified prepended to the handler.

type Request

type Request interface {
	// Headers is used to get a interface which can be used to manage the request headers.
	Headers() RequestHeaders

	// Bind is used to bind the request body to the interface specified.
	Bind(iface interface{}) error

	// Path is used to get the raw path.
	Path() []byte
}

Request is used to define a request manager.

type RequestHeaders

type RequestHeaders interface {
	// Get is used to get a requests headers as bytes.
	// Note that the bytes should not be used after the request is done.
	Get(Key string) []byte

	// GetString is used to get a requests headers as a string.
	GetString(Key string) string

	// Set is used to set the headers in the request. Note this does not edit the response.
	// This is mainly designed to be used in middleware.
	Set(Key, Value string)

	// Keys is used to get the keys of headers.
	Keys() []string
}

RequestHeaders is used to define a manager for request headers.

type Response

type Response struct {
	// WebsocketHandler is defined if the result is a WebSocket.
	WebsocketHandler func(*websocket.Conn) error

	// StatusCode is used to define the status code which should be sent back.
	StatusCode int

	// Reader is used to define the reader which should be sent back.
	Reader io.ReadCloser

	// Headers are the headers which should be sent back with the response.
	Headers map[string][]byte
}

Response is the response type which is sent back.

func HTML

func HTML(HTMLContent string, StatusCode int) (*Response, error)

HTML is used to write a HTML response.

func JSON

func JSON(content interface{}, StatusCode int) (*Response, error)

JSON is used to write a JSON response.

func NoContent

func NoContent() (*Response, error)

NoContent returns a 204.

func Text

func Text(Text string, StatusCode int) (*Response, error)

Text is used to write a text response.

func WebSocket

func WebSocket(handler func(conn *websocket.Conn) error) (*Response, error)

WebSocket is used to respond with a WebSocket stream if possible. Note that if there was an error with the socket, it will be put through the error handler but the error response will not be sent.

func XML

func XML(content interface{}, StatusCode int) (*Response, error)

XML is used to write a XML response.

type Transformer

type Transformer func([]byte) (iface interface{}, ok bool, err error)

Transformer is used to define a type used to transform a param argument.

Directories

Path Synopsis
cli module
internal

Jump to

Keyboard shortcuts

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