cheshire

package
v0.0.0-...-5dca3cc Latest Latest
Warning

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

Go to latest
Published: May 20, 2013 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const StrestVersion = float32(2)

what Strest protocol version we are using.

Variables

This section is empty.

Functions

func Flash

func Flash(txn *Txn, severity, message string)

func HandleRequest

func HandleRequest(request *Request, conn Writer, controller Controller, serverConfig *ServerConfig)

Implements the handle request, does the full filter stack.

func HttpListen

func HttpListen(port int, serverConfig *ServerConfig) error

func JsonListen

func JsonListen(port int, config *ServerConfig) error

func RandString

func RandString(length int) string

func Redirect

func Redirect(txn *Txn, url string)

Issues a redirect (301) to the url

func Register

func Register(methods []string, controller Controller)

Registers a new controller

func RegisterApi

func RegisterApi(route string, method string, handler func(*Txn), filters ...ControllerFilter)

Registers a controller funtion for api calls

func RegisterHtml

func RegisterHtml(route string, method string, handler func(*Txn), filters ...ControllerFilter)

Registers a controller function for html pages

func Render

func Render(txn *Txn, path string, context map[string]interface{})

func RenderInLayout

func RenderInLayout(txn *Txn, path, layoutPath string, context map[string]interface{})

Renders with a layout template.

Layout should have {{content}} variable

func SendError

func SendError(txn *Txn, code int, message string) (int, error)

Sends an error response to the channel

func SessionId

func SessionId() string

returns a unique session id

Types

type Bootstrap

type Bootstrap struct {
	Conf *ServerConfig
}

func NewBootstrap

func NewBootstrap(config *ServerConfig) *Bootstrap

func NewBootstrapFile

func NewBootstrapFile(configPath string) *Bootstrap

func NewExtendedBootstrap

func NewExtendedBootstrap(configPath string, extentions []func(conf *ServerConfig)) *Bootstrap

func (*Bootstrap) AddFilters

func (this *Bootstrap) AddFilters(filters ...ControllerFilter)

func (*Bootstrap) InitControllers

func (this *Bootstrap) InitControllers()

func (*Bootstrap) InitProcs

func (this *Bootstrap) InitProcs()

func (*Bootstrap) InitStaticFiles

func (this *Bootstrap) InitStaticFiles()

this needs to be setup correctly to key off of the config yaml

func (*Bootstrap) InitWebSockets

func (this *Bootstrap) InitWebSockets()

func (*Bootstrap) RunInitMethods

func (this *Bootstrap) RunInitMethods(target interface{})

Runs All methods that have prefix of Init

func (*Bootstrap) Start

func (this *Bootstrap) Start()

starts listening in all the configured listeners this method does not return until all listeners exit (i.e. never).

type Cache

type Cache interface {
	Set(key string, value []byte, expireSeconds int)

	// Sets the value if and only if there is no value associated with this key
	SetIfAbsent(key string, value []byte, expireSeconds int) bool

	// Deletes the value at the requested key
	Delete(key string)

	// Gets the value at the requested key
	Get(key string) ([]byte, bool)

	// Increment the key by val (val is allowed to be negative)
	// in most implementation expireSeconds will be from the first increment, but users should not count on that.
	// if no value is a present it should be added.
	// If a value is present which is not a number an error should be returned.
	Inc(key string, val int64, expireSeconds int) (int64, error)
}

A generic cache.

type Controller

type Controller interface {
	Config() *ControllerConfig
	HandleRequest(*Txn)
}

a Controller object

type ControllerConfig

type ControllerConfig struct {
	Route   string
	Filters []ControllerFilter
}

Configuration for a specific controller.

func NewControllerConfig

func NewControllerConfig(route string) *ControllerConfig

type ControllerFilter

type ControllerFilter interface {
	//This is called before the Controller is called.
	//returning false will stop the execution
	Before(*Txn) bool
}

Hooks to hook into before and after the controller execution.

type DefaultController

type DefaultController struct {
	Handlers map[string]func(*Txn)
	Conf     *ControllerConfig
}

func NewController

func NewController(route string, methods []string, handler func(*Txn)) *DefaultController

creates a new controller for the specified route for a specific method types (GET, POST, PUT, ect)

func NewControllerAll

func NewControllerAll(route string, handler func(*Txn)) *DefaultController

creates a new controller that will process all method types

func (*DefaultController) Config

func (this *DefaultController) Config() *ControllerConfig

func (*DefaultController) HandleRequest

func (this *DefaultController) HandleRequest(txn *Txn)

type DefaultNotFoundHandler

type DefaultNotFoundHandler struct {
}

func (*DefaultNotFoundHandler) Config

func (*DefaultNotFoundHandler) HandleRequest

func (h *DefaultNotFoundHandler) HandleRequest(txn *Txn)

type FilterAdvanced

type FilterAdvanced interface {
	ControllerFilter

	//Called immediately before the response is written.
	BeforeWrite(*Response, *Txn)

	//This is called after the controller is called.
	//The response has already been sent
	AfterWrite(*Response, *Txn)
}

Additional hooks if you need more granularity into the lifecycle

type HtmlController

type HtmlController struct {
	Handlers map[string]func(*Txn)
	Conf     *ControllerConfig
}

func NewHtmlController

func NewHtmlController(route string, methods []string, handler func(*Txn)) *HtmlController

func (*HtmlController) Config

func (this *HtmlController) Config() *ControllerConfig

func (*HtmlController) HandleRequest

func (this *HtmlController) HandleRequest(txn *Txn)

func (*HtmlController) HttpHijack

func (this *HtmlController) HttpHijack(writer http.ResponseWriter, req *http.Request, serverConfig *ServerConfig)

We hijack the request so we can use the html writer instead of the regular http writer. mostly this is so the filters know this is of type="html"

type HtmlFilter

type HtmlFilter interface {
	ControllerFilter

	//Allows you to hook in before anything is writen.
	//makes it possible to
	//set headers cookies, ect.
	BeforeHtmlWrite(txn *Txn, writer http.ResponseWriter) bool
}

Special filter for html lifecycle

type HtmlWriter

type HtmlWriter struct {
	*HttpWriter
}

func (*HtmlWriter) Type

func (this *HtmlWriter) Type() string

type HttpHijacker

type HttpHijacker interface {
	HttpHijack(writer http.ResponseWriter, req *http.Request, serverConfig *ServerConfig)
}

Implement this interface for a controller to skip the normal cheshire life cycle This should be only used in special cases (static file serving, websockets, ect) controllers that implement this interface will skip the HandleRequest function alltogether

type HttpWriter

type HttpWriter struct {
	Writer       http.ResponseWriter
	HttpRequest  *http.Request
	Request      *Request
	ServerConfig *ServerConfig
	// contains filtered or unexported fields
}

func ToHttpWriter

func ToHttpWriter(txn *Txn) (*HttpWriter, error)

func (*HttpWriter) Type

func (this *HttpWriter) Type() string

func (*HttpWriter) Write

func (conn *HttpWriter) Write(response *Response) (int, error)

type JsonWriter

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

func (*JsonWriter) Type

func (this *JsonWriter) Type() string

func (*JsonWriter) Write

func (this *JsonWriter) Write(response *Response) (int, error)

type Request

type Request struct {
	dynmap.DynMap
}

Standard STREST request. See protocol spec https://github.com/trendrr/strest-server/wiki/STREST-Protocol-Spec

func NewRequest

func NewRequest(uri, method string) *Request

Create a new request object. Values are all set to defaults

func ToStrestRequest

func ToStrestRequest(req *http.Request) *Request

func (*Request) Method

func (this *Request) Method() string

func (*Request) Params

func (this *Request) Params() *dynmap.DynMap

func (*Request) SetMethod

func (this *Request) SetMethod(method string)

func (*Request) SetParams

func (this *Request) SetParams(params *dynmap.DynMap)

func (*Request) SetTxnAccept

func (this *Request) SetTxnAccept(accept string)

Set to either "single" or "multi"

func (*Request) SetTxnAcceptMulti

func (this *Request) SetTxnAcceptMulti()

This request will accept multiple responses

func (*Request) SetTxnAcceptSingle

func (this *Request) SetTxnAcceptSingle()

This request will only accept a single response

func (*Request) SetTxnId

func (this *Request) SetTxnId(id string)

func (*Request) SetUri

func (this *Request) SetUri(uri string)

func (*Request) ToDynMap

func (this *Request) ToDynMap() *dynmap.DynMap

func (*Request) TxnAccept

func (this *Request) TxnAccept() string

func (*Request) TxnId

func (this *Request) TxnId() string

return the txnid.

func (*Request) Uri

func (this *Request) Uri() string

type RequestTxnId

type RequestTxnId interface {
	TxnId() string
}

Makes it simple to create a new response from anything implementing this interface

type Response

type Response struct {
	dynmap.DynMap
}

Standard STREST response See protocol spec https://github.com/trendrr/strest-server/wiki/STREST-Protocol-Spec

func NewError

func NewError(txn RequestTxnId, code int, message string) *Response

func NewResponse

func NewResponse(txn RequestTxnId) *Response

Creates a new response based on this request txn. auto fills the txn id

func (*Response) SetStatus

func (this *Response) SetStatus(code int, message string)

func (*Response) SetStatusCode

func (this *Response) SetStatusCode(code int)

func (*Response) SetStatusMessage

func (this *Response) SetStatusMessage(message string)

func (*Response) SetTxnId

func (this *Response) SetTxnId(id string)

func (*Response) SetTxnStatus

func (this *Response) SetTxnStatus(status string)

complete or continue

func (*Response) StatusCode

func (this *Response) StatusCode() int

func (*Response) StatusMessage

func (this *Response) StatusMessage() string

func (*Response) ToDynMap

func (this *Response) ToDynMap() *dynmap.DynMap

func (*Response) TxnId

func (this *Response) TxnId() string

func (*Response) TxnStatus

func (this *Response) TxnStatus() string

type RouteMatcher

type RouteMatcher interface {
	// A controller matches the given method, path
	Match(string, string) Controller
	// Registers a controller for the specified methods
	Register([]string, Controller)
}

The Cheshire Router, translates between a uri + method to a controller

type Router

type Router struct {
	NotFoundHandler Controller
	// contains filtered or unexported fields
}

This is a default implementation of a cheshire Router. it is based on the HTTP request multiplexer from golang sources: http://golang.org/src/pkg/net/http/server.go?s=25470:25535#L841

Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.

Should also takes care of sanitizing the URL request path, redirecting any request containing . or .. elements to an equivalent .- and ..-free URL.

func NewDefaultRouter

func NewDefaultRouter() *Router

NewServeMux allocates and returns a new CheshireMux.

func (*Router) Match

func (mux *Router) Match(method string, path string) (h Controller)

Match returns the registered Controller that matches the request or, if no match the registered not found handler is returned

func (*Router) Register

func (this *Router) Register(methods []string, handler Controller)

Handle registers the handler for the given pattern. If a handler already exists for pattern, Handle panics.

type ServerConfig

type ServerConfig struct {
	*dynmap.DynMap
	Router  RouteMatcher
	Filters []ControllerFilter
}

func NewServerConfig

func NewServerConfig() *ServerConfig

Creates a new server config with a default routematcher

func NewServerConfigFile

func NewServerConfigFile(path string) *ServerConfig

Parses a server config from a YAML file

func (*ServerConfig) Register

func (this *ServerConfig) Register(methods []string, controller Controller)

Registers a controller with the RouteMatcher. shortcut to conf.Router.Register(controller)

type Session

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

func NewSession

func NewSession(cache Cache, sessionMaxSeconds int) *Session

func (*Session) Before

func (this *Session) Before(txn *Txn) bool

This is called before the Controller is called. returning false will stop the execution

func (*Session) BeforeHtmlWrite

func (this *Session) BeforeHtmlWrite(txn *Txn, writer http.ResponseWriter) bool

type StaticFileController

type StaticFileController struct {
	Route   string
	Path    string
	Conf    *ControllerConfig
	Handler http.Handler
}

Allows us to use the fast static file handler built into golang standard lib Note that this skips the cheshire lifecycle so no middleware filters will be executed.

func NewStaticFileController

func NewStaticFileController(route string, path string) *StaticFileController

initial the handler via http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))

func (*StaticFileController) Config

func (this *StaticFileController) Config() *ControllerConfig

func (*StaticFileController) HandleRequest

func (this *StaticFileController) HandleRequest(txn *Txn)

func (*StaticFileController) HttpHijack

func (this *StaticFileController) HttpHijack(writer http.ResponseWriter, req *http.Request, serverConfig *ServerConfig)

type Txn

type Txn struct {
	Request *Request

	//writer should be threadsafe
	Writer Writer

	//Session is not currently threadsafe
	Session *dynmap.DynMap

	//The filters that will be run on this txn
	Filters []ControllerFilter

	//the immutable server config
	ServerConfig *ServerConfig
}

Represents a single transaction. This wraps the underlying Writer, and allows saving of session state ect.

func NewTxn

func NewTxn(request *Request, writer Writer, filters []ControllerFilter, serverConfig *ServerConfig) *Txn

func (*Txn) Params

func (this *Txn) Params() *dynmap.DynMap

func (*Txn) TxnId

func (this *Txn) TxnId() string

func (*Txn) Type

func (this *Txn) Type() string

Returns the connection type. currently will be one of http,html,json,websocket

func (*Txn) Write

func (this *Txn) Write(response *Response) (int, error)

Writes a response to the underlying writer.

type WebsocketController

type WebsocketController struct {
	Conf    *ControllerConfig
	Handler websocket.Handler
	// contains filtered or unexported fields
}

func NewWebsocketController

func NewWebsocketController(route string, config *ServerConfig) *WebsocketController

func (WebsocketController) Config

func (WebsocketController) HandleRequest

func (wc WebsocketController) HandleRequest(txn *Txn)

func (*WebsocketController) HandleWCConnection

func (this *WebsocketController) HandleWCConnection(ws *websocket.Conn)

func (*WebsocketController) HttpHijack

func (this *WebsocketController) HttpHijack(writer http.ResponseWriter, req *http.Request, serverConfig *ServerConfig)

implements the HttpHijacker interface so we can handle the request directly.

type WebsocketWriter

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

func (*WebsocketWriter) Type

func (this *WebsocketWriter) Type() string

func (*WebsocketWriter) Write

func (this *WebsocketWriter) Write(response *Response) (int, error)

type Writer

type Writer interface {
	//writes the response to the underlying channel
	// i.e. either to an http response writer or json socket.
	// implementers should make sure that this method is threadsafe as the
	// Writer may be shared across go routines.
	Write(*Response) (int, error)

	//What type of writer is this?
	//Examples: http,json,websocket
	Type() string
}

Directories

Path Synopsis
impl

Jump to

Keyboard shortcuts

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