Documentation
¶
Overview ¶
This package helps you to write golang application, which can be launched in PaaS Cocaine. Allows your application to communicate with another applications and services.
Typical application describes some handlers for incoming events. You have to register handler for event in special map before starts main eventloop. Look at this example:
func echo(request *cocaine.Request, response *cocaine.Response) { inc := <-request.Read() response.Write(inc) response.Close() } func main() { binds := map[string]cocaine.EventHandler{ "echo": echo, } Worker, err := cocaine.NewWorker() if err != nil { log.Fatal(err) } Worker.Loop(binds) }
Incoming event with named "echo" would be handled with echo func. Request and Response are in/out streams to/from worker. You are able to read incoming chunks of data, associated with current session using request.Read(). To send any chunk to client use response.Write(). Finish datastream by calling response.Close(). You must close response stream at any time before the end of the handler.
Index ¶
- Constants
- func CocaineHeaderToHttpHeader(hdr Headers) http.Header
- func UnpackProxyRequest(raw []byte) (*http.Request, error)
- func WrapHandleFuncs(hfs map[string]http.HandlerFunc, logger *Logger) (handlers map[string]EventHandler)
- func WriteHead(code int, headers Headers) interface{}
- type Endpoint
- type EventHandler
- type HTTPReq
- type Headers
- type Locator
- type Logger
- func (logger *Logger) Close()
- func (logger *Logger) Debug(message ...interface{})
- func (logger *Logger) Debugf(format string, args ...interface{})
- func (logger *Logger) Err(message ...interface{})
- func (logger *Logger) Errf(format string, args ...interface{})
- func (logger *Logger) Info(message ...interface{})
- func (logger *Logger) Infof(format string, args ...interface{})
- func (logger *Logger) Reconnect(force bool) error
- func (logger *Logger) Warn(message ...interface{})
- func (logger *Logger) Warnf(format string, args ...interface{})
- type Request
- type ResolveResult
- type Response
- type ResponseWriter
- type Service
- type ServiceError
- type ServiceMethod
- type ServiceResult
- type Worker
Constants ¶
const ( LOGINGNORE = iota LOGERROR LOGWARN LOGINFO LOGDEBUG )
const ( HANDSHAKE = iota HEARTBEAT TERMINATE INVOKE CHUNK ERROR CHOKE )
const ( HEARTBEAT_TIMEOUT = time.Second * 20 DISOWN_TIMEOUT = time.Second * 5 )
Variables ¶
This section is empty.
Functions ¶
func UnpackProxyRequest ¶
TBD: Extract more info
func WrapHandleFuncs ¶
func WrapHandleFuncs(hfs map[string]http.HandlerFunc, logger *Logger) (handlers map[string]EventHandler)
Types ¶
type EventHandler ¶
func WrapHandler ¶
func WrapHandler(handler http.Handler, logger *Logger) EventHandler
WrapHandler provides opportunity for using Go web frameworks, which supports http.Handler interface
Trivial example which is used martini web framework import ( "github.com/cocaine/cocaine-framework-go/cocaine" "github.com/codegangsta/martini" ) func main() { m := martini.Classic() m.Get("", func() string { return "This is an example server" }) m.Get("/hw", func() string { return "Hello world!" }) binds := map[string]cocaine.EventHandler{ "example": cocaine.WrapHandler(m, nil), } if worker, err := cocaine.NewWorker(); err == nil{ worker.Loop(binds) }else{ panic(err) } }
func WrapHandlerFunc ¶
func WrapHandlerFunc(hf http.HandlerFunc, logger *Logger) EventHandler
WrapHandlerFunc provides opportunity for using Go web frameworks, which supports http.HandlerFunc interface
Trivial example is import ( "net/http" "github.com/cocaine/cocaine-framework-go/cocaine" ) func handler(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/plain") w.Write([]byte("This is an example server.\n")) } func main() { binds := map[string]cocaine.EventHandler{ "example": cocaine.WrapHandlerFunc(handler, nil), } if worker, err := cocaine.NewWorker(); err == nil{ worker.Loop(binds) }else{ panic(err) } }
type Locator ¶
type Locator struct {
// contains filtered or unexported fields
}
func NewLocator ¶
func (*Locator) Resolve ¶
func (locator *Locator) Resolve(name string) chan ResolveResult
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func NewLoggerWithName ¶
type ResolveResult ¶
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Datastream from worker to a client.
func (*Response) Close ¶
func (response *Response) Close()
Notify a client about finishing the datastream.
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
ResponseWriter implements http.ResponseWriter interface. It implements cocaine integration.
func (*ResponseWriter) Header ¶
func (w *ResponseWriter) Header() http.Header
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(code int)
func (*ResponseWriter) WriteString ¶
func (w *ResponseWriter) WriteString(data string) (n int, err error)
type Service ¶
type Service struct { ResolveResult // contains filtered or unexported fields }
Allows you to invoke methods of services and send events to other cloud applications.
func NewService ¶
func (*Service) Call ¶
func (service *Service) Call(name string, args ...interface{}) chan ServiceResult
Calls a remote method by name and pass args
type ServiceError ¶
func (*ServiceError) Error ¶
func (err *ServiceError) Error() string
type ServiceMethod ¶
type ServiceMethod struct { Data []interface{} // contains filtered or unexported fields }
type ServiceResult ¶
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Performs IO operations between application and cocaine-runtime, dispatches incoming messages from runtime.
func (*Worker) Loop ¶
func (worker *Worker) Loop(bind map[string]EventHandler)
Initializes worker in runtime as starting. Launchs an eventloop.