goanna

package
v0.0.0-...-ec7f410 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2014 License: MIT Imports: 20 Imported by: 3

Documentation

Overview

Package goanna is an MVC toolkit with routing, controllers and sessions

Index

Examples

Constants

View Source
const (
	HEADER_CACHE_CONTROL = "Cache-Control"
	HEADER_CACHE_NOCACHE = "no-cache"
)
View Source
const LogRequestTemplate = `` /* 236-byte string literal not displayed */

Variables

View Source
var Logger *log.Logger
View Source
var Router *mux.Router

Router is a global router used for routing requests

View Source
var UrlBase url.URL

UrlBase is the base url used for creating absolute urls

Functions

func AbsoluteUrlFor

func AbsoluteUrlFor(name string, args ...string) *url.URL

UrlFor returns the avsolute URL for the route name. UrlBase is used for the URL Host and Scheme

func ClearRouter

func ClearRouter()

ClearRouter clears the global router

func Handler

Handler handles functions of type GoannaHandlerFunc

func LogHttpRequest

func LogHttpRequest(r *http.Request, v ...string)

LogHttpRequest logs a http request

func LogRequest

func LogRequest(r *Request, v ...string)

LogRequest logs a goanna request

func RenderTemplate

func RenderTemplate(t *template.Template, vars interface{}) []byte

RenderView renders a template using the provided template and vars struct and returns the rendered tamplate

func UrlFor

func UrlFor(name string, args ...string) *url.URL

UrlFor returns the relative URL for the route name

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

func (*Controller) UrlFor

func (c *Controller) UrlFor(routeName string, args ...string) *url.URL

UrlFor is helper function for controllers

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

type ControllerInterface interface {
	Init()
	Session() Session
	SetRequest(*http.Request)
}

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

type GoannaHandlerFunc func(r *Request) Response

GoannaHandlerFunc is a function type that can be handled by GoannaHandler

type GoannaSessionHandlerFunc

type GoannaSessionHandlerFunc func(r *Request, s Session) Response

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

type OkResponse struct {
	Header http.Header

	Code int
	// contains filtered or unexported fields
}

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 (*RedirectResponse) Target

func (r *RedirectResponse) Target() string

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request decorates a http.Request to add helper methods

func (*Request) BodyData

func (r *Request) BodyData() []byte

BodyData returns the full request body

func (*Request) CookieValue

func (r *Request) CookieValue(name string) string

CookieValue returns the value of the named cookie

func (*Request) FormValueOrDefault

func (r *Request) FormValueOrDefault(key string, def string) string

FormValueOrDefault returns the result of Request.FormValue, and if the result is empty returns the default string

func (*Request) IsGet

func (r *Request) IsGet() bool

IsGet returns whether the request is GET

func (*Request) IsHead

func (r *Request) IsHead() bool

IsPost returns whether the request is HEAD

func (*Request) IsPatch

func (r *Request) IsPatch() bool

IsPatch returns whether the request is PATCH

func (*Request) IsPost

func (r *Request) IsPost() bool

IsPost returns whether the request is POST

func (*Request) IsPut

func (r *Request) IsPut() bool

IsPut returns whether the request is PUT

func (*Request) Log

func (r *Request) Log(v ...string)

func (*Request) QueryValue

func (r *Request) QueryValue(key string) string

QueryValue returns the value in the GET query string

func (*Request) QueryValueOrDefault

func (r *Request) QueryValueOrDefault(key string, def string) string

func (*Request) UrlValue

func (r *Request) UrlValue(key string) string

UrlValue returns whether the request is PATCH

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

func NewStringResponse(content string) Response

type Session

type Session interface {
	GetId() string
	Get(string) string
	Set(string, string)
	SetMaxAge(time.Duration)
	HasExpired() bool
	Clear()
	WriteToResponse(Response)
}

func NopSessionFinder

func NopSessionFinder(_ *Request) Session

type SessionFinder

type SessionFinder func(*Request) Session

SessionFinder finds a session based on the request

func (SessionFinder) Handler

Directories

Path Synopsis
Package session implements a cookie session store for goanna
Package session implements a cookie session store for goanna

Jump to

Keyboard shortcuts

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