Documentation ¶
Index ¶
- Constants
- Variables
- func AddFormatter(fs ...Formatter)
- func ClearFormatters()
- func ConfigureDefaultFormatters()
- func DeleteFromSession(obj interface{}, r *http.Request, w http.ResponseWriter)
- func DictArgument(values ...interface{}) map[string]interface{}
- func Divisible(n, b int) bool
- func EnableSession(secret string)
- func Hash(s string) string
- func ListenAndServe(pattern string) error
- func ListenAndServeInServer(server *http.Server) 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 MapFormattedStatic(pathPrefix string, obj interface{})
- func MapFormattedStaticWithFunc(pathPrefix string, f func(cx *Context))
- func MapRest(pathPrefix string, controller RestController)
- func MapStatic(pathPrefix string, rootDirectory string)
- func MarshallInCookieSession(obj interface{}, r *http.Request, w http.ResponseWriter)
- func RawHtml(text string) template.HTML
- func SetDefaultFormat(format string)
- func SetDefaultIndex(path string)
- func SetDocumentRoot(root string)
- func UnmarshalForm(form *map[string][]string, v interface{}, autofill bool) error
- func UnmarshallInCookieSession(obj interface{}, r *http.Request) error
- type Context
- func (c *Context) AddSignedCookie(cookie *http.Cookie) (*http.Cookie, error)
- func (cx *Context) Fill(v interface{}, autofill bool) error
- func (cx *Context) FillAs(v interface{}, autofill bool, ct string) error
- func (c *Context) GetCallback() string
- func (c *Context) GetRequestContext() string
- func (c *Context) IsDelete() bool
- func (c *Context) IsGet() bool
- func (c *Context) IsMobile() 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(err error, statusCode int) error
- func (c *Context) RespondWithErrorCode(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) RespondWithRawData(data interface{}) error
- func (c *Context) RespondWithStatus(statusCode int) error
- func (c *Context) SignedCookie(name string) (*http.Cookie, error)
- func (c *Context) WriteResponse(obj interface{}, statusCode int) error
- type Controller
- type ControllerFunc
- type FormRequestDecoder
- type Formatter
- type GzipResponseWriter
- type HttpHandler
- type JsonFormatter
- type JsonRequestDecoder
- type MobileRestHtmlFormattor
- type MultiFormRequestDecoder
- type ParameterKeyMap
- type ParameterValueMap
- type RawFormatter
- type RequestDecoder
- type RestContext
- type RestController
- type RestCreator
- type RestDeleter
- type RestEditor
- type RestHtmlFormattor
- type RestManyDeleter
- type RestManyReader
- type RestManyUpdater
- type RestModelTemplate
- type RestNewer
- 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
- type RouteMatcherFunc
- type RouteMatcherFuncValue
- type XmlRequestDecoder
Constants ¶
const CREATE_REST_METHOD string = "create"
const DELETEMANY_REST_METHOD string = "deletemany"
const DELETE_HTTP_METHOD string = "DELETE"
const DELETE_REST_METHOD string = "delete"
const EDIT_REST_METHOD string = "edit"
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 MOBILE_FORMAT string = "mbl"
Constant string for HTML format
const NEW_REST_METHOD string = "new"
const OPTIONS_HTTP_METHOD string = "OPTIONS"
const OPTIONS_REST_METHOD string = "options"
const POST_HTTP_METHOD string = "POST"
const PUT_HTTP_METHOD string = "PUT"
const RAW_FORMAT string = "raw"
Constant string for JSON format
const READMANY_REST_METHOD string = "readmany"
const READ_REST_METHOD string = "read"
RESTFUL Methods
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 UPDATEMANY_REST_METHOD string = "updatemany"
const UPDATE_REST_METHOD string = "update"
const XML_FORMAT string = "xml"
Constant string for XML format
Variables ¶
var CookieIsMissing error = errors.New("Cookie is missing")
CookieIsMissing is returned when a cookie is missing.
var CookieNotValid error = errors.New("Cookie and signed cookie do not match")
CookieNotValid is returned when the cookie and its signed counterpart do not match.
I.e. the cookie value has been tampered with.
var DEFAULT_FORMAT string = HTML_FORMAT
The fallback format if one cannot be determined by the request
var HashSecret string = "cX8Os0wfB6uCGZZSZHIi6rKsy7b0scE9"
HashSecret represents a secret string that is used to hash cookies.
For true security, this should be changed for each application.
var ROUTE_REGEX_PLACEHOLDER string = "(.*)"
Regex placeholder pattern
var ( // SignedCookieFormat is the format string used to decide the name of the // signed cookie. SignedCookieFormat string = "%s_signed" )
var SignedCookieIsMissing error = errors.New("Signed cookie is missing")
SignedCookieIsMissing is returned when the signed cookie is missing.
var UseStandardResponse = true
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 DeleteFromSession ¶
func DeleteFromSession(obj interface{}, r *http.Request, w http.ResponseWriter)
func DictArgument ¶
func DictArgument(values ...interface{}) map[string]interface{}
func EnableSession ¶
func EnableSession(secret string)
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 ListenAndServeInServer ¶
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 MapFormattedStatic ¶
func MapFormattedStatic(pathPrefix string, obj interface{})
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 MarshallInCookieSession ¶
func MarshallInCookieSession(obj interface{}, r *http.Request, w http.ResponseWriter)
func SetDefaultFormat ¶
func SetDefaultFormat(format string)
func SetDefaultIndex ¶
func SetDefaultIndex(path string)
func SetDocumentRoot ¶
func SetDocumentRoot(root string)
Set the root for the rest html formating, formating is based on the method name(lower case)
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 // Mark the rest mothod for this context Rest RestContext // Stard Path PathWithOutSuffix string //parsed request bytes RequestData []byte }
Object holding details about the request and responses
func (*Context) AddSignedCookie ¶
AddSignedCookie adds the specified cookie to the response and also adds an additional 'signed' cookie that is used to validate the cookies value when SignedCookie is called.
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, standard bool) error
RespondWith* methods
func (*Context) RespondWithData ¶
Responds with the specified data
func (*Context) RespondWithError ¶
func (*Context) RespondWithErrorCode ¶
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) RespondWithRawData ¶
Responds with the specified data
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) SignedCookie ¶
Gets the cookie specified by name and validates that its value has not been tampered with by checking the signed cookie too. Will return CookieNotValid error if it has been tampered with, otherwise, it will return the actual cookie.
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 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 // init the formatter Init() }
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 GzipResponseWriter ¶
type GzipResponseWriter struct { io.Writer http.ResponseWriter }
type HttpHandler ¶
type HttpHandler struct { IndexPath string // 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, IndexPath: "/"}
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) Init ¶
func (f *JsonFormatter) Init()
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)
type MobileRestHtmlFormattor ¶
type MobileRestHtmlFormattor struct {
RestHtmlFormattor
}
func (*MobileRestHtmlFormattor) Init ¶
func (r *MobileRestHtmlFormattor) Init()
type MultiFormRequestDecoder ¶
type MultiFormRequestDecoder struct{}
a form-enc decoder for request body
type ParameterKeyMap ¶
Holds parameter keys and their respective positions in the path
type ParameterValueMap ¶
Holds parameter keys and actual values
type RawFormatter ¶
type RawFormatter struct{}
Formatter for JSON
func (*RawFormatter) Format ¶
func (f *RawFormatter) Format(cx *Context, input interface{}) (output []uint8, err error)
Readies response and converts input data into JSON
func (*RawFormatter) Init ¶
func (f *RawFormatter) Init()
func (*RawFormatter) Match ¶
func (f *RawFormatter) Match(cx *Context) bool
Gets the "application/json" content type
type RequestDecoder ¶
types that impliment RequestDecoder can unmarshal the request body into an apropriate type/struct
type RestContext ¶
type RestCreator ¶
type RestCreator interface {
Create(c *Context)
}
Handler method to create a new item
type RestDeleter ¶
Handler method to delete an item specified by the ID
type RestEditor ¶
Handler method to show the Edit page for item
type RestHtmlFormattor ¶
type RestHtmlFormattor struct {
// contains filtered or unexported fields
}
Formatter for JSON
func (*RestHtmlFormattor) Format ¶
func (f *RestHtmlFormattor) Format(cx *Context, input interface{}) ([]uint8, error)
Readies response and converts input data into JSON
func (*RestHtmlFormattor) Init ¶
func (r *RestHtmlFormattor) Init()
func (*RestHtmlFormattor) Match ¶
func (f *RestHtmlFormattor) Match(cx *Context) bool
Gets the "application/html" content type
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 RestModelTemplate ¶
type RestNewer ¶
type RestNewer interface {
New(c *Context)
}
Handler method to show the New page for item
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
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
Source Files ¶
- compression.go
- constants.go
- context.go
- controller.go
- cookies.go
- decoders.go
- form_decode.go
- formatters.go
- http_handler.go
- mobile.go
- parameter_key_map.go
- parameter_value_map.go
- path_parsing.go
- requests.go
- rest_context.go
- rest_controller.go
- resthtmlformater.go
- route.go
- route_manager.go
- route_matching.go
- security.go
- session.go
- shortcut_funcs.go
- standard_response.go