Documentation
¶
Overview ¶
The package httpio provides utility types and interfaces to complement the httpcrud package.
The main two types, RequestReader and ResponseWriter, both implement parts of the httpcrud.Handler interface and can therefore be utilized as composition blocks in a complete implementation of the interface.
Both, the RequestReader and ResponseWriter, by exporting a number of interface type fields, allow the user to split the implementation into smaller, more specific subtasks to read and write the requests and responses, respectively.
Index ¶
- func RegisterHTMLTemplatesOnce(tmap map[string]*template.Template)
- type BearerToken
- type BodyReader
- type BodyWriter
- type Bool
- type CSV
- type CSVWriter
- type CookieValues
- type Float32
- type Float64
- type Form
- type HTML
- type HeaderReader
- type HeaderReaderList
- type HeaderWriter
- type IPAddress
- type IPUA
- type Int
- type Int16
- type Int32
- type Int64
- type Int8
- type JSON
- type NoTemplateError
- type PathReader
- type PathReaderList
- type QueryReader
- type QueryReaderList
- type ReadError
- type Redirect
- type RequestDump
- type RequestReader
- type ResponseWriter
- type SetCookie
- type StreamWriter
- type String
- type Text
- type Uint
- type Uint16
- type Uint32
- type Uint64
- type Uint8
- type UserAgent
- type WriteError
- type XML
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterHTMLTemplatesOnce ¶
RegisterHTMLTemplatesOnce registers the given templates to be used by the HTML BodyWriter implementation. The RegisterHTMLTemplatesOnce function is intended be called once at program start up.
Types ¶
type BearerToken ¶
type BearerToken struct { // Pointer to the string which should be set to the bearer token. Val *string }
BearerToken can be used to read the bearer token value from the Authorization header of the incoming HTTP request and set it to the string pointed to by Val.
func (BearerToken) ReadHeader ¶
func (rr BearerToken) ReadHeader(header http.Header) error
ReadHeader implements the HeaderReader interface.
type BodyReader ¶
BodyReader interface can be implemented to read the body of an incoming request.
type BodyWriter ¶
type BodyWriter interface { WriteInit(w http.ResponseWriter) error WriteBody(w http.ResponseWriter, r *http.Request, statusCode int) error }
BodyWriter interface can be implemented to write the body of an outgoing response.
type Bool ¶
Bool is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the bools pointed to by the map's values.
func (Bool) ReadHeader ¶
Bool implements the HeaderReader interface.
type CSV ¶
type CSV struct {
Stream StreamWriter
}
The CSV type implements the BodyWriter interface by using a StreamWriter.
type CSVWriter ¶
type CSVWriter struct { // The list of field names. // SHOULD be set prior to the first call to WriteRow. Header []string // The file name to be used for the Content-Disposition header. // SHOULD be set prior to the first call to WriteRow. FileName string // The HTTP status code to be sent with the response. // SHOULD be set prior to the first call to WriteRow. // // Set by Open to 200 (Status OK) as default. If the embedding code needs // to send a different value it should implement its own Open method, invoke // the embedded Open method to set up the writer and *then* set the StatusCode // field to the desired value. StatusCode int // contains filtered or unexported fields }
CSVWriter implements the StreamWriter interface and is intended to be embedded by user-defined structs that want to implement custom csv writers.
func (*CSVWriter) Flush ¶
Flush flushes the underlying csv.Writer and returns and error if it fails. Flush will be invoked by (CSV).WriteBody indirectly through the StreamWriter interface, if, however, the embedding code overrides this method by providing its own impelmentation then that implementation MUST invoke this method directly.
func (*CSVWriter) Open ¶
func (w *CSVWriter) Open(rw http.ResponseWriter)
Open prepares the CSVWriter instance using the given http.ResponseWriter as the target into which the csv data will be written. Open will be invoked by (CSV).WriteInit indirectly through the StreamWriter interface, if, however, the embedding code overrides this method by providing its own impelmentation then that implementation MUST invoke this method directly.
type CookieValues ¶
CookieValues can be used to read the values from the Cookie header of the incoming HTTP request and indirectly set them to the strings pointed to by the map's values, using the map's keys to match against the Cookies' names.
func (CookieValues) ReadHeader ¶
func (rr CookieValues) ReadHeader(header http.Header) error
ReadHeader implements the HeaderReader interface.
type Float32 ¶
Float32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the float32s pointed to by the map's values.
func (Float32) ReadHeader ¶
Float32 implements the HeaderReader interface.
type Float64 ¶
Float64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the float64s pointed to by the map's values.
func (Float64) ReadHeader ¶
Float64 implements the HeaderReader interface.
type Form ¶
type Form struct { // The value to be url encoded and sent in an HTTP response body or // a pointer to the value to be url decoded from an HTTP request's body. Val interface{} }
The Form type implements both the BodyWriter and the BodyReader interfaces.
func (Form) ReadBody ¶
ReadBody implements the BodyReader interface by decoding the request's form body into the reciever's Val field.
type HTML ¶
type HTML struct { // The name (map key) of the template as registered with RegisterTemplates. Name string // Data to be passed to the template's Execute method. Data interface{} }
The HTML type implements the BodyWriter interface.
type HeaderReader ¶
HeaderReader interface can be implemented to read an incoming request's header.
type HeaderReaderList ¶
type HeaderReaderList []HeaderReader
HeaderReaderList can be used to read the HTTP headers of different types.
func (HeaderReaderList) ReadHeader ¶
func (list HeaderReaderList) ReadHeader(header http.Header) error
ReadHeader implements the HeaderReader interface.
type HeaderWriter ¶
HeaderWriter interface can be implemented to write an outgoing response's header.
type IPAddress ¶
type IPAddress struct { // Pointer to the string which should be set to the ip address. Val *string }
IPAddress can be used to read the IP address value from the header of the incoming HTTP request and set it to the string pointed to by Val.
type IPUA ¶ added in v0.0.3
type IPUA struct {
// Pointer to the strings which should be set to the ip & user agent.
IP, UA *string
}
IPUA can be used to read the IP address & User-Agent values from the header of the incoming HTTP request and set them to the strings pointed to by IP & UA.
type Int ¶
Int is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the ints pointed to by the map's values.
func (Int) ReadHeader ¶
Int implements the HeaderReader interface.
type Int16 ¶
Int16 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int16s pointed to by the map's values.
func (Int16) ReadHeader ¶
Int16 implements the HeaderReader interface.
type Int32 ¶
Int32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int32s pointed to by the map's values.
func (Int32) ReadHeader ¶
Int32 implements the HeaderReader interface.
type Int64 ¶
Int64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int64s pointed to by the map's values.
func (Int64) ReadHeader ¶
Int64 implements the HeaderReader interface.
type Int8 ¶
Int8 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int8s pointed to by the map's values.
func (Int8) ReadHeader ¶
Int8 implements the HeaderReader interface.
type JSON ¶
type JSON struct { // The value to be json encoded and sent in an HTTP response body or // a pointer to the value to be json decoded from an HTTP request's body. Val interface{} }
The JSON type implements both the BodyWriter and the BodyReader interfaces.
func (JSON) ReadBody ¶
ReadBody implements the BodyReader interface by decoding the request's json body into the reciever's Val field.
type NoTemplateError ¶
type NoTemplateError struct { // The provided template name. Name string }
NoTemplateError is returned when no template with the given name was registered.
func (NoTemplateError) Error ¶
func (e NoTemplateError) Error() string
type PathReader ¶
PathReader interface can be implemented to read the route.Params from an incoming request's url path.
type PathReaderList ¶
type PathReaderList []PathReader
PathReaderList can be used to read the path parameters of different types.
type QueryReader ¶
QueryReader interface can be implemented to read the query parameters from an incoming request's url.
type QueryReaderList ¶
type QueryReaderList []QueryReader
QueryReaderList can be used to read the url query parameters of different types.
type ReadError ¶
type ReadError struct { // The original error. Err error }
ReadError represents an error returned by a BodyReader.
type Redirect ¶
type Redirect struct { // The URL to which to redirect. URL string // The HTTP redirect status code. StatusCode int }
The Redirect type implements the BodyWriter interface.
type RequestDump ¶
type RequestDump struct { // The pointer to which to set the request dump. Val *[]byte // Indicates whether to dump the request's body as well. Body bool }
The RequestDump type implements the BodyReader interface.
type RequestReader ¶
type RequestReader struct { // If set, will read the header from the incoming request. Header HeaderReader // If set, will read the query parameters from the incoming request's url. Query QueryReader // If set, will read the route.Params from the path of incoming request's url. Path PathReader // If set, will read the body from the incoming request. Body BodyReader // contains filtered or unexported fields }
RequestReader is intended to be embedded as part of a Handler implementation to read the various data from the incoming HTTP request.
func (*RequestReader) GetContext ¶
func (rr *RequestReader) GetContext() context.Context
GetContext is a convenience method that returns the underlying http request's context value.
func (*RequestReader) GetRequest ¶
func (rr *RequestReader) GetRequest() *http.Request
GetRequest is a convenience method that returns the underlying http request.
func (*RequestReader) ReadRequest ¶
RequestReader implements the ReadRequest method of the Handler interface.
type ResponseWriter ¶
type ResponseWriter struct { // If set, will write the header of the outgoing response. Header HeaderWriter // If set, will write the body of the outgoing response. Body BodyWriter // If set, will be used as the HTTP status code of the outgoing response. Status int }
ResponseWriter is intended to be embedded as part of a Handler implementation to write the various data of the outgoing HTTP response.
func (*ResponseWriter) InitResponse ¶
func (rw *ResponseWriter) InitResponse(w http.ResponseWriter) error
ResponseWriter implements the InitResponse method of the Handler interface.
func (*ResponseWriter) WriteResponse ¶
func (rw *ResponseWriter) WriteResponse(w http.ResponseWriter, r *http.Request) error
ResponseWriter implements the WriteResponse method of the Handler interface.
type SetCookie ¶
SetCookie can be used to set the Set-Cookie header of the outgoing response.
func (SetCookie) WriteHeader ¶
WriteHeader implements the HeaderWriter interface.
type StreamWriter ¶
type StreamWriter interface { // Open, intended to be invoked by (BodyWriter).WriteInit, should prepare // the stream for writing using the given http.ResponseWriter as the medium // for transferring the data. Open(w http.ResponseWriter) // Flush, intended to be invoked by (BodyWriter).WriteBody, should write // any buffered data to the underlying http.ResponseWriter and return an // error if the flush fails. Flush() error }
StreamWriter interface can be used by BodyWriters
type String ¶
String is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the strings pointed to by the map's values.
func (String) ReadHeader ¶
String implements the HeaderReader interface.
type Text ¶
type Text struct { // The value to be sent in an HTTP response body. Val string }
The Text type implements the BodyWriter interface.
type Uint ¶
Uint is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uints pointed to by the map's values.
func (Uint) ReadHeader ¶
Uint implements the HeaderReader interface.
type Uint16 ¶
Uint16 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint16s pointed to by the map's values.
func (Uint16) ReadHeader ¶
Uint16 implements the HeaderReader interface.
type Uint32 ¶
Uint32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint32s pointed to by the map's values.
func (Uint32) ReadHeader ¶
Uint32 implements the HeaderReader interface.
type Uint64 ¶
Uint64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint64s pointed to by the map's values.
func (Uint64) ReadHeader ¶
Uint64 implements the HeaderReader interface.
type Uint8 ¶
Uint8 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint8s pointed to by the map's values.
func (Uint8) ReadHeader ¶
Uint8 implements the HeaderReader interface.
type UserAgent ¶
type UserAgent struct { // Pointer to the string which should be set to the user agent. Val *string }
UserAgent can be used to read the value from the User-Agent header of the incoming HTTP request and set it to the string pointed to by Val.
type WriteError ¶
type WriteError struct { // The original error. Err error }
WriteError represents an error returned by a BodyWriter.
func (WriteError) Error ¶
func (e WriteError) Error() string
type XML ¶
type XML struct { // The value to be xml encoded and sent in an HTTP response body or // a pointer to the value to be xml decoded from an HTTP request's body. Val interface{} }
The XML type implements both the BodyWriter and the BodyReader interfaces.
func (XML) ReadBody ¶
ReadBody implements the BodyReader interface by decoding the request's xml body into the reciever's Val field.