cocaine

package
v0.0.0-...-cdf4a0c Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2018 License: LGPL-3.0 Imports: 18 Imported by: 13

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

View Source
const (
	LOGINGNORE = iota
	LOGERROR
	LOGWARN
	LOGINFO
	LOGDEBUG
)
View Source
const (
	HANDSHAKE = iota
	HEARTBEAT
	TERMINATE
	INVOKE
	CHUNK
	ERROR
	CHOKE
)
View Source
const (
	HEARTBEAT_TIMEOUT = time.Second * 20
)

Variables

This section is empty.

Functions

func CocaineHeaderToHttpHeader

func CocaineHeaderToHttpHeader(hdr Headers) http.Header

func UnpackProxyRequest

func UnpackProxyRequest(raw []byte) (*http.Request, error)

TBD: Extract more info

func WrapHandleFuncs

func WrapHandleFuncs(hfs map[string]http.HandlerFunc, logger *Logger) (handlers map[string]EventHandler)

func WriteHead

func WriteHead(code int, headers Headers) interface{}

Types

type Endpoint

type Endpoint struct {
	Host string
	Port int
}

func (*Endpoint) AsString

func (endpoint *Endpoint) AsString() string

type EventHandler

type EventHandler func(*Request, *Response)

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 FallbackHandler

type FallbackHandler func(string, *Request, *Response)

type HTTPReq

type HTTPReq struct {
	*http.Request
}

type Headers

type Headers [][2]string

func HttpHeaderToCocaineHeader

func HttpHeaderToCocaineHeader(header http.Header) Headers

type LocalLogger

type LocalLogger interface {
	Debug(args ...interface{})
	Debugf(msg string, args ...interface{})
	Info(args ...interface{})
	Infof(msg string, args ...interface{})
	Warn(args ...interface{})
	Warnf(msg string, args ...interface{})
	Err(args ...interface{})
	Errf(msg string, args ...interface{})
}

type LocalLoggerImpl

type LocalLoggerImpl struct{}

func (*LocalLoggerImpl) Debug

func (l *LocalLoggerImpl) Debug(args ...interface{})

func (*LocalLoggerImpl) Debugf

func (l *LocalLoggerImpl) Debugf(msg string, args ...interface{})

func (*LocalLoggerImpl) Err

func (l *LocalLoggerImpl) Err(args ...interface{})

func (*LocalLoggerImpl) Errf

func (l *LocalLoggerImpl) Errf(msg string, args ...interface{})

func (*LocalLoggerImpl) Info

func (l *LocalLoggerImpl) Info(args ...interface{})

func (*LocalLoggerImpl) Infof

func (l *LocalLoggerImpl) Infof(msg string, args ...interface{})

func (*LocalLoggerImpl) Warn

func (l *LocalLoggerImpl) Warn(args ...interface{})

func (*LocalLoggerImpl) Warnf

func (l *LocalLoggerImpl) Warnf(msg string, args ...interface{})

type Locator

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

func NewLocator

func NewLocator(logger LocalLogger, args ...interface{}) (*Locator, error)

func (*Locator) Close

func (locator *Locator) Close()

func (*Locator) Resolve

func (locator *Locator) Resolve(name string) chan ResolveResult

type Logger

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

func NewLogger

func NewLogger(args ...interface{}) (logger *Logger, err error)

func NewLoggerWithName

func NewLoggerWithName(loggerName string, args ...interface{}) (logger *Logger, err error)

func (*Logger) Close

func (logger *Logger) Close()

func (*Logger) Debug

func (logger *Logger) Debug(message ...interface{})

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, args ...interface{})

func (*Logger) Err

func (logger *Logger) Err(message ...interface{})

func (*Logger) Errf

func (logger *Logger) Errf(format string, args ...interface{})

func (*Logger) Info

func (logger *Logger) Info(message ...interface{})

func (*Logger) Infof

func (logger *Logger) Infof(format string, args ...interface{})

func (*Logger) Reconnect

func (logger *Logger) Reconnect(force bool) error

func (*Logger) Warn

func (logger *Logger) Warn(message ...interface{})

func (*Logger) Warnf

func (logger *Logger) Warnf(format string, args ...interface{})

type Request

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

func (*Request) Read

func (request *Request) Read() chan []byte

type ResolveResult

type ResolveResult struct {
	Endpoint `codec:",omitempty"`
	Version  int
	API      map[int64]string
	// contains filtered or unexported fields
}

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.

func (*Response) ErrorMsg

func (response *Response) ErrorMsg(code int, msg string)

Send error to a client. Specify code and message, which describes this error.

func (*Response) Write

func (response *Response) Write(data interface{})

Sends chunk of data to a client.

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) Write

func (w *ResponseWriter) Write(data []byte) (n int, err error)

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 NewService(name string, args ...interface{}) (s *Service, err error)

func NewServiceWithLocalLogger

func NewServiceWithLocalLogger(name string, localLogger LocalLogger, args ...interface{}) (s *Service, err error)

func (*Service) Call

func (service *Service) Call(name string, args ...interface{}) chan ServiceResult

Calls a remote method by name and pass args

func (*Service) Close

func (service *Service) Close()

Disposes resources of a service. You must call this method if the service isn't used anymore.

func (*Service) Reconnect

func (service *Service) Reconnect(force bool) error

type ServiceError

type ServiceError struct {
	Code    int
	Message string
}

func (*ServiceError) Error

func (err *ServiceError) Error() string

type ServiceMethod

type ServiceMethod struct {
	Data []interface{}
	// contains filtered or unexported fields
}

type ServiceResult

type ServiceResult interface {
	Extract(interface{}) error
	Err() error
}

type Worker

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

Performs IO operations between application and cocaine-runtime, dispatches incoming messages from runtime.

func NewWorker

func NewWorker() (worker *Worker, err error)

func NewWorkerWithLocalLogger

func NewWorkerWithLocalLogger(localLogger LocalLogger) (worker *Worker, err error)

Creates new instance of Worker. Returns error on fail.

func (*Worker) Loop

func (worker *Worker) Loop(bind map[string]EventHandler)

Initializes worker in runtime as starting. Launchs an eventloop.

func (*Worker) SetDisownTimeout

func (worker *Worker) SetDisownTimeout(timeout time.Duration)

func (*Worker) SetFallbackHandler

func (worker *Worker) SetFallbackHandler(fallback FallbackHandler)

Jump to

Keyboard shortcuts

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