Documentation
¶
Index ¶
- Constants
- Variables
- func AddFormatter(formatter Formatter)
- func ClearFormatters()
- func ConfigureDefaultFormatters()
- func ListenAndServe(pattern string) error
- func ListenAndServeRoutes(pattern string, r *RouteManager) error
- func ListenAndServeRoutesTLS(pattern string, certFile string, keyFile string, r *RouteManager) error
- func ListenAndServeTLS(pattern string, certFile string, keyFile string) error
- func MapRest(pathPrefix string, controller RestController)
- func MapStatic(pathPrefix string, rootDirectory string)
- func UnmarshalForm(form url.Values, v interface{}) error
- type Context
- func (cx *Context) Fill(v interface{}) error
- func (c *Context) GetCallback() string
- func (c *Context) GetRequestContext() string
- func (c *Context) IsDelete() bool
- func (c *Context) IsGet() bool
- func (c *Context) IsOptions() bool
- func (c *Context) IsPost() bool
- func (c *Context) IsPut() bool
- func (c *Context) Respond(data interface{}, statusCode int, errors []string, context *Context) error
- func (c *Context) RespondWithData(data interface{}) error
- func (c *Context) RespondWithError(statusCode int) error
- func (c *Context) RespondWithErrorMessage(message string, statusCode int) error
- func (c *Context) RespondWithLocation(location string) error
- func (c *Context) RespondWithNotFound() error
- func (c *Context) RespondWithNotImplemented() error
- func (c *Context) RespondWithOK() error
- func (c *Context) RespondWithPaginatedData(data interface{}, limit, offset, count int) error
- func (c *Context) RespondWithStatus(statusCode int) error
- func (c *Context) WriteResponse(obj interface{}, statusCode int) error
- type Controller
- type ControllerFunc
- type FormRequestDecoder
- type Formatter
- type HttpHandler
- type JsonFormatter
- type JsonRequestDecoder
- type ParameterKeyMap
- type ParameterValueMap
- type RequestDecoder
- type RestController
- type RestCreator
- type RestCreatorWithId
- type RestDeleter
- type RestManyDeleter
- type RestManyReader
- type RestManyUpdater
- type RestOptions
- type RestReader
- type RestUpdater
- type Route
- type RouteManager
- func (manager *RouteManager) AddRoute(route *Route)
- func (manager *RouteManager) ClearRoutes()
- func (manager *RouteManager) Map(path string, controller Controller, matcherFuncs ...RouteMatcherFunc) *Route
- func (manager *RouteManager) MapFunc(path string, contorllerFunction func(*Context), ...) *Route
- func (manager *RouteManager) MapRest(pathPrefix string, controller RestController)
- type RouteMatcherFunc
- type RouteMatcherFuncValue
- type XmlRequestDecoder
Constants ¶
const DEFAULT_FORMAT string = JSON_FORMAT
The fallback format if one cannot be determined by the request
const DELETE_HTTP_METHOD string = "DELETE"
const ERR_NO_CONTROLLER string = "Routes must have a valid Controller"
const ERR_NO_MATCHING_ROUTE string = "No route found for that path"
const ERR_STANDARD_PREFIX string = "Oops, something went wrong: "
Error messages
const GET_HTTP_METHOD string = "GET"
HTTP Methods
const HTML_FORMAT string = "HTML"
Constant string for HTML format
const JSONP_CONTENT_TYPE string = "text/javascript"
JSONP Content-Type header value
const JSON_FORMAT string = "JSON"
Constant string for JSON format
const OPTIONS_HTTP_METHOD string = "OPTIONS"
const POST_HTTP_METHOD string = "POST"
const PUT_HTTP_METHOD string = "PUT"
const REQUEST_ALWAYS200_PARAMETER string = "always200"
Parameter name for Always200
const REQUEST_CALLBACK_PARAMETER string = "callback"
Parameter name for callback
const REQUEST_CONTEXT_PARAMETER string = "context"
Parameter name for Request Context
const REQUEST_METHOD_OVERRIDE_PARAMETER string = "method"
Parameter name for method override
const XML_FORMAT string = "XML"
Constant string for XML format
Variables ¶
var ROUTE_REGEX_PLACEHOLDER string = "(.*)"
Regex placeholder pattern
Functions ¶
func ClearFormatters ¶
func ClearFormatters()
Clears all formatters (including default internal ones)
func ConfigureDefaultFormatters ¶
func ConfigureDefaultFormatters()
Adds the default formatters to goweb so that
func ListenAndServe ¶
Listens for incomming requests and handles them using the DefaultHttpHandler
The same as:
http.ListenAndServe(pattern, DefaultHttpHandler)
for more information see http.ListenAndServe
A typical example:
func main() { goweb.Map("/people", peopleController) goweb.ListenAndServe(":8080") }
func ListenAndServeRoutes ¶
func ListenAndServeRoutes(pattern string, r *RouteManager) error
func ListenAndServeRoutesTLS ¶
func ListenAndServeRoutesTLS(pattern string, certFile string, keyFile string, r *RouteManager) error
func ListenAndServeTLS ¶
func MapRest ¶
func MapRest(pathPrefix string, controller RestController)
Maps an entire RESTful set of routes to the specified RestController You only have to specify the methods that you require see rest_controller.go for the list of interfaces that can be satisfied
func UnmarshalForm ¶
Fill a struct `v` from the values in `form`
Types ¶
type Context ¶
type Context struct { // The underlying http.Request for this context Request *http.Request // The underlying http.ResponseWriter for this context ResponseWriter http.ResponseWriter // A ParameterValueMap containing path parameters PathParams ParameterValueMap // The format that the response should be Format string }
Object holding details about the request and responses
func (*Context) Fill ¶
goweb.Context Helper function to fill a variable with the contents of the request body. The body will be decoded based on the content-type and an apropriate RequestDecoder automatically selected
func (*Context) GetCallback ¶
Gets the callback value from the request
func (*Context) GetRequestContext ¶
Gets the context value from the request
func (*Context) Respond ¶
func (c *Context) Respond(data interface{}, statusCode int, errors []string, context *Context) error
RespondWith* methods
func (*Context) RespondWithData ¶
Responds with the specified data
func (*Context) RespondWithError ¶
Responds with the specified HTTP status code defined in RFC 2616 and adds the description to the errors list see http://golang.org/src/pkg/http/status.go for options
func (*Context) RespondWithErrorMessage ¶
func (*Context) RespondWithLocation ¶
Responds with 302 Temporarily Moved (redirect)
func (*Context) RespondWithNotFound ¶
Responds with 404 Not Found
func (*Context) RespondWithNotImplemented ¶
Responds with 501 Not Implemented
func (*Context) RespondWithOK ¶
Responds with OK status (200) and no data
func (*Context) RespondWithPaginatedData ¶
func (*Context) RespondWithStatus ¶
Responds with the specified HTTP status code defined in RFC 2616 see http://golang.org/src/pkg/http/status.go for options
func (*Context) WriteResponse ¶
Writes the specified object out (with the specified status code) using the appropriate formatter
type Controller ¶
type Controller interface { // When implemented, handles the request HandleRequest(c *Context) }
Interface for controller types that handle requests
type ControllerFunc ¶
type ControllerFunc func(*Context)
The ControllerFunc type is an adapter to allow the use of ordinary functions as goweb handlers. If f is a function with the appropriate signature, ControllerFunc(f) is a Controller object that calls f.
func (ControllerFunc) HandleRequest ¶
func (f ControllerFunc) HandleRequest(c *Context)
HandleRequest calls f(c).
type FormRequestDecoder ¶
type FormRequestDecoder struct{}
a form-enc decoder for request body
func (*FormRequestDecoder) Unmarshal ¶
func (d *FormRequestDecoder) Unmarshal(cx *Context, v interface{}) error
type Formatter ¶
type Formatter interface { // method to transform response Format(context *Context, input interface{}) ([]uint8, error) // method that decides if this formatter will be used Match(*Context) bool }
Interface describing an object responsible for handling transformed/formatted response data
func GetFormatter ¶
Gets the relevant formatter for the specified context or returns an error if no formatter is found
type HttpHandler ¶
type HttpHandler struct {
// contains filtered or unexported fields
}
A handler type to handle actual http requests using the DefaultRouteManager to route requests to the right places
var DefaultHttpHandler *HttpHandler = &HttpHandler{routeManager: DefaultRouteManager}
The default http handler used to handle requests
func (*HttpHandler) GetMathingRoute ¶
func (h *HttpHandler) GetMathingRoute(responseWriter http.ResponseWriter, request *http.Request) (bool, *Route, *Context)
Searches DefaultRouteManager to find the first matching route and returns it along with a boolean describing whether any routes were found or not, and the Context object built while searching for routes
func (*HttpHandler) HandleError ¶
func (h *HttpHandler) HandleError(context *Context, err error)
Handles the specified error by passing it back to the user
func (*HttpHandler) ServeHTTP ¶
func (handler *HttpHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request)
Serves the HTTP request and writes the response to the specified writer
type JsonFormatter ¶
type JsonFormatter struct{}
Formatter for JSON
func (*JsonFormatter) Format ¶
func (f *JsonFormatter) Format(cx *Context, input interface{}) ([]uint8, error)
Readies response and converts input data into JSON
func (*JsonFormatter) Match ¶
func (f *JsonFormatter) Match(cx *Context) bool
Gets the "application/json" content type
type JsonRequestDecoder ¶
type JsonRequestDecoder struct{}
a JSON decoder for request body (just a wrapper to json.Unmarshal)
func (*JsonRequestDecoder) Unmarshal ¶
func (d *JsonRequestDecoder) Unmarshal(cx *Context, v interface{}) error
type ParameterKeyMap ¶
Holds parameter keys and their respective positions in the path
type ParameterValueMap ¶
Holds parameter keys and actual values
type RequestDecoder ¶
types that impliment RequestDecoder can unmarshal the request body into an apropriate type/struct
type RestCreator ¶
type RestCreator interface {
Create(c *Context)
}
Handler method to create a new item
type RestCreatorWithId ¶
Handler method to create a new item with an ID
type RestDeleter ¶
Handler method to delete an item specified by the ID
type RestManyDeleter ¶
type RestManyDeleter interface {
DeleteMany(c *Context)
}
Handler method to delete a collection of items
type RestManyReader ¶
type RestManyReader interface {
ReadMany(c *Context)
}
Handler method to read many items
type RestManyUpdater ¶
type RestManyUpdater interface {
UpdateMany(c *Context)
}
Handler method to update many items
type RestOptions ¶
type RestOptions interface {
Options(c *Context)
}
type RestReader ¶
Handler method to read an item by the specified ID
type RestUpdater ¶
Handler method to update a single item specified by the ID
type Route ¶
type Route struct { Path string Controller Controller MatcherFuncs []RouteMatcherFunc // contains filtered or unexported fields }
Represents a single route mapping
func Map ¶
func Map(path string, controller Controller, matcherFuncs ...RouteMatcherFunc) *Route
Maps a new route to a controller (with optional RouteMatcherFuncs) and returns the new route
func MapFunc ¶
func MapFunc(path string, controllerFunc func(*Context), matcherFuncs ...RouteMatcherFunc) *Route
Maps a new route to a function (with optional RouteMarcherFuncs) and returns the new route
func (*Route) DoesMatchContext ¶
Checks whether the context for this request matches the route
func (*Route) DoesMatchPath ¶
Checks whether a path matches a route or not
type RouteManager ¶
type RouteManager struct {
// contains filtered or unexported fields
}
Manages routes and matching
var DefaultRouteManager *RouteManager = new(RouteManager)
Default instance of the RouteManager
func (*RouteManager) AddRoute ¶
func (manager *RouteManager) AddRoute(route *Route)
Adds a route to the manager
func (*RouteManager) Map ¶
func (manager *RouteManager) Map(path string, controller Controller, matcherFuncs ...RouteMatcherFunc) *Route
Creates a route that maps the specified path to the specified controller along with any optional RouteMatcherFunc modifiers
func (*RouteManager) MapFunc ¶
func (manager *RouteManager) MapFunc(path string, contorllerFunction func(*Context), matcherFuncs ...RouteMatcherFunc) *Route
func (*RouteManager) MapRest ¶
func (manager *RouteManager) MapRest(pathPrefix string, controller RestController)
type RouteMatcherFunc ¶
type RouteMatcherFunc func(c *Context) RouteMatcherFuncValue
Functions used to decide whether a route matches a request or not
type RouteMatcherFuncValue ¶
type RouteMatcherFuncValue int
Represents the return value for RouteMatcher functions
const DontCare RouteMatcherFuncValue = -1
Indicates that the RouteMatcherFunc doesn't care whether the route is a Match or NoMatch
const Match RouteMatcherFuncValue = 1
Indicates that the route should match
const NoMatch RouteMatcherFuncValue = 0
Indicates that the route should NOT match
func DeleteMethod ¶
func DeleteMethod(c *Context) RouteMatcherFuncValue
Returns Match if the Method of the http.Request in the specified Context is DELETE, otherwise returns DontCare
func GetMethod ¶
func GetMethod(c *Context) RouteMatcherFuncValue
Returns Match if the Method of the http.Request in the specified Context is GET, otherwise returns DontCare
func OptionsMethod ¶
func OptionsMethod(c *Context) RouteMatcherFuncValue
Returns Match if the Method of the http.Request in the specified Context is OPTIONS, otherwise returns DontCare
func PostMethod ¶
func PostMethod(c *Context) RouteMatcherFuncValue
Returns Match if the Method of the http.Request in the specified Context is POST, otherwise returns DontCare
func PutMethod ¶
func PutMethod(c *Context) RouteMatcherFuncValue
Returns Match if the Method of the http.Request in the specified Context is PUT, otherwise returns DontCare
type XmlRequestDecoder ¶
type XmlRequestDecoder struct{}
an XML decoder for request body
func (*XmlRequestDecoder) Unmarshal ¶
func (d *XmlRequestDecoder) Unmarshal(cx *Context, v interface{}) error