goobar

package
v0.0.0-...-fbe25eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2020 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetViewFolder

func SetViewFolder(folder string)

SetViewFolder sets the file path of the local directory that contains the view templates.

func ViewFolder

func ViewFolder() string

ViewFolder returns the file path of the local directory that contains the view templates.

Types

type Action

type Action func(x *Exchange) Responder

Action is a function that handles an HTTP request.

type Exchange

type Exchange struct {
	// contains filtered or unexported fields
}

Exchange contains information about a HTTP exchange (request and response), providing comfort functions to get typed form or query values.

func (*Exchange) GetBool

func (x *Exchange) GetBool(key string) (bool, bool)

GetBool returns the boolean value passed by query or form with the given key. If the given key is not present or in the wrong format, false is returned for the second return value.

func (*Exchange) GetBoolOrDefault

func (x *Exchange) GetBoolOrDefault(key string, defaultVal bool) bool

GetBoolOrDefault returns the boolean value passed by query or form with the given key or the specified default value if no such value is present.

func (*Exchange) GetFloat

func (x *Exchange) GetFloat(key string) (float64, bool)

GetFloat returns the floating-point value passed by query or form with the given key. If the given key is not present or in the wrong format, false is returned for the second return value.

func (*Exchange) GetFloatOrDefault

func (x *Exchange) GetFloatOrDefault(key string, defaultVal float64) float64

GetFloatOrDefault returns the floating-point value passed by query or form with the given key or the specified default value if no such value is present.

func (*Exchange) GetID

func (x *Exchange) GetID() (int, bool)

GetID returns the numeric ID passed with the request, which is the last element of the requested URL path (e.g. for http://host:port/item/action/1, GetID would return 1). If no numeric ID is present, false is returned for the second return value.

func (*Exchange) GetIDString

func (x *Exchange) GetIDString() string

GetIDString returns the ID passed with the request (see GetID) or an empty string if no ID is present.

func (*Exchange) GetInt

func (x *Exchange) GetInt(key string) (int, bool)

GetInt returns the integer value passed by query or form with the given key. If the given key is not present or in the wrong format, false is returned for the second return value.

func (*Exchange) GetIntOrDefault

func (x *Exchange) GetIntOrDefault(key string, defaultVal int) int

GetIntOrDefault returns the integer value passed by query or form with the given key or the specified default value if no such value is present.

func (*Exchange) GetString

func (x *Exchange) GetString(key string) string

GetString returns the string value passed by query or form with the given key or the empty string if no value with the given key is present.

func (*Exchange) GetStringOrDefault

func (x *Exchange) GetStringOrDefault(key string, defaultVal string) string

GetStringOrDefault returns the string value passed by query or form with the given key or the specified default if no value with the given key is present.

func (*Exchange) MustGetBool

func (x *Exchange) MustGetBool(key string) bool

MustGetBool returns the boolean value passed by query or form with the given key or panics if not present or the found value is in the wrong format.

func (*Exchange) MustGetFloat

func (x *Exchange) MustGetFloat(key string) float64

MustGetFloat returns the floating-point value passed by query or form with the given key or panics if not present or the found value is in the wrong format.

func (*Exchange) MustGetID

func (x *Exchange) MustGetID() int

MustGetID returns the numeric ID passed with the request (see GetID) or panics if no numeric ID is present. The goobar HTTP handler will recover from this panic and write a 404 error to the client.

func (*Exchange) MustGetIDString

func (x *Exchange) MustGetIDString() string

MustGetIDString returns the ID passed with the request (see GetID) or panics if no ID is present. The goobar HTTP handler will recover from this panic and write a 404 error to the client.

func (*Exchange) MustGetInt

func (x *Exchange) MustGetInt(key string) int

MustGetInt returns the integer value passed by query or form with the given key or panics if not present or the found value is in the wrong format.

func (*Exchange) MustGetString

func (x *Exchange) MustGetString(key string) string

MustGetString returns the string value passed by query or form with the given key or panics if no value with the given key is present.

func (*Exchange) Request

func (x *Exchange) Request() *http.Request

Request returns a pointer to the http.Request contained in the Exchange.

func (*Exchange) Writer

func (x *Exchange) Writer() http.ResponseWriter

Writer returns the http.ResponseWriter contained in the Exchange.

type Handler

type Handler struct {
	Get, Post, Put, Delete, Options Action
}

Handler stores a record of functions that handle different HTTP methods invoked for a common path. It stores Actions for GET, POST, PUT, DELETE and OPTIONS.

func AnyMethod

func AnyMethod(action Action) *Handler

AnyMethod returns a Handler with only one action for all supported HTTP actions (GET, POST, PUT, DELETE, OPTIONS)

func Delete

func Delete(action Action) *Handler

Delete returns a Handler with only one action for the HTTP method DELETE.

func Get

func Get(action Action) *Handler

Get returns a Handler with only one action for the HTTP method GET.

func Post

func Post(action Action) *Handler

Post returns a Handler with only one action for the HTTP method Post.

func Put

func Put(action Action) *Handler

Put returns a Handler with only one action for the HTTP method PUT.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Responder

type Responder interface {
	// Respond writes an HTTP response to the specified io.Writer.
	Respond(writer io.Writer) error
	// ContentType returns the MIME type that describes the format
	// of the data returned by Respond.
	ContentType() string
}

Responder is the type returned by all goobar exchange handlers.

func Binary

func Binary(reader io.Reader) Responder

Binary returns a Responder that writes binary data to the client.

func Error

func Error(statusCode int, message string) Responder

Error returns a Responder that writes an HTTP error to the client.

func ImagePNG

func ImagePNG(img image.Image) Responder

ImagePNG returns a Responder that writes a PNG encoded image to the client.

func JSON

func JSON(v interface{}) Responder

JSON returns a Responder that sends an object in JSON format to the client.

func Nop

func Nop() Responder

Nop returns a responder that does not write any data back to the client. The handler that returns Nop() is responsible for writing to the client using Exchange.Writer().

func PlainText

func PlainText(text string) Responder

PlainText returns a Responder that writes a plain UTF-8 encoded text response to the client.

func View

func View(path string, model interface{}) Responder

View returns a Responder that renders a view from a Go template. The template is identified by the specified file path and the given model is pipelined to the template renderer.

func XML

func XML(v interface{}) Responder

XML returns a Responder that sends an object in XML format to the client. See the xml package for more info on the encoding.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL