mak

package module
v0.0.0-...-61367d4 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2018 License: MPL-2.0 Imports: 30 Imported by: 0

README

mak: it's tame and comfortable, are you?

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is the standard 404 error
	ErrNotFound = MakeErr(404, "not found")
	// ErrMethodNotAllowed is the standard 405 error
	ErrMethodNotAllowed = MakeErr(405, "method not allowed")
	// ErrIndeterminateData for when reflection goes wrong or there is malformed data
	ErrIndeterminateData = MakeErr(400, "unparsible or malformed data")
	// ErrUnsupportedMediaType for when a media type cannot be handled
	ErrUnsupportedMediaType = MakeErr(415, "unsupported media type")
	// ErrRequestBodyEmpty request's body content is absent; malformed POST most likely.
	ErrRequestBodyEmpty = MakeErr(400, "request body empty, cannot proceed")
	// ErrBadRange is for when the Requested Range is Not Satisfiable (out of bounds or such)
	ErrBadRange = MakeErr(416, "unsatisfiable range")
	// ErrPreConditionFail a precondition for request completion has not been met
	ErrPreConditionFail = MakeErr(412, "precondition failed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	AppName         string `json:"appname,omitempty" toml:"appname,omitempty"`
	Domain          string `json:"domain,omitempty" toml:"domain,omitempty"`
	MaintainerEmail string `json:"maintainer_email,omitempty" toml:"maintainer_email,omitempty"`

	DevMode bool `json:"devmode,omitempty" toml:"devmode,omitempty"`

	Address                string `json:"address" toml:"address"`
	SecondaryServerAddress string `json:"secondary_server_address" toml:"secondary_server_address"`

	DevAddress                string `json:"dev_address,omitempty" toml:"dev_address,omitempty"`
	DevSecondaryServerAddress string `json:"dev_secondary_server_address,omitempty" toml:"dev_secondary_server_address,omitempty"`

	PreferMsgpack bool `json:"prefer_msgpack,omitempty" toml:"prefer_msgpack,omitempty"`

	AutoPush bool `json:"autopush,omitempty" toml:"autopush,omitempty"`

	AutoCert    bool     `json:"autocert,omitempty" toml:"autocert,omitempty"`
	DevAutoCert bool     `json:"dev_autocert,omitempty" toml:"dev_autocert,omitempty"`
	Whitelist   []string `json:"whitelist,omitempty" toml:"whitelist,omitempty"`

	TLSKey  string `json:"tls_key,omitempty" toml:"tls_key,omitempty"`
	TLSCert string `json:"tls_cert,omitempty" toml:"tls_cert,omitempty"`

	Assets string `json:"assets,omitempty" toml:"assets,omitempty"`

	Private string `json:"private,omitempty" toml:"private,omitempty"`

	Cache string `json:"cache,omitempty" toml:"cache,omitempty"`

	Raw map[string]interface{} `json:"-" toml:"-"`
}

Config holds all the information necessary to fire up a mak instance

type Cookie = http.Cookie

Cookie is alias for http.Cookie

type Ctx

type Ctx struct {
	R             *http.Request
	W             http.ResponseWriter
	Body          io.Writer
	ContentLength int64
	Written       bool
	Status        int
	Path          string
	// contains filtered or unexported fields
}

Ctx handler context

func (*Ctx) Bind

func (c *Ctx) Bind(v interface{}) error

Bind interprets/parses/unmarshalls either the Request body (POST) or other Params (GET).

func (*Ctx) ClientAddress

func (c *Ctx) ClientAddress() string

ClientAddress returns the original network address that sent the r.

func (*Ctx) ContentType

func (c *Ctx) ContentType() string

ContentType returns the value of the Content-Type header

func (*Ctx) Cookie

func (c *Ctx) Cookie(name string) string

Cookie returns a cookie's value if present and an empty string if not

func (*Ctx) Cookies

func (c *Ctx) Cookies() []*Cookie

Cookies returns a slice of cookies present in the request

func (*Ctx) DelHeader

func (c *Ctx) DelHeader(name string)

DelHeader removes/deletes a header

func (*Ctx) GetContentType

func (c *Ctx) GetContentType() string

GetContentType returns the value of the Content-Type header

func (*Ctx) GetCookie

func (c *Ctx) GetCookie(name string) *Cookie

GetCookie returns an *http.Cookie matching a name and nil if no such cookie exists

func (*Ctx) GetHeader

func (c *Ctx) GetHeader(name string) string

GetHeader reads a header off what's already set in (http.ResponseWriter).Header()

func (*Ctx) Header

func (c *Ctx) Header(name string) string

Header reads a header off the http.Request

func (*Ctx) Headers

func (c *Ctx) Headers() http.Header

Headers gets the headers

func (*Ctx) Param

func (c *Ctx) Param(name string) *RequestParam

Param returns the matched `RequestParam` for the name. It returns nil if not found.

func (*Ctx) Params

func (c *Ctx) Params() []*RequestParam

Params returns all the `RequestParam` in the c.

func (*Ctx) Push

func (c *Ctx) Push(target string, headers map[string]string) error

Push initiates an HTTP/2 server push. This constructs a synthetic request using the target and headers, serializes that request into a PUSH_PROMISE frame, then dispatches that request using the server's request handlec. The target must either be an absolute path (like "/path") or an absolute URL that contains a valid host and the same scheme as the parent request. If the target is a path, it will inherit the scheme and host of the parent request. The headers specifies additional promised request headers. The headers cannot include HTTP/2 pseudo headers like ":path" and ":scheme", which will be added automatically.

func (*Ctx) Query

func (c *Ctx) Query(name string) string

Query returns the query param for the provided name.

func (*Ctx) QueryParams

func (c *Ctx) QueryParams() netURL.Values

QueryParams returns the query parameters as `url.Values`.

func (*Ctx) QueryString

func (c *Ctx) QueryString() string

QueryString returns the raw URL query string.

func (*Ctx) Redirect

func (c *Ctx) Redirect(url string) error

Redirect responds to the client with a redirection to the url.

func (*Ctx) RemoteAddress

func (c *Ctx) RemoteAddress() string

RemoteAddress returns the last network address that sent the r.

func (*Ctx) SetContentType

func (c *Ctx) SetContentType(ct string)

SetContentType sets the http response's Content-Type header

func (*Ctx) SetCookie

func (c *Ctx) SetCookie(name string, cookie *Cookie)

SetCookie sets a new *http.Cookie on the response

func (*Ctx) SetHeader

func (c *Ctx) SetHeader(name string, value string)

SetHeader sets a header

func (*Ctx) SetHeaderValue

func (c *Ctx) SetHeaderValue(name string, values ...string)

SetHeaderValue sets a header's value(s)

func (*Ctx) SetHeaderValues

func (c *Ctx) SetHeaderValues(name string, values []string)

SetHeaderValues sets a header's values

func (*Ctx) SetRawHeader

func (c *Ctx) SetRawHeader(name string, values []string)

SetRawHeader sets a header

func (*Ctx) SetStatus

func (c *Ctx) SetStatus(code int)

SetStatus sets the http response's status code

func (*Ctx) Write

func (c *Ctx) Write(content io.ReadSeeker) error

Write responds to the client with the content.

func (*Ctx) WriteBlob

func (c *Ctx) WriteBlob(b []byte) error

WriteBlob responds to the client with the content b.

func (*Ctx) WriteFile

func (c *Ctx) WriteFile(filename string) error

WriteFile responds to the client with a file content with the filename.

func (*Ctx) WriteHTML

func (c *Ctx) WriteHTML(h string) error

WriteHTML responds to the client with the "text/html" content h.

func (*Ctx) WriteJSON

func (c *Ctx) WriteJSON(v interface{}) error

WriteJSON responds to the client with the "application/json" content v.

func (*Ctx) WriteMsgpack

func (c *Ctx) WriteMsgpack(v interface{}) error

WriteMsgpack responds to the client with the "application/msgpack" content v.

func (*Ctx) WriteProtobuf

func (c *Ctx) WriteProtobuf(v interface{}) error

WriteProtobuf responds to the client with the "application/protobuf" content v.

func (*Ctx) WriteString

func (c *Ctx) WriteString(s string) error

WriteString responds to the client with the "text/plain" content s.

func (*Ctx) WriteTOML

func (c *Ctx) WriteTOML(v interface{}) error

WriteTOML responds to the client with the "application/toml" content v.

func (*Ctx) WriteXML

func (c *Ctx) WriteXML(v interface{}) error

WriteXML responds to the client with the "application/xml" content v.

type Err

type Err struct {
	Code  int    `json:"code" msgpack:"code"`
	Value string `json:"err" msgpack:"err"`
}

Err is mak's standard error type

func MakeErr

func MakeErr(code int, value string) *Err

MakeErr generates a new Mak error totally compatible with conventional go errors

func (*Err) Envoy

func (err *Err) Envoy(c *Ctx) error

Envoy set's the context's status code and just the returns the error as representative of what happened. Unlike Send, Envoy leaves writing to other handlers down the line.

func (*Err) Error

func (err *Err) Error() string

func (*Err) Send

func (err *Err) Send(c *Ctx) error

Send an error response through the context

type Handler

type Handler func(c *Ctx) error

Handler defines a function to serve requests.

type Instance

type Instance struct {
	Server          *http.Server
	SecondaryServer *http.Server

	Config    *Config
	RawConfig map[string]interface{}

	Router *Router

	Middleware    []Middleware
	PreMiddleware []Middleware

	AssetWares []Middleware

	SecondaryServerHandler http.Handler

	AutoCert *autocert.Manager

	ErrorHandler func(*Ctx, error) error

	NotFoundHandler func(*Ctx) error
}

Instance is a mak server with all the trinkets and bells

func Make

func Make(conf *Config) *Instance

Make Creates a new mak Instance

func MakeFromConf

func MakeFromConf(location string) *Instance

MakeFromConf read's a config file for configuration instructions instead of the usual user/manually generated *Config

func (*Instance) AddHTTPWare

func (in *Instance) AddHTTPWare(wares ...func(http.Handler) http.Handler)

AddHTTPWare adds plain http middleware(s) to the instance

func (*Instance) AddPreHTTPWare

func (in *Instance) AddPreHTTPWare(wares ...func(http.Handler) http.Handler)

AddPreHTTPWare adds plain http middleware(s) to the instance

func (*Instance) AddPreWare

func (in *Instance) AddPreWare(wares ...Middleware)

AddPreWare adds middleware(s) to the instance, wares that run before all the others

func (*Instance) AddWare

func (in *Instance) AddWare(wares ...Middleware)

AddWare adds middleware(s) to the instance

func (*Instance) CONNECT

func (in *Instance) CONNECT(path string, h Handler, wares ...Middleware)

CONNECT registers a new CONNECT route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) DELETE

func (in *Instance) DELETE(path string, h Handler, wares ...Middleware)

DELETE registers a new DELETE route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) FILE

func (in *Instance) FILE(path, filename string, wares ...Middleware)

FILE registers a new route with the path to serve a static file with the filename and the optional route-level gases.

func (*Instance) GET

func (in *Instance) GET(path string, h Handler, wares ...Middleware)

GET registers a new GET route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) HEAD

func (in *Instance) HEAD(path string, h Handler, wares ...Middleware)

HEAD registers a new HEAD route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) OPTIONS

func (in *Instance) OPTIONS(path string, h Handler, wares ...Middleware)

OPTIONS registers a new OPTIONS route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) PATCH

func (in *Instance) PATCH(path string, h Handler, wares ...Middleware)

PATCH registers a new PATCH route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) POST

func (in *Instance) POST(path string, h Handler, wares ...Middleware)

POST registers a new POST route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) PUT

func (in *Instance) PUT(path string, h Handler, wares ...Middleware)

PUT registers a new PUT route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) Run

func (in *Instance) Run() error

Run let's the mak instance's purpose actuate, until it dies or is otherwise stopped

func (*Instance) STATIC

func (in *Instance) STATIC(prefix, root string, wares ...Middleware)

STATIC registers a new route with the path prefix to serve the static files from the root with the optional route-level gases.

func (*Instance) ServeHTTP

func (in *Instance) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP implements the `http.Handler`.

func (*Instance) Stop

func (in *Instance) Stop() error

Stop makes the mak instance run no longer

func (*Instance) TRACE

func (in *Instance) TRACE(path string, h Handler, wares ...Middleware)

TRACE registers a new TRACE route for the path with the matching h in the router with the optional route-level gases.

func (*Instance) TimelyStop

func (in *Instance) TimelyStop(when time.Duration, postStop func())

TimelyStop makes the mak instance run no longer, when specified

type Middleware

type Middleware func(Handler) Handler

Middleware defines a function to process gases.

func WrapHTTPMiddleware

func WrapHTTPMiddleware(m func(http.Handler) http.Handler) Middleware

WrapHTTPMiddleware is a convenience method allowing the use of conventional `http.Handler` middleware by wrapping it and internally converting the middleware into a `Gas`.

type Node

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

Node is the Node of the radix tree.

type NodeKind

type NodeKind uint8

NodeKind is a kind of the `Node`.

type RequestParam

type RequestParam struct {
	Name   string
	Values []*RequestParamValue
}

RequestParam is an HTTP request param.

func (*RequestParam) Value

func (rp *RequestParam) Value() *RequestParamValue

Value returns the first value of the rp. It returns nil if the rp is nil or there are no values.

type RequestParamFileValue

type RequestParamFileValue struct {
	Filename      string
	ContentLength int64
	// contains filtered or unexported fields
}

RequestParamFileValue is an HTTP request param file value.

func (*RequestParamFileValue) Read

func (v *RequestParamFileValue) Read(b []byte) (int, error)

Read implements the `io.Reader`.

func (*RequestParamFileValue) Seek

func (v *RequestParamFileValue) Seek(offset int64, whence int) (int64, error)

Seek implements the `io.Seeker`.

type RequestParamValue

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

RequestParamValue is an HTTP request param value.

func (*RequestParamValue) Bool

func (rpv *RequestParamValue) Bool() (bool, error)

Bool returns a `bool` from the rpv's underlying value.

func (*RequestParamValue) File

File returns a `RequestParamFileValue` from the rpv's underlying value.

func (*RequestParamValue) Float32

func (rpv *RequestParamValue) Float32() (float32, error)

Float32 returns a `float32` from the rpv's underlying value.

func (*RequestParamValue) Float64

func (rpv *RequestParamValue) Float64() (float64, error)

Float64 returns a `float64` from the rpv's underlying value.

func (*RequestParamValue) Int

func (rpv *RequestParamValue) Int() (int, error)

Int returns an `int` from the rpv's underlying value.

func (*RequestParamValue) Int8

func (rpv *RequestParamValue) Int8() (int8, error)

Int8 returns an `int8` from the rpv's underlying value.

func (*RequestParamValue) Int16

func (rpv *RequestParamValue) Int16() (int16, error)

Int16 returns an `int16` from the rpv's underlying value.

func (*RequestParamValue) Int32

func (rpv *RequestParamValue) Int32() (int32, error)

Int32 returns an `int32` from the rpv's underlying value.

func (*RequestParamValue) Int64

func (rpv *RequestParamValue) Int64() (int64, error)

Int64 returns an `int64` from the rpv's underlying value.

func (*RequestParamValue) String

func (rpv *RequestParamValue) String() string

String returns a `string` from the rpv's underlying value.

func (*RequestParamValue) Uint

func (rpv *RequestParamValue) Uint() (uint, error)

Uint returns an `uint` from the rpv's underlying value.

func (*RequestParamValue) Uint8

func (rpv *RequestParamValue) Uint8() (uint8, error)

Uint8 returns an `uint8` from the rpv's underlying value.

func (*RequestParamValue) Uint16

func (rpv *RequestParamValue) Uint16() (uint16, error)

Uint16 returns an `uint16` from the rpv's underlying value.

func (*RequestParamValue) Uint32

func (rpv *RequestParamValue) Uint32() (uint32, error)

Uint32 returns an `uint32` from the rpv's underlying value.

func (*RequestParamValue) Uint64

func (rpv *RequestParamValue) Uint64() (uint64, error)

Uint64 returns an `uint64` from the rpv's underlying value.

type Router

type Router struct {
	Tree      *Node
	MaxParams int
	Routes    map[string]bool
}

Router is a registry of all Registered routes.

func MakeRouter

func MakeRouter() *Router

MakeRouter generates a new Router, ready for use

func (*Router) Insert

func (r *Router) Insert(
	method,
	path string,
	h Handler,
	nk NodeKind,
	paramNames []string,
)

Insert Inserts a new route into the `tree` of the r.

func (*Router) Register

func (r *Router) Register(method, path string, h Handler, wares ...Middleware)

Register Registers a new route for the method and the path with the matching h in the r with the optional route-level (middle)wares.

func (*Router) Route

func (r *Router) Route(c *Ctx) Handler

Route returns a handler Registered for the c.R.

Jump to

Keyboard shortcuts

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