Documentation
¶
Overview ¶
Package router The zappwork Golang router and is an easy to use router with pre/post processing middleware.
Serving static files ¶
Serving static files is easy, see the example below.
package main import ( "bitbucket.org/zappwork/router" ) func main() { rt := NewRouter(nil) rt.StaticFile("/", "./assets/index.html") rt.StaticFile("/favicon.ico", "./assets/favicon.ico") rt.StaticFolder("/css/", "./assets/css/") rt.StaticFolder("/js/", "./assets/js/") rt.StaticFolder("/img/", "./assets/img/") rt.Start(nil) }
Some examples of how to use the routing ¶
Catch request for file.html and request method GET
rt.Get("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Catch request for file.html and request method POST
rt.Post("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Catch request for file.html and request method PUT
rt.Put("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Catch request for file.html and request method DELETE
rt.Delete("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Catch request for file.html and request method OPTIONS
rt.Options("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Catch request for file.html and request method PATCH
rt.Patch("/file.html", func(r router.RouteRequest) (err *router.Error) { fmt.Fprintf(r.Writer(), r.Method()+" "+r.Path()) return })
Index ¶
- func ParseRemoteAddr(r *http.Request) (ipAddr string)
- type Config
- type DefaultLogger
- func (d DefaultLogger) Debug(args ...interface{})
- func (d DefaultLogger) Debugf(format string, args ...interface{})
- func (d DefaultLogger) Error(args ...interface{})
- func (d DefaultLogger) Errorf(format string, args ...interface{})
- func (d DefaultLogger) Info(args ...interface{})
- func (d DefaultLogger) Infof(format string, args ...interface{})
- func (d DefaultLogger) Request(uuid, method, path string, status int, duration, ip, useragent string, ...)
- type Error
- type Func
- type Logger
- type PostMap
- type RouteGroup
- func (g *RouteGroup) Catch(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Delete(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Get(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Group(gr *RouteGroup) *RouteGroup
- func (g *RouteGroup) Options(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Patch(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Path() string
- func (g *RouteGroup) Post(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) Put(path string, f Func, use ...Func) *RouteGroup
- func (g *RouteGroup) SetPath(path string)
- func (g *RouteGroup) Use(f ...Func) *RouteGroup
- type RouteRequest
- func (rr *RouteRequest) Accept() string
- func (rr *RouteRequest) AddHeader(name, value string)
- func (rr *RouteRequest) AskAuthentication(realm string)
- func (rr *RouteRequest) Context(key string) interface{}
- func (rr *RouteRequest) Cookie(name string) (*http.Cookie, error)
- func (rr *RouteRequest) Cookies() []*http.Cookie
- func (rr *RouteRequest) Credentials() (user, password string, ok bool)
- func (rr *RouteRequest) Domain() string
- func (rr *RouteRequest) HasQueryParam(key string) bool
- func (rr *RouteRequest) Header(h string) string
- func (rr *RouteRequest) Host() string
- func (rr *RouteRequest) IPAddress() string
- func (rr *RouteRequest) Method() string
- func (rr *RouteRequest) Path() string
- func (rr *RouteRequest) PathArg(key string) (arg string, ok bool)
- func (rr *RouteRequest) PathArgAsBool(key string) (bool, bool)
- func (rr *RouteRequest) PathArgAsInt(key string) (int, bool)
- func (rr *RouteRequest) PathArgAsInt64(key string) (arg int64, ok bool)
- func (rr *RouteRequest) PostArrayMap(key string) (arg map[int]PostMap, ok bool)
- func (rr *RouteRequest) PostMap(key string) (arg PostMap, ok bool)
- func (rr *RouteRequest) PostMultiValue(key string) ([]string, bool)
- func (rr *RouteRequest) PostValue(key string) (string, bool)
- func (rr *RouteRequest) PostValueAsBool(key string) (bool, bool)
- func (rr *RouteRequest) PostValueAsFloat64(key string) (float64, bool)
- func (rr *RouteRequest) PostValueAsInt(key string) (int, bool)
- func (rr *RouteRequest) PostValueAsInt64(key string) (int64, bool)
- func (rr *RouteRequest) QueryParam(key string) (string, bool)
- func (rr *RouteRequest) QueryParamAsBool(key string) (bool, bool)
- func (rr *RouteRequest) QueryParamAsFloat64(key string) (float64, bool)
- func (rr *RouteRequest) QueryParamAsInt64(key string) (int64, bool)
- func (rr *RouteRequest) QueryParamAsSlice(key, seperator string) (s []string, ok bool)
- func (rr *RouteRequest) QueryParamAsTime(key, layout string) (*time.Time, bool)
- func (rr *RouteRequest) QueryParamAsUint64(key string) (uint64, bool)
- func (rr *RouteRequest) RawPostBytes() (data []byte, err error)
- func (rr *RouteRequest) RawPostString() (data string, err error)
- func (rr *RouteRequest) ReadJSON(v interface{}) (err error)
- func (rr *RouteRequest) ReadXML(v interface{}) (err error)
- func (rr *RouteRequest) Redirect(path string, permanent bool)
- func (rr *RouteRequest) Request() *http.Request
- func (rr *RouteRequest) SetContext(key string, value interface{})
- func (rr *RouteRequest) SetCookie(cookie *http.Cookie)
- func (rr *RouteRequest) SetStatusCode(code int)
- func (rr *RouteRequest) StatusCode() int
- func (rr *RouteRequest) UnmarshalRawPost(v interface{}) (err error)
- func (rr *RouteRequest) Write(s string) (err *Error)
- func (rr *RouteRequest) WriteBuffer(b *bytes.Buffer) (err *Error)
- func (rr *RouteRequest) WriteBytes(b []byte) (err *Error)
- func (rr *RouteRequest) WriteJSON(v interface{}) (err *Error)
- func (rr *RouteRequest) WriteStatusCode(code int)
- func (rr *RouteRequest) WriteXML(v interface{}) (err *Error)
- func (rr *RouteRequest) Writer() http.ResponseWriter
- type Router
- func (r *Router) AddCatchAllHandler(handler Func)
- func (r *Router) AddErrorHandler(handler func(http.ResponseWriter, *http.Request, *Error))
- func (r *Router) AddNotFoundHandler(handler Func)
- func (r *Router) Catch(path string, f Func, use ...Func) *Router
- func (r *Router) Delete(path string, f Func, use ...Func) *Router
- func (r *Router) Get(path string, f Func, use ...Func) *Router
- func (r *Router) Group(g *RouteGroup) *Router
- func (r *Router) Handler() (handler http.Handler)
- func (r *Router) Options(path string, f Func, use ...Func) *Router
- func (r *Router) Patch(path string, f Func, use ...Func) *Router
- func (r *Router) Post(path string, f Func, use ...Func) *Router
- func (r *Router) Put(path string, f Func, use ...Func) *Router
- func (r *Router) Reset()
- func (r *Router) SetLogger(l Logger)
- func (r *Router) Start(c *Config, keepAlive bool)
- func (r *Router) StaticFile(path string, serve string) *Router
- func (r *Router) StaticFolder(base string, serve string) *Router
- func (r *Router) Stop(message string)
- func (r *Router) Use(f Func) *Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseRemoteAddr ¶
ParseRemoteAddr parse a remoteAddr to a human readable ip address
Types ¶
type Config ¶
type Config struct { Host string Port int StaticTimeout int RequestTimeout time.Duration TimeoutMessage string Logging bool Verbose bool ShowSystemInfo bool CorsEnabled bool CorsAllowOrigin string CorsAllowHeaders string CorsAllowMethods string }
Config foro router package
func (*Config) HostString ¶
HostString get the host string for the server
type DefaultLogger ¶
type DefaultLogger struct{}
DefaultLogger is the default logger
func (DefaultLogger) Debug ¶
func (d DefaultLogger) Debug(args ...interface{})
Debug logs debug information
func (DefaultLogger) Debugf ¶
func (d DefaultLogger) Debugf(format string, args ...interface{})
Debugf logs information
func (DefaultLogger) Error ¶
func (d DefaultLogger) Error(args ...interface{})
Error logs error information
func (DefaultLogger) Errorf ¶
func (d DefaultLogger) Errorf(format string, args ...interface{})
Errorf logs information
func (DefaultLogger) Infof ¶
func (d DefaultLogger) Infof(format string, args ...interface{})
Infof logs information
type Error ¶
Error generic router error
func NewJSONError ¶
NewJSONError returns an initialized error where the message interface is marshalled as and json string
type Func ¶
type Func func(*RouteRequest) *Error
Func is the generic router function used in the application
type Logger ¶
type Logger interface { Request(string, string, string, int, string, string, string, *Error) Info(args ...interface{}) Infof(format string, args ...interface{}) Error(args ...interface{}) Errorf(format string, args ...interface{}) Debug(args ...interface{}) Debugf(format string, args ...interface{}) }
Logger interface
type RouteGroup ¶
type RouteGroup struct {
// contains filtered or unexported fields
}
RouteGroup is grouping routes
func (*RouteGroup) Catch ¶
func (g *RouteGroup) Catch(path string, f Func, use ...Func) *RouteGroup
Catch a route independent of the request method and specify the handling function and/or controller
func (*RouteGroup) Delete ¶
func (g *RouteGroup) Delete(path string, f Func, use ...Func) *RouteGroup
Delete add a new route DELETE route to the router and specify the handling function and/or controller
func (*RouteGroup) Get ¶
func (g *RouteGroup) Get(path string, f Func, use ...Func) *RouteGroup
Get add a new route GET route to the router and specify the handling function and/or controller
func (*RouteGroup) Group ¶
func (g *RouteGroup) Group(gr *RouteGroup) *RouteGroup
Group add a grouped route on the given path
func (*RouteGroup) Options ¶
func (g *RouteGroup) Options(path string, f Func, use ...Func) *RouteGroup
Options add a new route OPTIONS route to the router and specify the handling function and/or controller
func (*RouteGroup) Patch ¶
func (g *RouteGroup) Patch(path string, f Func, use ...Func) *RouteGroup
Patch add a new route PATCH route to the router and specify the handling function and/or controller
func (*RouteGroup) Post ¶
func (g *RouteGroup) Post(path string, f Func, use ...Func) *RouteGroup
Post add a new route POST route to the router and specify the handling function and/or controller
func (*RouteGroup) Put ¶
func (g *RouteGroup) Put(path string, f Func, use ...Func) *RouteGroup
Put add a new route PUT route to the router and specify the handling function and/or controller
func (*RouteGroup) Use ¶
func (g *RouteGroup) Use(f ...Func) *RouteGroup
Use add middleware specific for this group
type RouteRequest ¶
type RouteRequest struct {
// contains filtered or unexported fields
}
RouteRequest struct used in the handler function
func (*RouteRequest) Accept ¶
func (rr *RouteRequest) Accept() string
Accept returns the type of response requested
func (*RouteRequest) AddHeader ¶
func (rr *RouteRequest) AddHeader(name, value string)
AddHeader to the response
func (*RouteRequest) AskAuthentication ¶
func (rr *RouteRequest) AskAuthentication(realm string)
AskAuthentication ask credentials for the given realm
func (*RouteRequest) Context ¶
func (rr *RouteRequest) Context(key string) interface{}
Context gets a context key value pairing
func (*RouteRequest) Cookie ¶
func (rr *RouteRequest) Cookie(name string) (*http.Cookie, error)
Cookie returns the named cookie provided in the request or ErrNoCookie if not found
func (*RouteRequest) Cookies ¶
func (rr *RouteRequest) Cookies() []*http.Cookie
Cookies parses and returns the HTTP cookies sent with the request
func (*RouteRequest) Credentials ¶
func (rr *RouteRequest) Credentials() (user, password string, ok bool)
Credentials get the credetials for authentication
func (*RouteRequest) Domain ¶
func (rr *RouteRequest) Domain() string
Domain return the tld for the current host
func (*RouteRequest) HasQueryParam ¶
func (rr *RouteRequest) HasQueryParam(key string) bool
HasQueryParam does the request have a query param
func (*RouteRequest) Header ¶
func (rr *RouteRequest) Header(h string) string
Header get a request header
func (*RouteRequest) IPAddress ¶
func (rr *RouteRequest) IPAddress() string
IPAddress get the remote IP and port used
func (*RouteRequest) Method ¶
func (rr *RouteRequest) Method() string
Method get the response writer to handle the response
func (*RouteRequest) PathArg ¶
func (rr *RouteRequest) PathArg(key string) (arg string, ok bool)
PathArg get the route/path arguments which were dynamic. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PathArgAsBool ¶
func (rr *RouteRequest) PathArgAsBool(key string) (bool, bool)
PathArgAsBool get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PathArgAsInt ¶
func (rr *RouteRequest) PathArgAsInt(key string) (int, bool)
PathArgAsInt get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PathArgAsInt64 ¶
func (rr *RouteRequest) PathArgAsInt64(key string) (arg int64, ok bool)
PathArgAsInt64 get the route/path arguments which were dynamic. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostArrayMap ¶
func (rr *RouteRequest) PostArrayMap(key string) (arg map[int]PostMap, ok bool)
PostArrayMap get the post ArrayMap posted in the from. They are int -> key/value pair and can be retrieved through this function
func (*RouteRequest) PostMap ¶
func (rr *RouteRequest) PostMap(key string) (arg PostMap, ok bool)
PostMap get the post ArrayMap posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostMultiValue ¶
func (rr *RouteRequest) PostMultiValue(key string) ([]string, bool)
PostMultiValue get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostValue ¶
func (rr *RouteRequest) PostValue(key string) (string, bool)
PostValue get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostValueAsBool ¶
func (rr *RouteRequest) PostValueAsBool(key string) (bool, bool)
PostValueAsBool get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostValueAsFloat64 ¶
func (rr *RouteRequest) PostValueAsFloat64(key string) (float64, bool)
PostValueAsFloat64 get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostValueAsInt ¶
func (rr *RouteRequest) PostValueAsInt(key string) (int, bool)
PostValueAsInt get the post params posted in the from. They are key/value pair and can be retrieved through this function
func (*RouteRequest) PostValueAsInt64 ¶
func (rr *RouteRequest) PostValueAsInt64(key string) (int64, bool)
PostValueAsInt64 get the post params posted in the form
func (*RouteRequest) QueryParam ¶
func (rr *RouteRequest) QueryParam(key string) (string, bool)
QueryParam get the query params as given in the url. They are key/value pair and can be retrieved through this function
func (*RouteRequest) QueryParamAsBool ¶
func (rr *RouteRequest) QueryParamAsBool(key string) (bool, bool)
QueryParamAsBool get query param as an float64
func (*RouteRequest) QueryParamAsFloat64 ¶
func (rr *RouteRequest) QueryParamAsFloat64(key string) (float64, bool)
QueryParamAsFloat64 get query param as an float64
func (*RouteRequest) QueryParamAsInt64 ¶
func (rr *RouteRequest) QueryParamAsInt64(key string) (int64, bool)
QueryParamAsInt64 get query param as an int64
func (*RouteRequest) QueryParamAsSlice ¶
func (rr *RouteRequest) QueryParamAsSlice(key, seperator string) (s []string, ok bool)
QueryParamAsSlice get query param as an float64
func (*RouteRequest) QueryParamAsTime ¶
func (rr *RouteRequest) QueryParamAsTime(key, layout string) (*time.Time, bool)
QueryParamAsTime get query param as a time.Time
func (*RouteRequest) QueryParamAsUint64 ¶
func (rr *RouteRequest) QueryParamAsUint64(key string) (uint64, bool)
QueryParamAsUint64 get query param as an uint64
func (*RouteRequest) RawPostBytes ¶
func (rr *RouteRequest) RawPostBytes() (data []byte, err error)
RawPostBytes get posted body as a string.
func (*RouteRequest) RawPostString ¶
func (rr *RouteRequest) RawPostString() (data string, err error)
RawPostString get posted body as a byte slice.
func (*RouteRequest) ReadJSON ¶
func (rr *RouteRequest) ReadJSON(v interface{}) (err error)
ReadJSON get the raw Json information
func (*RouteRequest) ReadXML ¶
func (rr *RouteRequest) ReadXML(v interface{}) (err error)
ReadXML get Unmarshal posted json into an interface.
func (*RouteRequest) Redirect ¶
func (rr *RouteRequest) Redirect(path string, permanent bool)
Redirect from RouteRequest to the given path
func (*RouteRequest) Request ¶
func (rr *RouteRequest) Request() *http.Request
Request get the original call request. This should not be needed for regular requests, but can be used if needed.
func (*RouteRequest) SetContext ¶
func (rr *RouteRequest) SetContext(key string, value interface{})
SetContext sets a context key value pairing
func (*RouteRequest) SetCookie ¶
func (rr *RouteRequest) SetCookie(cookie *http.Cookie)
SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers
func (*RouteRequest) SetStatusCode ¶
func (rr *RouteRequest) SetStatusCode(code int)
SetStatusCode set the return status code
func (*RouteRequest) StatusCode ¶
func (rr *RouteRequest) StatusCode() int
StatusCode get the status code
func (*RouteRequest) UnmarshalRawPost ¶
func (rr *RouteRequest) UnmarshalRawPost(v interface{}) (err error)
UnmarshalRawPost get Unmarshal posted json into an interface.
func (*RouteRequest) Write ¶
func (rr *RouteRequest) Write(s string) (err *Error)
Write interface as JSON to client
func (*RouteRequest) WriteBuffer ¶
func (rr *RouteRequest) WriteBuffer(b *bytes.Buffer) (err *Error)
WriteBuffer write bytes buffer to client
func (*RouteRequest) WriteBytes ¶
func (rr *RouteRequest) WriteBytes(b []byte) (err *Error)
WriteBytes write bytes to client
func (*RouteRequest) WriteJSON ¶
func (rr *RouteRequest) WriteJSON(v interface{}) (err *Error)
WriteJSON write interface as JSON to client
func (*RouteRequest) WriteStatusCode ¶
func (rr *RouteRequest) WriteStatusCode(code int)
WriteStatusCode set write the status code
func (*RouteRequest) WriteXML ¶
func (rr *RouteRequest) WriteXML(v interface{}) (err *Error)
WriteXML write interface as XML to client
func (*RouteRequest) Writer ¶
func (rr *RouteRequest) Writer() http.ResponseWriter
Writer get the response writer to handle the response
type Router ¶
type Router struct { // Unique number for the current router UUID string // OnStart custom start funcion else the default start message will be displayed OnStart func() // OnStop custom stop function else the default stop message will be displayed OnStop func(string) // contains filtered or unexported fields }
Router is a new Router instance
func (*Router) AddCatchAllHandler ¶
AddCatchAllHandler add a catch all unhandeled router to the router.
func (*Router) AddErrorHandler ¶
AddErrorHandler add custom error handler. This will enable you to show custom 500 error pages.
func (*Router) AddNotFoundHandler ¶
AddNotFoundHandler add a custom NotFound handler to the router. This will enable you serve custom 404 pages.
func (*Router) Catch ¶
Catch a route independent of the request method and specify the handling function and/or controller
func (*Router) Delete ¶
Delete add a new route DELETE route to the router and specify the handling function and/or controller
func (*Router) Get ¶
Get add a new route GET route to the router and specify the handling function and/or controller
func (*Router) Group ¶
func (r *Router) Group(g *RouteGroup) *Router
Group add a grouped route on the given path
func (*Router) Options ¶
Options add a new route OPTIONS route to the router and specify the handling function and/or controller
func (*Router) Patch ¶
Patch add a new route PATCH route to the router and specify the handling function and/or controller
func (*Router) Post ¶
Post add a new route POST route to the router and specify the handling function and/or controller
func (*Router) Put ¶
Put add a new route PUT route to the router and specify the handling function and/or controller
func (*Router) StaticFile ¶
StaticFile add a single file to serve through the router.
func (*Router) StaticFolder ¶
StaticFolder add static folder paths. This should be folders and paths will be cleaned correctly. And example would be: rt.AddStatic("/js/", "./assets/default/js") The first argument is the base path as defined in the html and the seconds is the path to the files on the server.