Documentation
¶
Overview ¶
Package gokoa is a Koa styled web framework written in Go.
package main import ( "github.com/azxj/gokoa" ) func main() { app := gokoa.NewApplication(nil) app.Use(func(ctx *gokoa.Context, fn func() error) error { ctx.SetBody("hello gokoa") return nil }) app.Listen(8080) }
Index ¶
- type Application
- type ApplicationConfig
- type Context
- type ErrorHandler
- type Middleware
- type Request
- type Response
- func (response *Response) Get(field string) string
- func (response *Response) GetBody() []byte
- func (response *Response) GetLength() int
- func (response *Response) GetStatus() int
- func (response *Response) Has(field string) bool
- func (response *Response) Remove(field string)
- func (response *Response) Set(field string, value string)
- func (response *Response) SetBody(body interface{}) error
- func (response *Response) SetLength(length int)
- func (response *Response) SetStatus(statusCode int)
- func (response *Response) SetType(contentType string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct { // Env is the deploying environment variable, default to GOKOA_ENV // or "development". Env string // Keys is the signed cookie keys, which will be used to sign and // verify client cookies, default to an empty array. Keys []string // Proxy is equal to true when fields in the proxy header will be // trusted, default to false. Proxy bool // SubdomainOffset is the offset of subdomain to be ignored, default // to 0 (means no ignoring). SubdomainOffset int // contains filtered or unexported fields }
An Application represents a HTTP web server, which handle HTTP requests by processing HTTP responses.
func NewApplication ¶
func NewApplication(config ApplicationConfig) *Application
NewApplication returns a new Application initialized with the given config.
The config can be nil, which causes the Application to use default configuration settings.
Values in key-value pairs must be in the valid type, otherwise NewApplication will panic.
func (*Application) Callback ¶
func (app *Application) Callback() func(res http.ResponseWriter, req *http.Request)
Callback returns a function that composes all middlewares registered into the Application, creates a new Context, a new Request and a new Response for an incoming connection, and handles this HTTP request.
func (*Application) Listen ¶
func (app *Application) Listen(port int) (*http.Server, error)
Listen causes the Application to create a new HTTP server with a single handler which composes all middlewares registered in, and listen on the given TCP port for incoming connections.
Listen returns the created http.Server when ListenAndServe() does NOT returns an error, otherwise returns it.
func (*Application) OnError ¶
func (app *Application) OnError(handler ErrorHandler)
OnError registers a new ErrorHandler into the Application.
func (*Application) Use ¶
func (app *Application) Use(middleware Middleware) *Application
Use registers the given middleware into the Application.
Use returns the Application itself, which enables chained function call instead of function calls in multiple lines.
type ApplicationConfig ¶
type ApplicationConfig map[string]interface{}
An ApplicationConfig is a container which stores settings for configuring an Application.
The ApplicationConfig is organized as key-value pairs, where the value is of limited type.
type Context ¶
type Context struct { Request *Request Response *Response // State is the recommended namespace for passing information // through different middlewares. State map[string]interface{} // contains filtered or unexported fields }
A Context contains information related to a single HTTP request.
func NewContext ¶
func NewContext() *Context
NewContext returns a new empty Context.
NewContext allocates memory for State, which means that a key-value pair can be directly added to State, without calling make() by yourself.
type ErrorHandler ¶
type ErrorHandler func(err error)
An ErrorHandler is a function, which is responsible for handling error returned by any middleware.
type Middleware ¶
A Middleware is a single function, which will be registered into an Application to be executed during HTTP request handling.
type Request ¶
type Request struct { // Req is the primitive HTTP request. Req *http.Request // contains filtered or unexported fields }
A Request represents a HTTP request received by the Application.
type Response ¶
type Response struct { // Res is the primitive HTTP response. Res http.ResponseWriter // contains filtered or unexported fields }
A Response represents a HTTP response sent back by the Application.
func (*Response) SetBody ¶
SetBody assigns the given object to the HTTP response body.
The given object can be either a string, a byte array, an io.Reader, or a map containing key-value pairs. Whenever what type the given object is, it will be transformed into a byte array.
func (*Response) SetLength ¶
SetLength assigns the given integer to the HTTP response Content-Length header.