cookoo: Index | Examples | Files | Directories

package cookoo

import ""

Package cookoo is a Chain-of-Command (CoCo) framework for writing applications.


* Building Web Apps with Cookoo:

* Building CLI Apps with Cookoo:

A chain of command framework works as follows:

* A "route" is constructed as a chain of commands -- a series of
single-purpose tasks that are run in sequence.
* An application is composed of one or more routes.
* Commands in a route communicate using a Context.
* An application Router is used to receive a route name and then
execute the appropriate chain of commands.

To create a new Cookoo application, use cookoo.Cookoo(). This will configure and create a new registry, request router, and context. From there, use the Registry to build chains of commands, and then use the Router to execute chains of commands.

Unlike other CoCo implementations (like Pronto.js or Fortissimo), Cookoo commands are just functions.


There are four types of interrupts that you may wish to return:

1. FatalError: This will stop the route immediately.
2. RecoverableError: This will allow the route to continue moving.
3. Stop: This will stop the current request, but not as an error.
4. Reroute: This will stop executing the current route, and switch to executing another route.

To learn how to write Cookoo applications, you may wish to examine the small Skunk application:


// This is an admittedly contrived example in which we first store a
// "Hello World" message, and then tell the logger to get that stored
// message and write it to the log.
reg, router, cxt := Cookoo()
    Name: "hello",
    Help: "Sends the log message 'Hello World'",
    Does: Tasks{
        // First, store the message "Hello World" in the context.
            Name: "message",
            Fn:   AddToContext,
            Using: []Param{
                    Name:         "hello",
                    DefaultValue: "Hello World",
        // Now get that message and write it to the log.
            Name: "log",
            Fn:   LogMessage,
            Using: []Param{
                    Name: "msg",
                    From: "cxt:message",

router.HandleRequest("hello", cxt, false)



Package Files

commands.go context.go cookoo.go getter.go mapper.go params.go registry.go router.go sync_context.go


const VERSION = "1.3.0"

VERSION provides the current version of Cookoo.

func AddToContext Uses

func AddToContext(cxt Context, params *Params) (interface{}, Interrupt)

AddToContext adds all of the param name/value pairs into the context.


- Any params will be added into the context.

func ForwardTo Uses

func ForwardTo(cxt Context, params *Params) (interface{}, Interrupt)

ForwardTo forwards to the given route name.

To prevent possible loops or problematic re-routes, use ignoreRoutes.


- route: The route to forward to. This is required.
- ignoreRoutes: Route names that should be ignored (generate recoverable errors).

func GetBool Uses

func GetBool(key string, defaultValue bool, source Getter) bool

GetBool gets a boolean value from any Getter.

func GetFloat64 Uses

func GetFloat64(key string, defaultVal float64, source Getter) float64

GetFloat64 gets a float64 from any Getter.

func GetFromFirst Uses

func GetFromFirst(key string, defaultVal interface{}, sources ...Getter) (interface{}, Getter)

GetFromFirst gets the value from the first Getter that has the key.

This provides a method for scanning, for example, Params, Context, and KeyValueDatasource and returning the first one that matches.

If no Getter has the key, the default value is returned, and the returned Getter is an instance of DefaultGetter.

func GetInt Uses

func GetInt(key string, defaultValue int, source Getter) int

GetInt gets an int from any Getter.

func GetInt32 Uses

func GetInt32(key string, defaultValue int32, source Getter) int32

GetInt32 gets an int32 from any Getter.

func GetInt64 Uses

func GetInt64(key string, defaultValue int64, source Getter) int64

GetInt64 gets an int64 from any Getter.

func GetString Uses

func GetString(key, defaultValue string, source Getter) string

GetString is a convenience function for getting strings.

This simplifies getting strings from a Context, a Params, or a GettableDatasource.

func GetUint64 Uses

func GetUint64(key string, defaultVal uint64, source Getter) uint64

GetUint64 gets a uint64 from any Getter.

func HasBool Uses

func HasBool(key string, source Getter) (bool, bool)

HasBool returns the value and a flag indicated whether the flag value was found.

Default value is false if ok is false.

func HasFloat64 Uses

func HasFloat64(key string, source Getter) (float64, bool)

HasFloat64 returns the float64 value for key, and a flag indicated if it was found.

If ok is false, the float value will be 0

func HasInt Uses

func HasInt(key string, source Getter) (int, bool)

HasInt returns the int value for key, and a flag indicated if it was found.

If ok is false, the int value will be 0

func HasInt32 Uses

func HasInt32(key string, source Getter) (int32, bool)

HasInt32 returns the int32 value for key, and a flag indicated if it was found.

If ok is false, the int value will be 0

func HasInt64 Uses

func HasInt64(key string, source Getter) (int64, bool)

HasInt64 returns the int64 value for key, and a flag indicated if it was found.

If ok is false, the int value will be 0

func HasString Uses

func HasString(key string, source Getter) (string, bool)

HasString is a convenience function to perform Has() and return a string.

func HasUint64 Uses

func HasUint64(key string, source Getter) (uint64, bool)

HasUint64 returns the uint64 value for key, and a flag indicated if it was found.

If ok is false, the int value will be 0

func LogMessage Uses

func LogMessage(cxt Context, params *Params) (interface{}, Interrupt)

LogMessage prints a message to the log.


- msg: The message to print
- level: The log level (default: "info")

type BasicRequestResolver Uses

type BasicRequestResolver struct {
    // contains filtered or unexported fields

BasicRequestResolver is a basic resolver that assumes that the given request name *is* the route name.

func (*BasicRequestResolver) Init Uses

func (r *BasicRequestResolver) Init(registry *Registry)

Init initializes the BasicRequestResolver.

func (*BasicRequestResolver) Resolve Uses

func (r *BasicRequestResolver) Resolve(path string, cxt Context) (string, error)

Resolve returns the given path. This is a non-transforming resolver.

type Cmd Uses

type Cmd struct {
    Name  string
    Fn    Command
    Using Parameters

Cmd associates a cookoo.Command to a Route.

The Name is the direct reference to a command. When a Command returns output, that output is inserted into the Context with the key Name.

Fn specifies which cookoo.Command should be executed during this step.

Using contains a list of Parameters that Cookoo can pass into the Command at execution time.

type CmdDef Uses

type CmdDef struct {
    Name  string
    Def   CommandDefinition
    Using Parameters

type Command Uses

type Command func(cxt Context, params *Params) (interface{}, Interrupt)

Command executes a command and returns a result. A Cookoo app has a registry, which has zero or more routes. Each route executes a sequence of zero or more commands. A command is of this type.

type CommandDefinition Uses

type CommandDefinition interface {
    // Run provides the same functionality as a Command function, but with
    // the added benefit of having all of the fields present on the struct. For that
    // reason, there is no Params attached.
    Run(c Context) (interface{}, Interrupt)

CommandDefinition describes the fields that should be attached to a Command.

CommandDefinitions should be composed of public fields and a Run method.

type Person struct {
	Name string
	Age int
	IgnoreMe string `coo:"-"`

func Map Uses

func Map(c Context, p *Params, d CommandDefinition) (CommandDefinition, Interrupt)

Map merges params into a CommandDefinition and returns a Command Definition.

type Context Uses

type Context interface {
    // Add puts a name/value pair to the context.
    // DEPRECATED. This will be removed in Cookoo 2.0. Use
    // Put instead.
    Add(string, ContextValue)

    // Put inserts a name/value pair into the context.
    // This is used to add data to a context. The context does nothing
    // to manage manipulation of context values. Values are placed in
    // as-is, and are retrieved as-is. Unless an implementor has
    // made a value immutable, context values are mutable.
    Put(string, ContextValue)

    // Given a name, get a value from the context.
    // Get requires a default value (which may be nil).
    // Example:
    // 	ip := cxt.Get("ip", "").(string)
    // Contrast this usage with that of cxt.Has(), which may be used for more
    // traditional field checking:
    // Example:
    // 	ip, ok := cxt.Has("ip")
    // 	if !ok {
    // 		// do something error-ish
    // 	}
    // 	ipStr := ip.(string)
    // The cxt.Get() call avoids the cumbersome check/type-assertion combo
    // that occurs with cxt.Has().
    Get(string, interface{}) ContextValue
    // Given a name, check if the key exists, and if it does return the value.
    Has(string) (ContextValue, bool)
    // Get a datasource by name.
    Datasource(string) Datasource
    // Get a map of all datasources.
    Datasources() map[string]Datasource
    // Check if a datasource exists, and return it if it does.
    HasDatasource(string) (Datasource, bool)
    // Add a datasource.
    AddDatasource(string, Datasource)
    // Remove a datasource from the context.
    // Get the length of the context. This is the number of context values.
    // Datsources are not counted.
    Len() int
    // Make a shallow copy of the context.
    Copy() Context
    // Get the content (no datasources) as a map.
    AsMap() map[string]ContextValue
    // Get a logger.
    Logger(name string) (io.Writer, bool)
    // Add a logger.
    AddLogger(name string, logger io.Writer)
    // Remove a logger.
    RemoveLogger(name string)
    // Send a log with a prefix.
    Log(prefix string, v ...interface{})
    // Send a log and formatting string with a prefix.
    Logf(prefix string, format string, v ...interface{})

A Context is a collection of data that is associated with the current request.

Contexts are used to exchange information from command to command inside of a particular chain of commands (a route). Commands may access the data inside of a context, and may also modify a context.

A context maintains two different types of data: *context variables* and *datasources*.

Context variables are data that can be passed, in current form, from command to command -- analogous to passing variables via parameters in function calls.

Datasources are (as the name implies) sources of data. For example, a database, a file, a cache, and a key-value store are all datasources.

For long-running apps, it is generally assumed (though by no means required) that datasources are "long lived" and context variables are "short lived." While modifying a data source may impact other requests, generally it is safe to assume that modifying a variable is localized to the particular request.

Correct Usage

A word of warning.

The Cookoo system was designed around the theory that commands should generally work with datasources *directly* and context variables *indirectly*. Context variables should generally be passed into a command via a cookoo.Param. And a command generally should return a value that can then be placed into the context on its behalf.

The reason for this design is that it then makes it much easier for higher- level programming, such as changing input or modifying output at the registry level, not within the commands themselves.

Datasources, on the other hand, are designed to be leveraged primarily by commands. This involves a layer of conventionality, but it also pushes data access logic into the commands where it belongs.

So, for example, a SQL-based datasource should be *declared* at the top level of a program (where it will be added to the context), but the actual interaction with that datasource should happen inside of commands themselves, not at the registry level.

func NewContext Uses

func NewContext() Context

NewContext creates a new empty cookoo.ExecutionContext and calls its Init() method.

func SyncContext Uses

func SyncContext(cxt Context) Context

SyncContext wraps a context, syncronizing access to it.

This uses a read/write mutex which allows multiple reads at a time, but locks both reading and writing for writes.

To avoid really nefarious bugs, the same mutex locks context values and datasource values (since there is no guarantee that one is not backed by the other).

type ContextValue Uses

type ContextValue interface{}

ContextValue is an empty interface defining a context value. Semantically, this is the same as interface{}

type Datasource Uses

type Datasource interface{}

Datasource is an empty interface defining a Datasource. Semantically, this is the same as interface{}

type DefaultGetter Uses

type DefaultGetter struct {
    // contains filtered or unexported fields

DefaultGetter represents a Getter instance for a default value.

A default getter always returns the given default value.

func (*DefaultGetter) Get Uses

func (e *DefaultGetter) Get(name string, value interface{}) interface{}

func (*DefaultGetter) Has Uses

func (e *DefaultGetter) Has(name string) (interface{}, bool)

type ExecutionContext Uses

type ExecutionContext struct {
    // contains filtered or unexported fields

ExecutionContext is the core implementation of a Context.

An ExecutionContext is an unordered map-based context.

func (*ExecutionContext) Add Uses

func (cxt *ExecutionContext) Add(name string, value ContextValue)

Add a name/value pair to the context. DEPRECATED: Use Put instead.

func (*ExecutionContext) AddDatasource Uses

func (cxt *ExecutionContext) AddDatasource(name string, ds Datasource)

AddDatasource adds a datasource to the map of datasources. A datasource is typically something like a connection to a database that you want to keep open persistently and share between requests. To add a datasource to the map just add it with a name. e.g. cxt.AddDatasource("mysql", foo) where foo is the struct for the datasource.

func (*ExecutionContext) AddLogger Uses

func (cxt *ExecutionContext) AddLogger(name string, logger io.Writer)

AddLogger adds a logger. The logging system can have one of more loggers keyed by name.

func (*ExecutionContext) AsMap Uses

func (cxt *ExecutionContext) AsMap() map[string]ContextValue

AsMap returns the values of the context as a map keyed by a string.

func (*ExecutionContext) Copy Uses

func (cxt *ExecutionContext) Copy() Context

Copy the context into a new context.

func (*ExecutionContext) Datasource Uses

func (cxt *ExecutionContext) Datasource(name string) Datasource

Datasource get a datasource from the map of datasources. A datasource (e.g., a connection to a database) is retrieved as an interface so its type will need to be specified before it can be used. Take an example of the variable foo that is a struct of type Foo. foo = cxt.Datasource("foo").(*Foo)

func (*ExecutionContext) Datasources Uses

func (cxt *ExecutionContext) Datasources() map[string]Datasource

Datasources gets the map of datasources.

func (*ExecutionContext) Get Uses

func (cxt *ExecutionContext) Get(name string, defaultValue interface{}) ContextValue

Get retrieves a value from the context given a name. If a value does not exist on the context the default is returned.

func (*ExecutionContext) GetAll Uses

func (cxt *ExecutionContext) GetAll() map[string]ContextValue

GetAll gets a map of all name/value pairs in the present context.

func (*ExecutionContext) Has Uses

func (cxt *ExecutionContext) Has(name string) (value ContextValue, found bool)

Has is a special form of Get that also returns a flag indicating if the value is found. This fetches the value and also returns a flag indicating if the value was found. This is useful in cases where the value may legitimately be 0.

func (*ExecutionContext) HasDatasource Uses

func (cxt *ExecutionContext) HasDatasource(name string) (Datasource, bool)

HasDatasource checks whether the named datasource exists, and return it if it does.

func (*ExecutionContext) Init Uses

func (cxt *ExecutionContext) Init() *ExecutionContext

Init initializes a context.

If an existing context is re-initialized, all of its associated values, datasources, and loggers will be unset.

func (*ExecutionContext) Len Uses

func (cxt *ExecutionContext) Len() int

Len returns the length of the context as in the length of the values stores.

func (*ExecutionContext) Log Uses

func (cxt *ExecutionContext) Log(prefix string, v ...interface{})

Log logs a message to one of more loggers.

func (*ExecutionContext) Logf Uses

func (cxt *ExecutionContext) Logf(prefix string, format string, v ...interface{})

Logf logs a message to one or more loggers and uses a format string.

func (*ExecutionContext) Logger Uses

func (cxt *ExecutionContext) Logger(name string) (io.Writer, bool)

Logger gets a logger. The logging system can have one or more loggers that are stored keyed by name.

func (*ExecutionContext) Put Uses

func (cxt *ExecutionContext) Put(name string, value ContextValue)

Put inserts a value into the context.

func (*ExecutionContext) RemoveDatasource Uses

func (cxt *ExecutionContext) RemoveDatasource(name string)

RemoveDatasource removes a datasouce from the map of datasources.

func (*ExecutionContext) RemoveLogger Uses

func (cxt *ExecutionContext) RemoveLogger(name string)

RemoveLogger removes a logger. The logging system can have one of more loggers keyed by name.

func (*ExecutionContext) SkipLogPrefix Uses

func (cxt *ExecutionContext) SkipLogPrefix(prefixes ...string)

SkipLogPrefix ignores logging messages to any of the given prefixes.

While this is not a part of the Context interface, the ExecutionContext allows you to ignore certain logging prefixes. For example, to ignore the `debug` and `info` messages, you might want to do something like this:

cxt.(*ExecutionContext).SkipLogPrefix("debug", "info")
cxt.Logf("debug", "This message will be ignored.")

In the above case, the subsequent call to `Logf()` is ignored.

type FatalError Uses

type FatalError struct {
    Message string

FatalError is a fatal error, which will stop the router from continuing a route.

When Cookoo encounters a `FatalError`, it will log the error and immediately stop processing the route.

Note that by default Cookoo treats and unhandled `error` as if it were a `FatalError`.

func (*FatalError) Error Uses

func (err *FatalError) Error() string

Error returns the error message.

type Getter Uses

type Getter interface {
    Get(string, interface{}) interface{}
    Has(string) (interface{}, bool)

Getter can get values in two ways.

A Get() can be given a default value, in which case it will return either the value associated with the key or, if that's not found, the default value.

A Has() doesn't take a default value, but instead returns both the value (if found) and a boolean flag indicating whether it is found.

In Cookoo 1.x, Context's Get() function returns a ContextValue instead of an interface. For that reason, you may need to wrap Cxt in GettableCxt to make it a true Getter.

In Cookoo 1.x, KeyValueDatasource uses Value() instead of Get()/Has(). For that reason, you can wrap a KeyValueDatasource in a GettableDS() to make it behave like a Getter.

func GettableCxt Uses

func GettableCxt(cxt Context) Getter

GettableCxt makes a Context into a Getter.

This is forward-compatibility code, and will be rendered unnecessary in Cookoo 2.x.

func GettableDS Uses

func GettableDS(ds KeyValueDatasource) Getter

GettableDS makes a KeyValueDatasource into a Getter.

This is forward-compatibility code, and will be rendered unnecessary in Cookoo 2.x.

type Include Uses

type Include struct {
    Path string

Include imports all of the Tasks on another route into the present Route.

type Interrupt Uses

type Interrupt interface{}

Interrupt is a generic return for a command. Generally, a command should return one of the following in the interrupt slot: - A FatalError, which will stop processing. - A RecoverableError, which will continue the chain. - A Reroute, which will cause a different route to be run.

type KeyValueDatasource Uses

type KeyValueDatasource interface {
    Value(key string) interface{}

KeyValueDatasource is a datasource that can retrieve values by (string) keys. Datsources can be just about anything. But a key/value datasource can be used for a special purpose. They can be accessed in From() clauses in a registry configuration.

type Param Uses

type Param struct {
    Name         string
    DefaultValue interface{}
    From         string

Param describes an individual parameter which will be passed to a Command.

The Name is the name of the parameter. The Command itself dictates which Names it uses.

The DefaultValue is the value of the Parameter if nothing else is specified.

From indicates where the Param value may come from. Examples: `From("cxt:foo")` gets the value from the value of the key 'foo' in the Context.

type Parameters Uses

type Parameters []Param

type Params Uses

type Params struct {
    // contains filtered or unexported fields

func NewParams Uses

func NewParams(size int) *Params

NewParams creates a Params object of a given size.

func NewParamsWithValues Uses

func NewParamsWithValues(initialVals map[string]interface{}) *Params

NewParamsWithValues initializes a Params object with the given values.

Create a new Params instance, initialized with the given map. Note that the given map is actually used (not copied).

func (*Params) AsMap Uses

func (p *Params) AsMap() map[string]interface{}

AsMap returns all parameters as a map[string]interface{}.

This does no checking of the parameters.

func (*Params) Get Uses

func (p *Params) Get(name string, defaultValue interface{}) interface{}

Get gets a parameter value, or returns the default value.

func (*Params) Has Uses

func (p *Params) Has(name string) (value interface{}, ok bool)

Has checks if a parameter exists, and return it if found.

func (*Params) Init Uses

func (p *Params) Init(initialValues map[string]interface{})

Init initializes a Params object with an initial map of values.

func (*Params) Len Uses

func (p *Params) Len() int

Len returns the number of params.

func (*Params) Requires Uses

func (p *Params) Requires(paramNames ...string) (ok bool, missing []string)

Requires verifies that the given keys exist in the Params.

Require that a given list of parameters are present. If they are all present, ok = true. Otherwise, ok = false and the `missing` array contains a list of missing params.

func (*Params) RequiresValue Uses

func (p *Params) RequiresValue(paramNames ...string) (ok bool, missing []string)

RequiresValue verifies that the given keys exist and that their values are non-empty.

Requires that given parameters are present and non-empty. This is more powerful than Requires(), which simply checks to see if the the Using() clause declared the value.

func (*Params) Validate Uses

func (p *Params) Validate(name string, validator func(interface{}) bool) (value interface{}, ok bool)

Validate provides a validator callback for params. Given a name and a validation function, return a valid value. If the value is not valid, ok = false.

type RecoverableError Uses

type RecoverableError struct {
    Message string

RecoverableError is an error that should not cause the router to stop processing.

When Cookoo encounters a `RecoverableError`, it will log the error as a warning, but will then continue to execute the next command in the route.

func (*RecoverableError) Error Uses

func (err *RecoverableError) Error() string

Error returns the error message.

type Registry Uses

type Registry struct {
    // contains filtered or unexported fields

A Registry contains the the callback routes and the commands each route executes.

func Cookoo Uses

func Cookoo() (reg *Registry, router *Router, cxt Context)

Cookoo creates a new Cookoo app.

This is the main progenitor of a Cookoo application. Whether a plain Cookoo app, or a Web or CLI program, this is the function you will use to bootstrap.

The `*Registry` is used to declare new routes, where a "route" may be thought of as a task composed of a series of steps (commands).

The `*Router` is responsible for the actual execution of a Cookoo route. The main method used to call a route is `Router.HandleRequest()`.

The `Context` is a container for passing information down a chain of commands. Apps may insert "global" information to a context at startup and make it available to all commands.


reg, router, cxt := Cookoo()
    // The name of the route. You execute routes by name. (See router.HandleRequest below)
    Name: "hello",
    // This is for documentation/help tools.
    Help: "Print a message on standard output",

    // This is a list of things you want this route to do. When executed,
    // it will run these commands in order.
    Does: Tasks{
        // Declare a new command.
            // Give the command a name. Programs reference command output
            // by this name.
            Name: "print",

            // Tell Cookoo what function to execute when we get to this
            // step.
            // Usually we define functions elsewhere so we can re-use them.
            Fn: func(c Context, p *Params) (interface{}, Interrupt) {
                // Print whatever the content of the 'msg' parameter is.
                fmt.Println(p.Get("msg", "").(string))
                return nil, nil
            // Send some parameters into Fn. Here we define the 'msg'
            // parameter that Fn prints. While we just use a default
            // value here, Cookoo can get that information from another
            // source and then send it into Fn.
            Using: []Param{
                    Name:         "msg",
                    DefaultValue: "Hello World",

// Now we execute the "hello" chain of commands.
router.HandleRequest("hello", cxt, false)


Hello World

func NewRegistry Uses

func NewRegistry() *Registry

NewRegistry returns a new initialized registry.

func (*Registry) AddRoute Uses

func (r *Registry) AddRoute(route Route) error

AddRoute adds a single route to the registry.

func (*Registry) AddRoutes Uses

func (r *Registry) AddRoutes(routes ...Route) error

AddRoutes adds one or more routes to the registry.

func (*Registry) Does Uses

func (r *Registry) Does(cmd Command, commandName string) *Registry

Does adds a command to the end of the chain of commands for the current (most recently specified) route.

func (*Registry) DoesCmdDef Uses

func (r *Registry) DoesCmdDef(cd CommandDefinition, name string) *Registry

func (*Registry) From Uses

func (r *Registry) From(fromVal ...string) *Registry

From sepcifies where to get the value from for the most recently specified paramater as set by Using.

func (*Registry) Includes Uses

func (r *Registry) Includes(route string) *Registry

Includes makes the commands from another route avaiable on this route.

func (*Registry) Init Uses

func (r *Registry) Init() *Registry

Init initializes a registry. If a Registry is created through a means other than NewRegistry Init should be called on it.

func (*Registry) Route Uses

func (r *Registry) Route(name, description string) *Registry

Route specifies a new route to add to the registry.

func (*Registry) RouteNames Uses

func (r *Registry) RouteNames() []string

RouteNames gets a slice containing the names of every registered route.

The route names are returned in the order they were added to the registry. This is useful to some resolvers, which apply rules in order.

func (*Registry) RouteSpec Uses

func (r *Registry) RouteSpec(routeName string) (spec *routeSpec, ok bool)

RouteSpec gets a ruote cased on its name.

func (*Registry) Routes Uses

func (r *Registry) Routes() map[string]*routeSpec

Routes gets an unordered map of routes names to route specs.

If order is important, use RouteNames to get the names (in order).

func (*Registry) Using Uses

func (r *Registry) Using(name string) *Registry

Using specifies a paramater to use for the most recently specified command as set by Does.

func (*Registry) WithDefault Uses

func (r *Registry) WithDefault(value interface{}) *Registry

WithDefault specifies the default value for the most recently specified parameter as set by Using.

type RequestResolver Uses

type RequestResolver interface {
    Init(registry *Registry)
    Resolve(path string, cxt Context) (string, error)

RequestResolver is the interface for the request resolver. A request resolver is responsible for transforming a request name to a route name. For example, a web-specific resolver may take a URI and return a route name. Or it make take an HTTP verb and return a route name.

type Reroute Uses

type Reroute struct {
    Route string

Reroute is a command can return a Reroute to tell the router to execute a different route.

A `Command` may return a `Reroute` to cause Cookoo to stop executing the current route and jump to another.

func Forward(c Context, p *Params) (interface{}, Interrupt) {
	return nil, &Reroute{"anotherRoute"}

func NewReroute Uses

func NewReroute(route string) *Reroute

Creates a new Reroute.

func (*Reroute) RouteTo Uses

func (rr *Reroute) RouteTo() string

RouteTo returns the route to reroute to.

type Route Uses

type Route struct {
    Name, Help string
    Does       Tasks

Route declares a new Cookoo route.

A Route has a name, which is used to identify and call it, and Help. The Help can be used by other tools to generate help text or information about an application's structure.

Routes are composed of a series of Tasks, each of which is executed in order.

type RouteDetails Uses

type RouteDetails interface {
    Name() string
    Description() string

type RouteError Uses

type RouteError struct {
    Message string

RouteError indicates that a route cannot be executed successfully.

func (*RouteError) Error Uses

func (e *RouteError) Error() string

Error returns the error.

type Router Uses

type Router struct {
    // contains filtered or unexported fields

Router is the Cookoo router. A Cookoo app works by passing a request into a router, and relying on the router to execute the appropriate chain of commands.

func NewRouter Uses

func NewRouter(reg *Registry) *Router

NewRouter creates a new Router.

func (*Router) HandleRequest Uses

func (r *Router) HandleRequest(name string, cxt Context, taint bool) error

HandleRequest does a request. This executes a request "named" name (this string is passed through the request resolver.) The context is cloned (shallow copy) and passed in as the base context.

If taint is `true`, then no routes that begin with `@` can be executed. Taint should be set to true on anything that relies on a name supplied by an external client.

This will do the following:

- resolve the request name into a route name (using a RequestResolver)
- look up the route
- execute each command on the route in order

The following context variables are placed into the context during a run:

route.Name - Processed name of the current route
route.Description - Description of the current route
route.RequestName - raw route name as passed by the client
command.Name - current command name (changed with each command)

If an error occurred during processing, an error type is returned.

func (*Router) HasRoute Uses

func (r *Router) HasRoute(name string) bool

HasRoute checks whether or not the route exists. Note that this does NOT resolve a request name into a route name. This expects a route name.

func (*Router) Init Uses

func (r *Router) Init(registry *Registry) *Router

Init initializes the Router.

func (*Router) RequestResolver Uses

func (r *Router) RequestResolver() RequestResolver

RequestResolver gets the request resolver.

func (*Router) ResolveRequest Uses

func (r *Router) ResolveRequest(name string, cxt Context) (string, error)

ResolveRequest resolver a given string into a route name.

func (*Router) SetRegistry Uses

func (r *Router) SetRegistry(reg *Registry)

SetRegistry sets the registry.

func (*Router) SetRequestResolver Uses

func (r *Router) SetRequestResolver(resolver RequestResolver)

SetRequestResolver sets the request resolver. The resolver is responsible for taking an arbitrary string and resolving it to a registry route.

Example: Take a URI and translate it to a route.

type Stop Uses

type Stop struct{}

Stop a route, but not as an error condition.

When Cookoo encounters a `Stop`, it will not execute any more commands on a given route. However, it will not emit an error, either.

type Task Uses

type Task interface {
    // contains filtered or unexported methods

type Tasks Uses

type Tasks []Task

Tasks represents a list of discrete tasks that are run on a Route.

There are two kinds of Tasks: Cmd (a command) and Include, which imports a Tasks list from another route.


database/activeactive package contains basic Active Record support.
database/sqlSQL datasource and commands for Cookoo.
fmtThe Cookoo `fmt` package provides utility wrappers for formatting text.
logPackage log contains logging helpers.
safelySafely is a package for providing safety wrappers around commonly used features.
webExtra datasources for Web servers.

Package cookoo imports 7 packages (graph) and is imported by 261 packages. Updated 2016-07-14. Refresh now. Tools for package owners.