Documentation ¶
Overview ¶
Package rest implements a simple REST microframework designed for Google App Engine
Index ¶
- Constants
- Variables
- func Body(ctx context.Context) []byte
- func BodyString(ctx context.Context) string
- func Bytes(ctx context.Context, code int, contentType string, b []byte) error
- func CSS(ctx context.Context, code int, css interface{}) error
- func Decode(ctx context.Context, dst interface{}) error
- func Error(ctx context.Context, code int, err error) error
- func Font(ctx context.Context, b []byte) error
- func FormFile(ctx context.Context, key string) (multipart.File, *multipart.FileHeader, error)
- func FormValue(ctx context.Context, key string) string
- func GetCode(ctx context.Context) int
- func HTML(ctx context.Context, code int, html interface{}) error
- func Handler(fn ...AppHandler) http.Handler
- func Header(ctx context.Context) http.Header
- func Headers(ctx context.Context) http.Header
- func Init(w http.ResponseWriter, r *http.Request) context.Context
- func JPEG(ctx context.Context, code int, img image.Image) error
- func JSON(ctx context.Context, code int, data interface{}) error
- func New() *mux.Router
- func NoContent(ctx context.Context) error
- func PNG(ctx context.Context, code int, img image.Image) error
- func Redirect(ctx context.Context, urlStr string, code int) error
- func Request(ctx context.Context) *http.Request
- func ResponseWriter(ctx context.Context) http.ResponseWriter
- func Status(ctx context.Context, code int) error
- func TestClient(ctx context.Context, resp *http.Response, err error) *http.Client
- func Text(ctx context.Context, code int, str interface{}) error
- func Unauthorized(ctx context.Context) error
- func User(ctx context.Context, user interface{}) error
- func XML(ctx context.Context, code int, data interface{}) error
- type AppHandler
- type LogFunc
- type MIME
- type ResponseBytes
- type ResponseCode
- type ResponseContentType
- type ResponseReader
- type ResponseString
- type RoundTripper
- type StatusCode
- type TestRequest
- func NewTestRequest(req *http.Request) *TestRequest
- func TestPost(urlStr string, contentType string, body io.Reader) (req *TestRequest, err error)
- func TestPostForm(urlStr string, form url.Values) (req *TestRequest, err error)
- func TestPostJSON(urlStr string, data interface{}) (req *TestRequest, err error)
- func TestPostXML(urlStr string, data interface{}) (req *TestRequest, err error)
Constants ¶
const ( CONNECT = "CONNECT" DELETE = "DELETE" GET = "GET" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" POST = "POST" PUT = "PUT" TRACE = "TRACE" )
HTTP methods
const ( HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAllow = "Allow" HeaderAuthorization = "Authorization" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderCookie = "Cookie" HeaderSetCookie = "Set-Cookie" HeaderIfModifiedSince = "If-Modified-Since" HeaderLastModified = "Last-Modified" HeaderLocation = "Location" HeaderUpgrade = "Upgrade" HeaderVary = "Vary" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderXForwardedFor = "X-Forwarded-For" HeaderXForwardedProto = "X-Forwarded-Proto" HeaderXForwardedProtocol = "X-Forwarded-Protocol" HeaderXForwardedSsl = "X-Forwarded-Ssl" HeaderXUrlScheme = "X-Url-Scheme" HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" HeaderXRealIP = "X-Real-IP" HeaderXRequestID = "X-Request-ID" HeaderServer = "Server" HeaderOrigin = "Origin" // Access control HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" HeaderAccessControlMaxAge = "Access-Control-Max-Age" // Security HeaderStrictTransportSecurity = "Strict-Transport-Security" HeaderXContentTypeOptions = "X-Content-Type-Options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXFrameOptions = "X-Frame-Options" HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderXCSRFToken = "X-CSRF-Token" )
Headers
Variables ¶
var ( ErrBodyIsEmpty = errors.New("body is empty") ErrUnknownContentType = errors.New("could not determinte content type") )
var ( ContextKeyNamepace context.Key = "namespace" ContextKeyRequestBody context.Key = "request.body" ContextKeyRequestVars context.Key = "request.vars" ContextKeyResponseWriter context.Key = "http.responsewriter" ContextKeyRequest context.Key = "request" ContextKeyResponseCode context.Key = "response.code" ContextKeyInitialized context.Key = "initialized" ContextKeyResponseBody context.Key = "response.body" ContextKeyEnvironment context.Key = "environment" )
Context key standardized definitions
var ( // UserFunc is a used defined function which defines which user is associated with the current request UserFunc func(ctx context.Context, user interface{}) error ErrNoUserFunc = errors.New("no user func defined") )
var GetErrorCode func(ctx context.Context, err error) int = func(ctx context.Context, err error) (code int) { exist := GetCode(ctx) if exist != http.StatusOK { return exist } switch err.(type) { case StatusCode: return err.(StatusCode).Code() } switch { case err == datastore.ErrNoSuchEntity: code = http.StatusNotFound case appengine.IsOverQuota(err): code = http.StatusTooManyRequests case appengine.IsTimeoutError(err): code = http.StatusGatewayTimeout default: code = http.StatusInternalServerError } return }
GetErrorCode allows customizing of the http.StatusCode for any given error. If the code has already been set with SetCode() then it will not be overwritten by this function.
var Namespace func(ctx context.Context) (string, error) = func(ctx context.Context) (string, error) { return "", nil }
Namespace enables setting custom namespace
var OnError func(ctx context.Context, code int, err error) = func(ctx context.Context, code int, err error) { Errorf(ctx, "error: %v", err) http.Error(ResponseWriter(ctx), err.Error(), code) }
OnError is a custom error handler definition. By default it simple returns an http.Error() with the given code and status text.
OnUnauthorized handles the response for 401 responses
Functions ¶
func BodyString ¶
BodyString returns the body of the request as a string
func Handler ¶
func Handler(fn ...AppHandler) http.Handler
Handler is a chainable set of AppHandler middleware funcs
func ResponseWriter ¶
func ResponseWriter(ctx context.Context) http.ResponseWriter
ResponseWriter returns the response writer for the given context
func TestClient ¶
TestClient creates a http.Client which will return the given response
func Unauthorized ¶
Unauthorized returns a 401 error with 401 status text
Types ¶
type AppHandler ¶
AppHandler is the wrapper for all HTTP requests. It provides a valid context, authorization information, and route parameters. The returned interface is written to the response, unless an error is returned.
var OnPanic AppHandler
OnPanic when defined, will handle any panics from resulting funcs
type LogFunc ¶
LogFunc is a custom log function type
var Criticalf LogFunc = func(ctx context.Context, format string, args ...interface{}) { log.Errorf(format, args...) }
Criticalf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed
var Debugf LogFunc = func(ctx context.Context, format string, args ...interface{}) { log.Debugf(format, args...) }
Debugf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed
var Errorf LogFunc = func(ctx context.Context, format string, args ...interface{}) { log.Errorf(format, args...) }
Errorf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed
var Fatalf LogFunc = func(ctx context.Context, format string, args ...interface{}) { log.Fatalf(format, args...) }
Fatalf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed
type MIME ¶
type MIME string
MIME is a string which implements the ContentType interface
const ( MIMEApplicationJSON MIME = "application/json" MIMEApplicationJSONCharsetUTF8 MIME = MIMEApplicationJSON + "; " + charsetUTF8 MIMEApplicationJavaScript MIME = "application/javascript" MIMEApplicationJavaScriptCharsetUTF8 MIME = MIMEApplicationJavaScript + "; " + charsetUTF8 MIMEApplicationXML MIME = "application/xml" MIMEApplicationXMLCharsetUTF8 MIME = MIMEApplicationXML + "; " + charsetUTF8 MIMETextXML MIME = "text/xml" MIMETextXMLCharsetUTF8 MIME = MIMETextXML + "; " + charsetUTF8 MIMEApplicationForm MIME = "application/x-www-form-urlencoded" MIMEApplicationProtobuf MIME = "application/protobuf" MIMEApplicationMsgpack MIME = "application/msgpack" MIMETextHTML MIME = "text/html" MIMETextHTMLCharsetUTF8 MIME = MIMETextHTML + "; " + charsetUTF8 MIMETextPlain MIME = "text/plain" MIMETextPlainCharsetUTF8 MIME = MIMETextPlain + "; " + charsetUTF8 MIMEMultipartForm MIME = "multipart/form-data" MIMEOctetStream MIME = "application/octet-stream" )
MIME type definitions
func (MIME) ContentType ¶
ContentType returns the content-type header for the mime type
type ResponseBytes ¶
type ResponseBytes interface {
Body() []byte
}
ResponseBytes defines an interface which returns a Body() of a byte slice
type ResponseCode ¶
type ResponseCode interface {
Code() int
}
ResponseCode defines an interface which returns an HTTP response code
type ResponseContentType ¶
type ResponseContentType interface {
ContentType() string
}
ResponseContentType defines an interface which returns a content-type
type ResponseReader ¶
ResponseReader defines an interface with returns a response body of a io.Readoer
type ResponseString ¶
type ResponseString interface {
Body() string
}
ResponseString defines an interface with returns a response body of a string
type RoundTripper ¶
RoundTripper provides a easy way to implement the http.RoundTripper interface for simulating HTTP responses without having to make an HTTP request
type StatusCode ¶
type StatusCode int
StatusCode is an HTTP status code, with associated error messages
const ( StatusContinue StatusCode = 100 // RFC 7231, 6.2.1 StatusSwitchingProtocols StatusCode = 101 // RFC 7231, 6.2.2 StatusProcessing StatusCode = 102 // RFC 2518, 10.1 StatusOK StatusCode = 200 // RFC 7231, 6.3.1 StatusCreated StatusCode = 201 // RFC 7231, 6.3.2 StatusAccepted StatusCode = 202 // RFC 7231, 6.3.3 StatusNonAuthoritativeInfo StatusCode = 203 // RFC 7231, 6.3.4 StatusNoContent StatusCode = 204 // RFC 7231, 6.3.5 StatusResetContent StatusCode = 205 // RFC 7231, 6.3.6 StatusPartialContent StatusCode = 206 // RFC 7233, 4.1 StatusMultiStatus StatusCode = 207 // RFC 4918, 11.1 StatusAlreadyReported StatusCode = 208 // RFC 5842, 7.1 StatusIMUsed StatusCode = 226 // RFC 3229, 10.4.1 StatusMultipleChoices StatusCode = 300 // RFC 7231, 6.4.1 StatusMovedPermanently StatusCode = 301 // RFC 7231, 6.4.2 StatusFound StatusCode = 302 // RFC 7231, 6.4.3 StatusSeeOther StatusCode = 303 // RFC 7231, 6.4.4 StatusNotModified StatusCode = 304 // RFC 7232, 4.1 StatusUseProxy StatusCode = 305 // RFC 7231, 6.4.5 StatusTemporaryRedirect StatusCode = 307 // RFC 7231, 6.4.7 StatusPermanentRedirect StatusCode = 308 // RFC 7538, 3 StatusBadRequest StatusCode = 400 // RFC 7231, 6.5.1 StatusPaymentRequired StatusCode = 402 // RFC 7231, 6.5.2 StatusForbidden StatusCode = 403 // RFC 7231, 6.5.3 StatusNotFound StatusCode = 404 // RFC 7231, 6.5.4 StatusMethodNotAllowed StatusCode = 405 // RFC 7231, 6.5.5 StatusNotAcceptable StatusCode = 406 // RFC 7231, 6.5.6 StatusProxyAuthRequired StatusCode = 407 // RFC 7235, 3.2 StatusRequestTimeout StatusCode = 408 // RFC 7231, 6.5.7 StatusConflict StatusCode = 409 // RFC 7231, 6.5.8 StatusGone StatusCode = 410 // RFC 7231, 6.5.9 StatusLengthRequired StatusCode = 411 // RFC 7231, 6.5.10 StatusPreconditionFailed StatusCode = 412 // RFC 7232, 4.2 StatusRequestEntityTooLarge StatusCode = 413 // RFC 7231, 6.5.11 StatusRequestURITooLong StatusCode = 414 // RFC 7231, 6.5.12 StatusUnsupportedMediaType StatusCode = 415 // RFC 7231, 6.5.13 StatusRequestedRangeNotSatisfiable StatusCode = 416 // RFC 7233, 4.4 StatusExpectationFailed StatusCode = 417 // RFC 7231, 6.5.14 StatusTeapot StatusCode = 418 // RFC 7168, 2.3.3 StatusUnprocessableEntity StatusCode = 422 // RFC 4918, 11.2 StatusLocked StatusCode = 423 // RFC 4918, 11.3 StatusFailedDependency StatusCode = 424 // RFC 4918, 11.4 StatusUpgradeRequired StatusCode = 426 // RFC 7231, 6.5.15 StatusPreconditionRequired StatusCode = 428 // RFC 6585, 3 StatusTooManyRequests StatusCode = 429 // RFC 6585, 4 StatusRequestHeaderFieldsTooLarge StatusCode = 431 // RFC 6585, 5 StatusInternalServerError StatusCode = 500 // RFC 7231, 6.6.1 StatusNotImplemented StatusCode = 501 // RFC 7231, 6.6.2 StatusBadGateway StatusCode = 502 // RFC 7231, 6.6.3 StatusGatewayTimeout StatusCode = 504 // RFC 7231, 6.6.5 StatusHTTPVersionNotSupported StatusCode = 505 // RFC 7231, 6.6.6 StatusVariantAlsoNegotiates StatusCode = 506 // RFC 2295, 8.1 StatusInsufficientStorage StatusCode = 507 // RFC 4918, 11.5 StatusLoopDetected StatusCode = 508 // RFC 5842, 7.2 StatusNotExtended StatusCode = 510 // RFC 2774, 7 StatusNetworkAuthenticationRequired StatusCode = 511 // RFC 6585, 6 )
HTTP status codes. They implement the Error() and ResponseCode interfaces, so then can be used for one-line error responses which have the default status text set.
func (StatusCode) Body ¶
func (s StatusCode) Body() string
Body returns the http.Response body string
func (StatusCode) ContentType ¶
func (s StatusCode) ContentType() string
ContentType returns the http.Response content-type header
func (StatusCode) Error ¶
func (s StatusCode) Error() string
func (StatusCode) String ¶
func (s StatusCode) String() string
type TestRequest ¶
type TestRequest struct { Writer *httptest.ResponseRecorder Request *http.Request Context context.Context }
func NewTestRequest ¶
func NewTestRequest(req *http.Request) *TestRequest
func TestPostForm ¶
func TestPostForm(urlStr string, form url.Values) (req *TestRequest, err error)
TestPostForm bootstraps a POST request to the given urlStr with the form as the request body
func TestPostJSON ¶
func TestPostJSON(urlStr string, data interface{}) (req *TestRequest, err error)
TestPostJSON bootstraps a POST request to the given urlStr with encoded JSON data as the request body
func TestPostXML ¶
func TestPostXML(urlStr string, data interface{}) (req *TestRequest, err error)
TestPostXML bootstraps a POST request to the given urlStr with encoded XML data as the request body
func (*TestRequest) Decode ¶
func (r *TestRequest) Decode(data interface{}) error
func (*TestRequest) Do ¶
func (r *TestRequest) Do(h AppHandler) error
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package cache provides a unified interface to in memory cache.
|
Package cache provides a unified interface to in memory cache. |
Package cookies provides a convenience wrapper around gorilla/securecookie with shared hash keys
|
Package cookies provides a convenience wrapper around gorilla/securecookie with shared hash keys |