httpserver

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2016 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderContentType = "Content-Type"
	ContentTypeHTML   = "text/html; charset=utf-8"
	ContentTypeJSON   = "application/json; charset=utf-8"
	ContentTypeText   = "text/plain; charset=utf-8"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request  *http.Request
	Response Response
	Params   Params                 // Params from the path (eg. /thing/:id)
	Locals   map[string]interface{} // Local values set by middleware
	Debug    bool
	// contains filtered or unexported fields
}

Context manages the control flow of middleware

func (*Context) Clear

func (c *Context) Clear(res http.ResponseWriter)

Clear resets the context so it can be used by another request

func (*Context) ClientIp

func (c *Context) ClientIp() string

ClientIp implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.

func (*Context) ContinueRequest

func (c *Context) ContinueRequest()

ContinueRequest asks the server to call the next handler for this request after the current handler function has returned. This should be preferred over PerformRequest().Now() because it keeps the program stack simpler.

func (*Context) GetLocal

func (c *Context) GetLocal(key string) (value interface{}, exists bool)

GetLocal returns the value for the given key

func (*Context) MustGetLocal

func (c *Context) MustGetLocal(key string) interface{}

Returns the value for the given key if it exists, otherwise it panics.

func (*Context) PerformRequest

func (c *Context) PerformRequest()

PerformRequest is used to handle the request immediately. It can be used instead of ContinueRequest when it must perform logic after the normal request handler has run. If neither method is called, then the server does not run any more request handlers.

PerformRequest should be used to initiate the request for the first time; if there are no handlers when PerformRequest is called, Go will panic with an index out of range runtime error.

func (*Context) SetLocal

func (c *Context) SetLocal(key string, value interface{})

SetLocal is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Locals if it was not used previously.

type HandlerFunc

type HandlerFunc func(c *Context)

type HandlersChain

type HandlersChain []HandlerFunc

func (HandlersChain) Last

func (chain HandlersChain) Last() HandlerFunc

Last returns the last handler in the chain. ie. the last handler is the main one.

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the server. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) (va string)

func (Params) Get

func (ps Params) Get(name string) (string, bool)

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type Response

type Response struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*Response) Clear

func (r *Response) Clear(writer http.ResponseWriter)

func (*Response) CloseNotify

func (r *Response) CloseNotify() <-chan bool

Implements the http.CloseNotify interface

func (*Response) Flush

func (r *Response) Flush()

Implements the http.Flush interface

func (*Response) HEAD

func (r *Response) HEAD(status int)

func (*Response) HTML

func (r *Response) HTML(status int, html string) (err error)

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Implements the http.Hijacker interface

func (*Response) JSON

func (r *Response) JSON(status int, obj interface{}) (err error)

func (*Response) Render

func (r *Response) Render(status int, contentType string)

func (*Response) Rendered

func (r *Response) Rendered() bool

func (*Response) Status

func (r *Response) Status() int

func (*Response) Text

func (r *Response) Text(status int, text string) (err error)

func (*Response) WriteHeader

func (r *Response) WriteHeader(status int)

Override http.ResponseWriter's WriteHeader method

type RouteGroup

type RouteGroup struct {
	Handlers HandlersChain
	// contains filtered or unexported fields
}

RouteGroup is used internally to configure a server, a RouteGroup is associated with a prefix and an array of handlers (middleware)

func (*RouteGroup) Any

func (group *RouteGroup) Any(relativePath string, handlers ...HandlerFunc) RouteHandler

Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE

func (*RouteGroup) AnyRaw

func (group *RouteGroup) AnyRaw(relativePath string, raw http.Handler) RouteHandler

AnyRaw registers a route matching all methods, but handled with a raw http.Handler

func (*RouteGroup) BasePath

func (group *RouteGroup) BasePath() string

func (*RouteGroup) DELETE

func (group *RouteGroup) DELETE(relativePath string, handlers ...HandlerFunc) RouteHandler

Delete is a shortcut for server.Handle("DELETE", path, handle)

func (*RouteGroup) Directory

func (group *RouteGroup) Directory(relativePath, root string) RouteHandler

Directory serves files from the given file system directory. eg. router.Directory("/", "/var/www")

func (*RouteGroup) File

func (group *RouteGroup) File(relativePath, filepath string) RouteHandler

File registers a single route in order to server a single file of the local filesystem. eg. router.File("favicon.ico", "./resources/favicon.ico")

func (*RouteGroup) GET

func (group *RouteGroup) GET(relativePath string, handlers ...HandlerFunc) RouteHandler

Get is a shortcut for server.Handle("GET", path, handle)

func (*RouteGroup) Group

func (group *RouteGroup) Group(relativePath string, handlers ...HandlerFunc) *RouteGroup

Group creates a new server group. You should add all the routes that have common middlwares or the same path prefix. For example, all the routes that use a common middlware for authorization could be grouped.

func (*RouteGroup) HEAD

func (group *RouteGroup) HEAD(relativePath string, handlers ...HandlerFunc) RouteHandler

Head is a shortcut for server.Handle("HEAD", path, handle)

func (*RouteGroup) Handle

func (group *RouteGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) RouteHandler

Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in github.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*RouteGroup) HandleRaw

func (group *RouteGroup) HandleRaw(httpMethod, relativePath string, raw http.Handler) RouteHandler

HandleRaw registers a new request handler using a normal http.Handler

func (*RouteGroup) OPTIONS

func (group *RouteGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) RouteHandler

Options is a shortcut for server.Handle("OPTIONS", path, handle)

func (*RouteGroup) PATCH

func (group *RouteGroup) PATCH(relativePath string, handlers ...HandlerFunc) RouteHandler

Patch is a shortcut for server.Handle("PATCH", path, handle)

func (*RouteGroup) POST

func (group *RouteGroup) POST(relativePath string, handlers ...HandlerFunc) RouteHandler

Post is a shortcut for server.Handle("POST", path, handle)

func (*RouteGroup) PUT

func (group *RouteGroup) PUT(relativePath string, handlers ...HandlerFunc) RouteHandler

Put is a shortcut for server.Handle("PUT", path, handle)

func (*RouteGroup) Use

func (group *RouteGroup) Use(middleware ...HandlerFunc) RouteHandler

Use adds middleware to the group, see example code in github.

type RouteInfo

type RouteInfo struct {
	Method  string
	Path    string
	Handler string
}

type Server

type Server struct {
	RouteGroup

	DebugEnabled bool
	// contains filtered or unexported fields
}

Server supports configure middleware and routing for a handler Create an instance of Server, by using New()

func New

func New() *Server

New returns a new blank Server instance without any middleware attached

func (*Server) AllowedMethods

func (s *Server) AllowedMethods(path string) []string

AllowedMethods returns a slice of HTTP methods that are allowed for a path

func (*Server) DescribeOptions

func (s *Server) DescribeOptions(handlers ...HandlerFunc)

DescribeOptions registers a global handler chain for OPTIONS requests, with the "Allow" header set automatically to list allowed HTTP methods for the route.

func (*Server) IsAvailable

func (s *Server) IsAvailable() bool

IsAvailable returns whether the server is available or not. If the server is not available, the unavailable handler will be called instead of using the normal routing rules.

func (*Server) NoMethod

func (s *Server) NoMethod(handlers ...HandlerFunc)

NoMethod registers a handler chain for requests with a method that isn't allowed for a path

func (*Server) NotFound

func (s *Server) NotFound(handlers ...HandlerFunc)

NotFound registers a handler chain for requests with a path that does not exist

func (*Server) Routes

func (s *Server) Routes() (routes []RouteInfo)

Routes returns a slice of registered routes, including some useful information, such as: the http method, path and the handler name.

func (*Server) Run

func (s *Server) Run(addr ...string) error

Run attaches the server to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, server)

func (*Server) RunTLS

func (s *Server) RunTLS(addr string, certFile string, keyFile string) (err error)

RunTLS attaches the server to a http.Server and starts listening and serving HTTPS (secure) requests. It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, server)

func (*Server) RunUnix

func (s *Server) RunUnix(file string) (err error)

RunUnix attaches the server to a http.Server and starts listening and serving HTTP requests through the specified unix socket (ie. a file).

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

Conforms to the http.Handler interface.

func (*Server) SetAvailable

func (s *Server) SetAvailable(available bool)

SetAvailable toggles whether the server is available or not. If the server is not available, the unavailable handler will be called instead of using the normal routing rules.

func (*Server) Unavailable

func (s *Server) Unavailable(handlers ...HandlerFunc)

Unavailable registers a handler chain for requests received while the server is marked unavailable

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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