Documentation ¶
Overview ¶
Package kite is a library for creating micro-services. Two main types implemented by this package are Kite for creating a micro-service server called "Kite" and Client for communicating with another kites.
kontrolclient implements a kite.Client for interacting with Kontrol kite.
Package server implements a HTTP(S) server for kites.
Index ¶
- Variables
- type Auth
- type Client
- func (c *Client) Close()
- func (c *Client) Dial() (err error)
- func (c *Client) DialForever() (connected chan bool, err error)
- func (c *Client) Go(method string, args ...interface{}) chan *response
- func (c *Client) GoWithTimeout(method string, timeout time.Duration, args ...interface{}) chan *response
- func (c *Client) OnConnect(handler func())
- func (c *Client) OnDisconnect(handler func())
- func (c *Client) RemoteAddr() string
- func (c *Client) SetUsername(username string)
- func (c *Client) Tell(method string, args ...interface{}) (result *dnode.Partial, err error)
- func (c *Client) TellWithTimeout(method string, timeout time.Duration, args ...interface{}) (result *dnode.Partial, err error)
- type Error
- type Event
- type EventHandler
- type Handler
- type HandlerFunc
- type Kite
- func (k *Kite) Addr() string
- func (k *Kite) AuthenticateFromKiteKey(r *Request) error
- func (k *Kite) AuthenticateFromToken(r *Request) error
- func (k *Kite) Close()
- func (k *Kite) GetKites(query *protocol.KontrolQuery) ([]*Client, error)
- func (k *Kite) GetToken(kite *protocol.Kite) (string, error)
- func (k *Kite) Handle(method string, handler Handler) *Method
- func (k *Kite) HandleFunc(method string, handler HandlerFunc) *Method
- func (k *Kite) Kite() *protocol.Kite
- func (k *Kite) KontrolReadyNotify() chan struct{}
- func (k *Kite) NewClient(remoteURL string) *Client
- func (k *Kite) OnConnect(handler func(*Client))
- func (k *Kite) OnDisconnect(handler func(*Client))
- func (k *Kite) OnFirstRequest(handler func(*Client))
- func (k *Kite) PostHandle(handler Handler)
- func (k *Kite) PostHandleFunc(handler HandlerFunc)
- func (k *Kite) PreHandle(handler Handler)
- func (k *Kite) PreHandleFunc(handler HandlerFunc)
- func (k *Kite) RSAKey(token *jwt.Token) (interface{}, error)
- func (k *Kite) Register(kiteURL *url.URL) (*registerResult, error)
- func (k *Kite) RegisterForever(kiteURL *url.URL) error
- func (k *Kite) RegisterToProxy(registerURL *url.URL, query *protocol.KontrolQuery)
- func (k *Kite) RegisterToTunnel()
- func (k *Kite) RegisterURL(local bool) *url.URL
- func (k *Kite) Run()
- func (k *Kite) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (k *Kite) ServerCloseNotify() chan bool
- func (k *Kite) ServerReadyNotify() chan bool
- func (k *Kite) SetupKontrolClient() error
- func (k *Kite) SetupSignalHandler()
- func (k *Kite) TrustKontrolKey(issuer, key string)
- func (k *Kite) UseTLS(certPEM, keyPEM string)
- func (k *Kite) UseTLSFile(certFile, keyFile string)
- func (k *Kite) WatchKites(query *protocol.KontrolQuery, onEvent EventHandler) (*Watcher, error)
- type Level
- type Logger
- type Method
- func (m *Method) DisableAuthentication() *Method
- func (m *Method) PostHandle(handler Handler) *Method
- func (m *Method) PostHandleFunc(handler HandlerFunc) *Method
- func (m *Method) PreHandle(handler Handler) *Method
- func (m *Method) PreHandleFunc(handler HandlerFunc) *Method
- func (m *Method) ServeKite(r *Request) (interface{}, error)
- type MethodHandling
- type Request
- type Response
- type TokenRenewer
- type Watcher
Constants ¶
This section is empty.
Variables ¶
var ErrNoKitesAvailable = errors.New("no kites availabile")
Returned from GetKites when query matches no kites.
Functions ¶
This section is empty.
Types ¶
type Auth ¶
type Auth struct { // Type can be "kiteKey", "token" or "sessionID" for now. Type string `json:"type"` Key string `json:"key"` }
Authentication is used when connecting a Client.
type Client ¶
type Client struct { // The information about the kite that we are connecting to. protocol.Kite // A reference to the current Kite running. LocalKite *Kite // Credentials that we sent in each request. Auth *Auth // Should we reconnect if disconnected? Reconnect bool // SockJS base URL URL string // Should we process incoming messages concurrently or not? Default: true Concurrent bool // contains filtered or unexported fields }
Client is the client for communicating with another Kite. It has Tell() and Go() methods for calling methods sync/async way.
func (*Client) DialForever ¶
Dial connects to the remote Kite. If it can't connect, it retries indefinitely. It returns a channel to check if it's connected or not.
func (*Client) Go ¶
Go makes an unblocking method call to the server. It returns a channel that the caller can wait on it to get the response.
func (*Client) GoWithTimeout ¶
func (c *Client) GoWithTimeout(method string, timeout time.Duration, args ...interface{}) chan *response
GoWithTimeout does the same thing with Go() method except it takes an extra argument that is the timeout for waiting reply from the remote Kite. If timeout is given 0, the behavior is same as Go().
func (*Client) OnConnect ¶
func (c *Client) OnConnect(handler func())
OnConnect registers a function to run on connect.
func (*Client) OnDisconnect ¶
func (c *Client) OnDisconnect(handler func())
OnDisconnect registers a function to run on disconnect.
func (*Client) RemoteAddr ¶
func (*Client) SetUsername ¶
func (*Client) Tell ¶
Tell makes a blocking method call to the server. Waits until the callback function is called by the other side and returns the result and the error.
func (*Client) TellWithTimeout ¶
func (c *Client) TellWithTimeout(method string, timeout time.Duration, args ...interface{}) (result *dnode.Partial, err error)
TellWithTimeout does the same thing with Tell() method except it takes an extra argument that is the timeout for waiting reply from the remote Kite. If timeout is given 0, the behavior is same as Tell().
type Error ¶
type Error struct { Type string `json:"type"` Message string `json:"message"` CodeVal string `json:"code"` }
Error is the type of the kite related errors returned from kite package.
type EventHandler ¶
type Handler ¶
Objects implementing the Handler interface can be registered to a method. The returned result must be marshalable with json package.
type HandlerFunc ¶
HandlerFunc is a type adapter to allow the use of ordinary functions as Kite handlers. If h is a function with the appropriate signature, HandlerFunc(h) is a Handler object that calls h.
func (HandlerFunc) ServeKite ¶
func (h HandlerFunc) ServeKite(r *Request) (interface{}, error)
ServeKite calls h(r)
type Kite ¶
type Kite struct { Config *config.Config // Log logs with the given Logger interface Log Logger // SetLogLevel changes the level of the logger. Default is INFO. SetLogLevel func(Level) // Contains different functions for authenticating user from request. // Keys are the authentication types (options.auth.type). Authenticators map[string]func(*Request) error // MethodHandling defines how the kite is returning the response for // multiple handlers MethodHandling MethodHandling TLSConfig *tls.Config Id string // Unique kite instance id // contains filtered or unexported fields }
Kite defines a single process that enables distributed service messaging amongst the peers it is connected. A Kite process acts as a Client and as a Server. That means it can receive request, process them, but it also can make request to other kites.
Do not use this struct directly. Use kite.New function, add your handlers with HandleFunc mehtod, then call Run method to start the inbuilt server (or pass it to any http.Handler compatible server)
func New ¶
New creates, initialize and then returns a new Kite instance. Version must be in 3-digit semantic form. Name is important that it's also used to be searched by others.
func (*Kite) AuthenticateFromKiteKey ¶
AuthenticateFromKiteKey authenticates user from kite key.
func (*Kite) AuthenticateFromToken ¶
AuthenticateFromToken is the default Authenticator for Kite.
func (*Kite) GetKites ¶
func (k *Kite) GetKites(query *protocol.KontrolQuery) ([]*Client, error)
GetKites returns the list of Kites matching the query. The returned list contains Ready to connect Client instances. The caller must connect with Client.Dial() before using each Kite. An error is returned when no kites are available.
func (*Kite) Handle ¶
Handle registers the handler for the given method. The handler is called when a method call is received from a Kite.
func (*Kite) HandleFunc ¶
func (k *Kite) HandleFunc(method string, handler HandlerFunc) *Method
HandleFunc registers a handler to run when a method call is received from a Kite. It returns a *Method option to further modify certain options on a method call
func (*Kite) KontrolReadyNotify ¶
func (k *Kite) KontrolReadyNotify() chan struct{}
KontrolReadyNotify returns a channel that is closed when a successful registiration to kontrol is done.
func (*Kite) NewClient ¶
NewClient returns a pointer to a new Client. The returned instance is not connected. You have to call Dial() or DialForever() before calling Tell() and Go() methods.
func (*Kite) OnDisconnect ¶
OnDisconnect registers a function to run when a connected Kite is disconnected.
func (*Kite) OnFirstRequest ¶
OnFirstRequest registers a function to run when a Kite connects to this Kite.
func (*Kite) PostHandle ¶
PostHandle registers an handler which is executed after a kite.Handler method is executed. Calling PostHandler multiple times registers multiple handlers. A non-error return triggers the execution of the next handler. The execution order is FIFO.
func (*Kite) PostHandleFunc ¶
func (k *Kite) PostHandleFunc(handler HandlerFunc)
PostHandleFunc is the same as PostHandle. It accepts a HandlerFunc.
func (*Kite) PreHandle ¶
PreHandle registers an handler which is executed before a kite.Handler method is executed. Calling PreHandle multiple times registers multiple handlers. A non-error return triggers the execution of the next handler. The execution order is FIFO.
func (*Kite) PreHandleFunc ¶
func (k *Kite) PreHandleFunc(handler HandlerFunc)
PreHandleFunc is the same as PreHandle. It accepts a HandlerFunc.
func (*Kite) RSAKey ¶
RSAKey returns the corresponding public key for the issuer of the token. It is called by jwt-go package when validating the signature in the token.
func (*Kite) Register ¶
Register registers current Kite to Kontrol. After registration other Kites can find it via GetKites() or WatchKites() method. This method does not handle the reconnection case. If you want to keep registered to kontrol, use RegisterForever().
func (*Kite) RegisterForever ¶
RegisterForever is equilavent to Register(), but it tries to re-register if there is a disconnection. The returned error is for the first register attempt. It returns nil if ReadNotify() is ready and it's registered succesfull.
func (*Kite) RegisterToProxy ¶
func (k *Kite) RegisterToProxy(registerURL *url.URL, query *protocol.KontrolQuery)
RegisterToProxy is just like RegisterForever but registers the given URL to kontrol over a kite-proxy. A Kiteproxy is a reverseproxy that can be used for SSL termination or handling hundreds of kites behind a single. This is a blocking function.
func (*Kite) RegisterToTunnel ¶
func (k *Kite) RegisterToTunnel()
RegisterToTunnel finds a tunnel proxy kite by asking kontrol then registers itselfs on proxy. On error, retries forever. On every successfull registration, it sends the proxied URL to the registerChan channel. There is no register URL needed because the Tunnel Proxy automatically gets the IP from tunneling. This is a blocking function.
func (*Kite) RegisterURL ¶
RegisterURL returns a URL that is either local or public. It's an helper method to get a Registration URL that can be passed to Kontrol (via the methods Register(), RegisterToProxy(), etc.) It needs to be called after all configurations are done (like TLS, Port,etc.). If local is true a local IP is used, otherwise a public IP is being used.
func (*Kite) Run ¶
func (k *Kite) Run()
Run is a blocking method. It runs the kite server and then accepts requests asynchronously. It supports graceful restart via SIGUSR2.
func (*Kite) ServerCloseNotify ¶
func (*Kite) ServerReadyNotify ¶
func (*Kite) SetupKontrolClient ¶
SetupKontrolClient setups and prepares a the kontrol instance. It connects to kontrol and reconnects again if there is any disconnections. This method is called internally whenever a kontrol client specific action is taking. However if you wish to connect earlier you may call this method.
func (*Kite) SetupSignalHandler ¶
func (k *Kite) SetupSignalHandler()
SetupSignalHandler listens to signals and toggles the log level to DEBUG mode when it received a SIGUSR2 signal. Another SIGUSR2 toggles the log level back to the old level.
func (*Kite) TrustKontrolKey ¶
Trust a Kontrol key for validating tokens.
func (*Kite) UseTLSFile ¶
func (*Kite) WatchKites ¶
func (k *Kite) WatchKites(query *protocol.KontrolQuery, onEvent EventHandler) (*Watcher, error)
WatchKites watches for Kites that matches the query. The onEvent functions is called for current kites and every nekite event.
type Logger ¶
type Logger interface { // Fatal logs to the FATAL, ERROR, WARNING, INFO and DEBUG levels, // including a stack trace of all running goroutines, then calls // os.Exit(1). Fatal(format string, args ...interface{}) // Error logs to the ERROR, WARNING, INFO and DEBUG level. Error(format string, args ...interface{}) // Warning logs to the WARNING, INFO and DEBUG level. Warning(format string, args ...interface{}) // Info logs to the INFO and DEBUG level. Info(format string, args ...interface{}) // Debug logs to the DEBUG level. Debug(format string, args ...interface{}) }
Logger is the interface used to log messages in different levels.
type Method ¶
type Method struct {
// contains filtered or unexported fields
}
Method defines a method and the Handler it is bind to. By default "ReturnMethod" handling is used.
func (*Method) DisableAuthentication ¶
DisableAuthentication disables authentication check for this method.
func (*Method) PostHandle ¶
PostHandle adds a new kite handler which is executed after the method.
func (*Method) PostHandleFunc ¶
func (m *Method) PostHandleFunc(handler HandlerFunc) *Method
PostHandlerFunc adds a new kite handlerfunc which is executed before the method.
func (*Method) PreHandleFunc ¶
func (m *Method) PreHandleFunc(handler HandlerFunc) *Method
PreHandlerFunc adds a new kite handlerfunc which is executed before the method.
type MethodHandling ¶
type MethodHandling int
MethodHandling defines how to handle chaining of kite.Handler middlewares. An error breaks the chain regardless of what handling is used. Note that all Pre and Post handlers are executed regardless the handling logic, only the return paramater is defined by the handling mode.
const ( // ReturnMethod returns main method's response. This is the standard default. ReturnMethod MethodHandling = iota // ReturnFirst returns the first non-nil response. ReturnFirst // ReturnLatest returns the latest response (waterfall behaviour) ReturnLatest )
type Request ¶
type Request struct { // Method defines the method name which is invoked by the incoming request Method string // Args defines the incoming arguments for the given method Args *dnode.Partial // LocalKite defines a context for the local kite LocalKite *Kite // Client defines a context for the remote kite Client *Client // Username defines the username which the incoming request is bound to. // This is authenticated and validated if authentication is enabled. Username string // Auth stores the authentication information for the incoming request and // the type of authentication. This is not used when authentication is disabled Auth *Auth // Context holds a context that used by the current ServeKite handler. Any // items added to the Context can be fetched from other handlers in the // chain. This is useful with PreHandle and PostHandle handlers to pass // data between handlers. Context cache.Cache }
Request contains information about the incoming request.
type Response ¶
type Response struct { Error *Error `json:"error" dnode:"-"` Result interface{} `json:"result"` }
Response is the type of the object that is returned from request handlers and the type of only argument that is passed to callback functions.
type TokenRenewer ¶
type TokenRenewer struct {
// contains filtered or unexported fields
}
TokenRenewer renews the token of a Client just before it expires.
func NewTokenRenewer ¶
func NewTokenRenewer(r *Client, k *Kite) (*TokenRenewer, error)
func (*TokenRenewer) RenewWhenExpires ¶
func (t *TokenRenewer) RenewWhenExpires()
RenewWhenExpires renews the token before it expires.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package config contains a Config struct for kites.
|
Package config contains a Config struct for kites. |
Package dnode implements a dnode scrubber.
|
Package dnode implements a dnode scrubber. |
examples
|
|
Command line tool for using kite services.
|
Command line tool for using kite services. |
Package kitekey provides method for reading and writing kite.key file.
|
Package kitekey provides method for reading and writing kite.key file. |
Package protocol defines the communication between the components of the Kite infrastructure.
|
Package protocol defines the communication between the components of the Kite infrastructure. |
Package systeminfo provides a way of getting memory usage, disk usage and various information about the host.
|
Package systeminfo provides a way of getting memory usage, disk usage and various information about the host. |
Package testkeys contains RSA keys and TLS certificates for using in examples and tests.
|
Package testkeys contains RSA keys and TLS certificates for using in examples and tests. |
Package testutil provides a default Kontrol kites for using in tests.
|
Package testutil provides a default Kontrol kites for using in tests. |