Documentation ¶
Index ¶
- Constants
- func Flash(txn *Txn, severity, message string)
- func HandleRequest(request *Request, conn Writer, controller Controller, ...)
- func HttpListen(port int, serverConfig *ServerConfig) error
- func JsonListen(port int, config *ServerConfig) error
- func RandString(length int) string
- func Redirect(txn *Txn, url string)
- func Register(methods []string, controller Controller)
- func RegisterApi(route string, method string, handler func(*Txn), filters ...ControllerFilter)
- func RegisterHtml(route string, method string, handler func(*Txn), filters ...ControllerFilter)
- func Render(txn *Txn, path string, context map[string]interface{})
- func RenderInLayout(txn *Txn, path, layoutPath string, context map[string]interface{})
- func SendError(txn *Txn, code int, message string) (int, error)
- func SessionId() string
- type Bootstrap
- func (this *Bootstrap) AddFilters(filters ...ControllerFilter)
- func (this *Bootstrap) InitControllers()
- func (this *Bootstrap) InitProcs()
- func (this *Bootstrap) InitStaticFiles()
- func (this *Bootstrap) InitWebSockets()
- func (this *Bootstrap) RunInitMethods(target interface{})
- func (this *Bootstrap) Start()
- type Cache
- type Controller
- type ControllerConfig
- type ControllerFilter
- type DefaultController
- type DefaultNotFoundHandler
- type FilterAdvanced
- type HtmlController
- type HtmlFilter
- type HtmlWriter
- type HttpHijacker
- type HttpWriter
- type JsonWriter
- type Request
- func (this *Request) Method() string
- func (this *Request) Params() *dynmap.DynMap
- func (this *Request) SetMethod(method string)
- func (this *Request) SetParams(params *dynmap.DynMap)
- func (this *Request) SetTxnAccept(accept string)
- func (this *Request) SetTxnAcceptMulti()
- func (this *Request) SetTxnAcceptSingle()
- func (this *Request) SetTxnId(id string)
- func (this *Request) SetUri(uri string)
- func (this *Request) ToDynMap() *dynmap.DynMap
- func (this *Request) TxnAccept() string
- func (this *Request) TxnId() string
- func (this *Request) Uri() string
- type RequestTxnId
- type Response
- func (this *Response) SetStatus(code int, message string)
- func (this *Response) SetStatusCode(code int)
- func (this *Response) SetStatusMessage(message string)
- func (this *Response) SetTxnId(id string)
- func (this *Response) SetTxnStatus(status string)
- func (this *Response) StatusCode() int
- func (this *Response) StatusMessage() string
- func (this *Response) ToDynMap() *dynmap.DynMap
- func (this *Response) TxnId() string
- func (this *Response) TxnStatus() string
- type RouteMatcher
- type Router
- type ServerConfig
- type Session
- type StaticFileController
- type Txn
- type WebsocketController
- func (wc WebsocketController) Config() *ControllerConfig
- func (wc WebsocketController) HandleRequest(txn *Txn)
- func (this *WebsocketController) HandleWCConnection(ws *websocket.Conn)
- func (this *WebsocketController) HttpHijack(writer http.ResponseWriter, req *http.Request, serverConfig *ServerConfig)
- type WebsocketWriter
- type Writer
Constants ¶
const StrestVersion = float32(2)
what Strest protocol version we are using.
Variables ¶
This section is empty.
Functions ¶
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 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
Types ¶
type Bootstrap ¶
type Bootstrap struct {
Conf *ServerConfig
}
func NewBootstrap ¶
func NewBootstrap(config *ServerConfig) *Bootstrap
func NewBootstrapFile ¶
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) 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
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 (h *DefaultNotFoundHandler) Config() *ControllerConfig
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
type JsonWriter ¶
type JsonWriter struct {
// contains filtered or unexported fields
}
func (*JsonWriter) Type ¶
func (this *JsonWriter) Type() string
type Request ¶
Standard STREST request. See protocol spec https://github.com/trendrr/strest-server/wiki/STREST-Protocol-Spec
func NewRequest ¶
Create a new request object. Values are all set to defaults
func ToStrestRequest ¶
func (*Request) SetTxnAccept ¶
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
type RequestTxnId ¶
type RequestTxnId interface {
TxnId() string
}
Makes it simple to create a new response from anything implementing this interface
type Response ¶
Standard STREST response See protocol spec https://github.com/trendrr/strest-server/wiki/STREST-Protocol-Spec
func NewResponse ¶
func NewResponse(txn RequestTxnId) *Response
Creates a new response based on this request txn. auto fills the txn id
func (*Response) SetStatusCode ¶
func (*Response) SetStatusMessage ¶
func (*Response) SetTxnStatus ¶
complete or continue
func (*Response) StatusCode ¶
func (*Response) StatusMessage ¶
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 (*Session) Before ¶
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
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 (wc WebsocketController) Config() *ControllerConfig
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
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 }