Back to godoc.org
bitbucket.org/zombiezen/webapp

Package webapp

v0.0.0-...-e8e5051
Latest Go to latest

The latest major version is .

Published: Jun 13, 2014 | License: BSD-2-Clause | Module: bitbucket.org/zombiezen/webapp

Overview

Package webapp provides utilities for building a web app.

Index

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.

func AddFuncs

func AddFuncs(t *template.Template, router *mux.Router)

AddFuncs adds the package's template functions.

func Attachment

func Attachment(h http.Header, base, ext string)

Attachment sets the Content-Disposition header to an attachment with the given file name.

func ContentLength

func ContentLength(h http.Header, size int64)

ContentLength sets the Content-Length header to size.

func Cycle

func Cycle(i int, arg0 string, args ...string) string

Cycle cycles among the given strings given an index.

Example

Code:

for i := 0; i < 3; i++ {
	fmt.Println(Cycle(i, "even", "odd"))
}
even
odd
even

func IsNotFound

func IsNotFound(e error) bool

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

func RoutePath(router *mux.Router) func(string, ...interface{}) (template.URL, error)

RoutePath returns a function suitable for a template that returns the path for a route and its parameters.

func RouteURL

func RouteURL(router *mux.Router) func(string, ...interface{}) (template.URL, error)

RouteURL returns a function suitable for a template that returns the full URL for a route and its parameters.

func RunInTransaction

func RunInTransaction(db *sql.DB, f func(*sql.Tx) error) error

RunInTransaction executes a SQL operation in a transaction. Any non-nil error will be wrapped in a TransactionError.

func ScanOneStruct

func ScanOneStruct(rows *sql.Rows, val interface{}) error

ScanOneStruct is equivalent to calling rows.Next, ScanStruct, then rows.Close. If there is no next row, sql.ErrNoRows is returned.

func ScanStruct

func ScanStruct(rows *sql.Rows, val interface{}) error

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

Code:

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)
}

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 Logger

type Logger struct {
	http.Handler
}

Logger logs all HTTP requests sent to an http.Handler.

func (Logger) ServeHTTP

func (l Logger) ServeHTTP(w http.ResponseWriter, req *http.Request)

type MediaRange

type MediaRange struct {
	Range   string
	Quality float64
	Params  map[string][]string
}

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

type ResponseBuffer struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

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) Write

func (br *ResponseBuffer) Write(p []byte) (int, error)

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) Write

func (r *ResponseStats) Write(p []byte) (n int, err error)

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

type URLError

type URLError struct {
	URL *url.URL
	Err error
}

URLError records an error and the URL that caused it.

func (*URLError) Error

func (e *URLError) Error() string

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier