Documentation
¶
Overview ¶
Package webapp provides utilities for building a web app.
Index ¶
- Constants
- Variables
- func AddFuncs(t *template.Template, router *mux.Router)
- func Attachment(h http.Header, base, ext string)
- func ContentLength(h http.Header, size int64)
- func Cycle(i int, arg0 string, args ...string) string
- func IsNotFound(e error) bool
- func JSONResponse(w http.ResponseWriter, v interface{}) error
- func MethodNotAllowed(w http.ResponseWriter, methods ...string)
- func RoutePath(router *mux.Router) func(string, ...interface{}) (template.URL, error)
- func RouteURL(router *mux.Router) func(string, ...interface{}) (template.URL, error)
- func RunInTransaction(db *sql.DB, f func(*sql.Tx) error) error
- func ScanOneStruct(rows *sql.Rows, val interface{}) error
- func ScanStruct(rows *sql.Rows, val interface{}) error
- type AcceptHeader
- type Logger
- type MediaRange
- type MultiError
- type ResponseBuffer
- func (br *ResponseBuffer) Copy(w http.ResponseWriter) error
- func (br *ResponseBuffer) Header() http.Header
- func (br *ResponseBuffer) HeaderSent() http.Header
- func (br *ResponseBuffer) Size() int64
- func (br *ResponseBuffer) StatusCode() int
- func (br *ResponseBuffer) Write(p []byte) (int, error)
- func (br *ResponseBuffer) WriteHeader(code int)
- type ResponseStats
- type TransactionError
- type URLError
Examples ¶
Constants ¶
const ( HeaderAccept = "Accept" HeaderAllow = "Allow" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderLocation = "Location" )
Common HTTP headers
const ( HTMLType = "text/html; charset=utf-8" JSONType = "application/json; charset=utf-8" )
Content types
Variables ¶
var NotFound = errors.New("not found")
NotFound is an HTTP 404 error.
Functions ¶
func Attachment ¶
Attachment sets the Content-Disposition header to an attachment with the given file name.
func ContentLength ¶
ContentLength sets the Content-Length header to size.
func Cycle ¶
Cycle cycles among the given strings given an index.
Example ¶
for i := 0; i < 3; i++ { fmt.Println(Cycle(i, "even", "odd")) }
Output: even odd even
func IsNotFound ¶
IsNotFound reports whether an error is a NotFound error.
func JSONResponse ¶
func JSONResponse(w http.ResponseWriter, v interface{}) error
JSONResponse writes a JSON value to w, setting the Content-Type.
func MethodNotAllowed ¶
func MethodNotAllowed(w http.ResponseWriter, methods ...string)
MethodNotAllowed replies to a request with an HTTP 405 method not allowed error.
func RoutePath ¶
RoutePath returns a function suitable for a template that returns the path for a route and its parameters.
func RouteURL ¶
RouteURL returns a function suitable for a template that returns the full URL for a route and its parameters.
func RunInTransaction ¶
RunInTransaction executes a SQL operation in a transaction. Any non-nil error will be wrapped in a TransactionError.
func ScanOneStruct ¶
ScanOneStruct is equivalent to calling rows.Next, ScanStruct, then rows.Close. If there is no next row, sql.ErrNoRows is returned.
func ScanStruct ¶
ScanStruct extracts a single row into the struct pointed to by val.
Each exported struct field is converted to a column name by using the "sql" tag of that field, or by transforming the field name from upper camel case to lowercase underscore-separated words if there is no tag. If the field tag is "-", then the field will be ignored.
It is not an error to have extra columns or fields.
Example ¶
type Person struct { ID int `sql:"ID"` FirstName string // implicitly "first_name" LastName string // implicitly "last_name" Ignored bool `sql:"-"` } var person Person db, err := sql.Open("sqlite3", ":memory:") if err != nil { log.Fatal(err) } _, err = db.Exec("CREATE TABLE person ( ID integer, first_name text, last_name text );") if err != nil { log.Fatal(err) } _, err = db.Exec("INSERT INTO person ( ID, first_name, last_name ) VALUES (1, 'John', 'Doe');") if err != nil { log.Fatal(err) } rows, err := db.Query("SELECT * FROM person;") defer rows.Close() if err != nil { log.Fatal(err) } if err := ScanStruct(rows, &person); err != nil { log.Fatal(err) }
Output:
Types ¶
type AcceptHeader ¶
type AcceptHeader []MediaRange
An AcceptHeader represents a set of media ranges as sent in the Accept header of an HTTP request.
http://tools.ietf.org/html/rfc2616#section-14.1
func ParseAcceptHeader ¶
func ParseAcceptHeader(accept string) (AcceptHeader, error)
ParseAcceptHeader parses an Accept header of an HTTP request. The media ranges are unsorted.
func (AcceptHeader) Quality ¶
func (h AcceptHeader) Quality(contentType string, params map[string][]string) float64
Quality returns the quality of a content type based on the media ranges in h.
func (AcceptHeader) String ¶
func (h AcceptHeader) String() string
type MediaRange ¶
A MediaRange represents a set of MIME types as sent in the Accept header of an HTTP request.
func (*MediaRange) Match ¶
func (mr *MediaRange) Match(contentType string, params map[string][]string) bool
Match reports whether the range applies to a content type.
func (*MediaRange) String ¶
func (mr *MediaRange) String() string
type MultiError ¶
type MultiError []error
A MultiError is returned by operations that have errors on particular elements. This is functionally identical to appengine.MultiError.
func (MultiError) Error ¶
func (e MultiError) Error() string
type ResponseBuffer ¶
A ResponseBuffer is a ResponseWriter that stores the data written to it in memory. The zero value is an empty response.
func (*ResponseBuffer) Copy ¶
func (br *ResponseBuffer) Copy(w http.ResponseWriter) error
Copy sends br's data to another ResponseWriter.
func (*ResponseBuffer) Header ¶
func (br *ResponseBuffer) Header() http.Header
func (*ResponseBuffer) HeaderSent ¶
func (br *ResponseBuffer) HeaderSent() http.Header
HeaderSent returns the headers that were sent when WriteHeader was called or nil if WriteHeader has not been called.
func (*ResponseBuffer) Size ¶
func (br *ResponseBuffer) Size() int64
Size returns the number of bytes in the buffer.
func (*ResponseBuffer) StatusCode ¶
func (br *ResponseBuffer) StatusCode() int
StatusCode returns the status code sent with WriteHeader or 0 if WriteHeader has not been called.
func (*ResponseBuffer) WriteHeader ¶
func (br *ResponseBuffer) WriteHeader(code int)
type ResponseStats ¶
type ResponseStats struct {
// contains filtered or unexported fields
}
ResponseStats is a ResponseWriter that records statistics about a response.
func NewResponseStats ¶
func NewResponseStats(w http.ResponseWriter) *ResponseStats
NewResponseStats returns a new ResponseStats that writes to w.
func (*ResponseStats) Header ¶
func (r *ResponseStats) Header() http.Header
func (*ResponseStats) Size ¶
func (r *ResponseStats) Size() int64
Size returns the number of bytes written to the underlying ResponseWriter.
func (*ResponseStats) StatusCode ¶
func (r *ResponseStats) StatusCode() int
StatusCode returns the status code sent with WriteHeader or 0 if WriteHeader has not been called.
func (*ResponseStats) WriteHeader ¶
func (r *ResponseStats) WriteHeader(statusCode int)
type TransactionError ¶
type TransactionError struct { Err error // Error surrounding transaction TxErr error // Error during transaction }
TransactionError is returned by RunInTransaction.
func (*TransactionError) Error ¶
func (e *TransactionError) Error() string