Documentation ¶
Overview ¶
Package goanna is an MVC toolkit with routing, controllers and sessions
Index ¶
- Constants
- Variables
- func AbsoluteUrlFor(name string, args ...string) *url.URL
- func ClearRouter()
- func Handler(gf GoannaHandlerFunc) http.HandlerFunc
- func LogHttpRequest(r *http.Request, v ...string)
- func LogRequest(r *Request, v ...string)
- func RenderTemplate(t *template.Template, vars interface{}) []byte
- func UrlFor(name string, args ...string) *url.URL
- type Controller
- func (c *Controller) LogRequest(reason string)
- func (c *Controller) PermanentRedirect(urlStr string) *RedirectResponse
- func (c *Controller) Redirect(urlStr string) *RedirectResponse
- func (c *Controller) RedirectRoute(routeName string, args ...string) *RedirectResponse
- func (c *Controller) Render(templateStr string, vars interface{}) *OkResponse
- func (c *Controller) RenderTemplate(t *template.Template, vars interface{}) []byte
- func (c *Controller) RenderTemplateResponse(t *template.Template, vars interface{}) *OkResponse
- func (c *Controller) RenderView(templateStr string, vars interface{}) []byte
- func (c *Controller) Session() Session
- func (c *Controller) SetRequest(req *http.Request)
- func (c *Controller) UrlFor(routeName string, args ...string) *url.URL
- type ControllerFactoryFunc
- type ControllerHandler
- type ControllerInterface
- type CookieSigner
- type ErrorResponse
- type GoannaHandlerFunc
- type GoannaSessionHandlerFunc
- type JsonResponse
- type NopSession
- type OkResponse
- func (r *OkResponse) ClearCookie(name string)
- func (r *OkResponse) Content() []byte
- func (r *OkResponse) Cookies() []http.Cookie
- func (r *OkResponse) Headers() http.Header
- func (r *OkResponse) Send(w http.ResponseWriter)
- func (r *OkResponse) SendHeaders(w http.ResponseWriter)
- func (r *OkResponse) SetContent(content []byte)
- func (r *OkResponse) SetCookie(cookie http.Cookie)
- func (r *OkResponse) SetNoCache()
- func (r *OkResponse) SetStatusCode(code int)
- func (r *OkResponse) StatusCode() int
- type RedirectResponse
- type Request
- func (r *Request) BodyData() []byte
- func (r *Request) CookieValue(name string) string
- func (r *Request) FormValueOrDefault(key string, def string) string
- func (r *Request) IsGet() bool
- func (r *Request) IsHead() bool
- func (r *Request) IsPatch() bool
- func (r *Request) IsPost() bool
- func (r *Request) IsPut() bool
- func (r *Request) Log(v ...string)
- func (r *Request) QueryValue(key string) string
- func (r *Request) QueryValueOrDefault(key string, def string) string
- func (r *Request) UrlValue(key string) string
- type Response
- type Session
- type SessionFinder
Examples ¶
Constants ¶
const ( HEADER_CACHE_CONTROL = "Cache-Control" HEADER_CACHE_NOCACHE = "no-cache" )
const LogRequestTemplate = `` /* 236-byte string literal not displayed */
Variables ¶
var Logger *log.Logger
var Router *mux.Router
Router is a global router used for routing requests
var UrlBase url.URL
UrlBase is the base url used for creating absolute urls
Functions ¶
func AbsoluteUrlFor ¶
UrlFor returns the avsolute URL for the route name. UrlBase is used for the URL Host and Scheme
func Handler ¶
func Handler(gf GoannaHandlerFunc) http.HandlerFunc
Handler handles functions of type GoannaHandlerFunc
func LogHttpRequest ¶
LogHttpRequest logs a http request
func RenderTemplate ¶
RenderView renders a template using the provided template and vars struct and returns the rendered tamplate
Types ¶
type Controller ¶
type Controller struct { Request *Request // contains filtered or unexported fields }
Controller is an embeddable type for controllers
func NewController ¶
func NewController(sessionFinder SessionFinder) Controller
NewController instantiates a new Controller using the given request and session store
func (*Controller) LogRequest ¶
func (c *Controller) LogRequest(reason string)
LogRequest dumps the current request to stdout
func (*Controller) PermanentRedirect ¶
func (c *Controller) PermanentRedirect(urlStr string) *RedirectResponse
PermanentRedirect returns a new RedirectResponse with a permanent redirect (HTTP 301)
func (*Controller) Redirect ¶
func (c *Controller) Redirect(urlStr string) *RedirectResponse
func (*Controller) RedirectRoute ¶
func (c *Controller) RedirectRoute(routeName string, args ...string) *RedirectResponse
RedirectRoute returns a RedirectResponse to the route
func (*Controller) Render ¶
func (c *Controller) Render(templateStr string, vars interface{}) *OkResponse
Render renders a template using the provided template and vars struct and returns a response with the rendered template
func (*Controller) RenderTemplate ¶
func (c *Controller) RenderTemplate(t *template.Template, vars interface{}) []byte
func (*Controller) RenderTemplateResponse ¶
func (c *Controller) RenderTemplateResponse(t *template.Template, vars interface{}) *OkResponse
func (*Controller) RenderView ¶
func (c *Controller) RenderView(templateStr string, vars interface{}) []byte
RenderView renders a template string using the provided template and vars struct and returns the rendered tamplate
func (*Controller) Session ¶
func (c *Controller) Session() Session
Session returns the session for the current request
func (*Controller) SetRequest ¶
func (c *Controller) SetRequest(req *http.Request)
SetRequest injects a request into the controller
type ControllerFactoryFunc ¶
type ControllerFactoryFunc func() ControllerInterface
type ControllerHandler ¶
type ControllerHandler struct {
// contains filtered or unexported fields
}
ControllerHandler is a http.Handler for handling incoming requests and despatching to controllers
func NewHandler ¶
func NewHandler(factory ControllerFactoryFunc, methodName string) ControllerHandler
NewHandler creates a ControllerHandler from the factory and methodName
func (ControllerHandler) ServeHTTP ¶
func (handler ControllerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles a http request
type ControllerInterface ¶
ControllerInterface is implemented by controllers
type CookieSigner ¶
type CookieSigner struct {
// contains filtered or unexported fields
}
CookieSigner signs a cookie with a sha236 hmac to ensure that it cannot be tampered with
Example ¶
package main import ( "fmt" "github.com/99designs/goodies/goanna" "net/http" ) func main() { signer := goanna.NewCookieSigner([]byte("secret")) cookie := http.Cookie{ Name: "foo", Value: "bar", } fmt.Println(cookie.Value) // bar signer.EncodeCookie(&cookie) fmt.Println(cookie.Value) // aMcN2wzx4XLp9w3CPrwNb6PtTzECzkMPIiEfDqVDk4k=.bar signer.DecodeCookie(&cookie) fmt.Println(cookie.Value) // bar }
Output:
func NewCookieSigner ¶
func NewCookieSigner(key []byte) CookieSigner
NewCookieSigner returns a new CookieSigner using the given key
func (CookieSigner) DecodeCookie ¶
func (c CookieSigner) DecodeCookie(cookie *http.Cookie) error
DecodeCookie verifies the signature and decodes the value into the cookie
func (CookieSigner) DecodeValue ¶
func (c CookieSigner) DecodeValue(encodedvalue string) (string, error)
DecodeValue validates and decodes a cookie value
func (CookieSigner) EncodeCookie ¶
func (c CookieSigner) EncodeCookie(cookie *http.Cookie)
EncodeCookie signs and encodes the cookie
func (CookieSigner) EncodeValue ¶
func (c CookieSigner) EncodeValue(value string) string
EncodeValue signs and encodes a cookie value
type ErrorResponse ¶
type ErrorResponse struct { *OkResponse Message string }
func NewErrorResponse ¶
func NewErrorResponse(message string, code int) *ErrorResponse
func (*ErrorResponse) Send ¶
func (r *ErrorResponse) Send(w http.ResponseWriter)
type GoannaHandlerFunc ¶
GoannaHandlerFunc is a function type that can be handled by GoannaHandler
type JsonResponse ¶
type JsonResponse struct {
*OkResponse
}
func NewJsonDataResponse ¶
func NewJsonDataResponse(data interface{}) *JsonResponse
func NewJsonResponse ¶
func NewJsonResponse() *JsonResponse
func (*JsonResponse) SetData ¶
func (this *JsonResponse) SetData(data interface{})
type NopSession ¶
type NopSession struct{}
func (NopSession) Clear ¶
func (s NopSession) Clear()
func (NopSession) Get ¶
func (s NopSession) Get(_ string) string
func (NopSession) GetId ¶
func (s NopSession) GetId() string
func (NopSession) HasExpired ¶
func (s NopSession) HasExpired() bool
func (NopSession) Set ¶
func (s NopSession) Set(_string, _ string)
func (NopSession) SetMaxAge ¶
func (s NopSession) SetMaxAge(_ time.Duration)
func (NopSession) WriteToResponse ¶
func (s NopSession) WriteToResponse(_ Response)
type OkResponse ¶
func NewJsonpResponse ¶
func NewJsonpResponse(callback string, data interface{}) *OkResponse
func NewResponse ¶
func NewResponse() *OkResponse
func RenderTemplateResponse ¶
func RenderTemplateResponse(t *template.Template, vars interface{}) *OkResponse
Render renders a template using the provided template and vars struct and returns a response with the rendered template
func (*OkResponse) ClearCookie ¶
func (r *OkResponse) ClearCookie(name string)
func (*OkResponse) Content ¶
func (r *OkResponse) Content() []byte
func (*OkResponse) Cookies ¶
func (r *OkResponse) Cookies() []http.Cookie
func (*OkResponse) Headers ¶
func (r *OkResponse) Headers() http.Header
func (*OkResponse) Send ¶
func (r *OkResponse) Send(w http.ResponseWriter)
func (*OkResponse) SendHeaders ¶
func (r *OkResponse) SendHeaders(w http.ResponseWriter)
func (*OkResponse) SetContent ¶
func (r *OkResponse) SetContent(content []byte)
func (*OkResponse) SetCookie ¶
func (r *OkResponse) SetCookie(cookie http.Cookie)
func (*OkResponse) SetNoCache ¶
func (r *OkResponse) SetNoCache()
func (*OkResponse) SetStatusCode ¶
func (r *OkResponse) SetStatusCode(code int)
func (*OkResponse) StatusCode ¶
func (r *OkResponse) StatusCode() int
type RedirectResponse ¶
type RedirectResponse struct { *OkResponse // contains filtered or unexported fields }
func NewPermanentRedirectResponse ¶
func NewPermanentRedirectResponse(urlStr string) *RedirectResponse
func NewRedirectResponse ¶
func NewRedirectResponse(urlStr string) *RedirectResponse
func Redirect ¶
func Redirect(urlStr string) *RedirectResponse
Redirect returns a new RedirectResponse (HTTP 302)
func (*RedirectResponse) Send ¶
func (r *RedirectResponse) Send(w http.ResponseWriter)
func (*RedirectResponse) Target ¶
func (r *RedirectResponse) Target() string
type Request ¶
Request decorates a http.Request to add helper methods
func (*Request) CookieValue ¶
CookieValue returns the value of the named cookie
func (*Request) FormValueOrDefault ¶
FormValueOrDefault returns the result of Request.FormValue, and if the result is empty returns the default string
func (*Request) QueryValue ¶
QueryValue returns the value in the GET query string
func (*Request) QueryValueOrDefault ¶
type Response ¶
type Response interface { Send(http.ResponseWriter) SetCookie(http.Cookie) ClearCookie(string) Cookies() []http.Cookie Content() []byte StatusCode() int SetStatusCode(int) Headers() http.Header }
Response is a http response that can be built up
func NewStringResponse ¶
type Session ¶
type Session interface { GetId() string Get(string) string Set(string, string) SetMaxAge(time.Duration) HasExpired() bool Clear() WriteToResponse(Response) }
func NopSessionFinder ¶
type SessionFinder ¶
SessionFinder finds a session based on the request
func (SessionFinder) Handler ¶
func (finder SessionFinder) Handler(handler GoannaSessionHandlerFunc) GoannaHandlerFunc