server

package
v0.12.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2016 License: GPL-3.0 Imports: 15 Imported by: 0

README

Server

This package contains classes for starting and running HTTP and websocket servers.

Server interface

Servers implements the Server interface. A 'ServerConfig' and 'gin.Engine' object is supplied in the 'Start' method, so that they may set themselves up and set up the routes etc.

type Server interface {
	Start(*ServerConfig, *gin.Engine)
	Running() bool
	ShutDown()
}

The Server interface can be found in server.go.

ServeProcess

The ServeProcess does the port binding and listening. You may attach any number of servers to the serve-process, and it will automatically call their 'Start' and 'ShutDown' methods when starting up and shutting down. You may also attach start and shutdown listeners to the ServeProcess.

The ServeProcess class can be found in server.go.

WebSocketServer

The WebSocketServer is a template for servers that use websocket connections rather then HTTP. It will

Config

The config assumes that there is a default HTTP and Websocket server for RPC, and some other fields. See the main README.md for details.

While the system is generic (i.e. it does not care what a Server is or does), the configuration file is not. The reason is that the server is written specifically for eris-db, and I do not want to manage generic config files (or perhaps even one per server).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitLogger

func InitLogger(config *ServerConfig)

This is basically the same code as in tendermint. Initialize root and maybe later also track the loggers here.

func New

func New(ctx ...interface{}) log15.Logger

func NewCORSMiddleware

func NewCORSMiddleware(options CORS) gin.HandlerFunc

func RootHandler

func RootHandler() log15.Handler

See binary/log for an example of usage.

func WriteServerConfig

func WriteServerConfig(filePath string, cfg *ServerConfig) error

Write a server configuration file.

Types

type Bind

type Bind struct {
	Address string `toml:"address"`
	Port    uint16 `toml:"port"`
}

Standard configuration file for the server.

type CORS

type CORS struct {
	Enable           bool     `toml:"enable"`
	AllowOrigins     []string `toml:"allow_origins"`
	AllowCredentials bool     `toml:"allow_credentials"`
	AllowMethods     []string `toml:"allow_methods"`
	AllowHeaders     []string `toml:"allow_headers"`
	ExposeHeaders    []string `toml:"expose_headers"`
	MaxAge           uint64   `toml:"max_age"`
}

Options stores configurations

type HTTP

type HTTP struct {
	JsonRpcEndpoint string `toml:"json_rpc_endpoint"`
}

Standard configuration file for the server.

type HttpService

type HttpService interface {
	Process(*http.Request, http.ResponseWriter)
}

type IdPool

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

Simple id pool. Lets you get and release uints. Will panic if trying to get an id and it's empty.

func NewIdPool

func NewIdPool(totNum uint) *IdPool

func (*IdPool) GetId

func (idp *IdPool) GetId() (uint, error)

Get an id from the pool.

func (*IdPool) ReleaseId

func (idp *IdPool) ReleaseId(id uint)

Release an id back into the pool.

type Logging

type Logging struct {
	ConsoleLogLevel string `toml:"console_log_level"`
	FileLogLevel    string `toml:"file_log_level"`
	LogFile         string `toml:"log_file"`
}

Standard configuration file for the server.

type ServeProcess

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

The ServeProcess wraps all the Servers. Starting it will add all the server handlers to the router and start listening for incoming requests. There is also startup and shutdown events that can be listened to, on top of any events that the servers may have (the default websocket server has events for monitoring sessions. Startup event listeners should be added before calling 'Start()'. Stop event listeners can be added up to the point where the server is stopped and the event is fired.

func NewServeProcess

func NewServeProcess(config *ServerConfig, servers ...Server) *ServeProcess

Creates a new serve process.

func (*ServeProcess) Start

func (this *ServeProcess) Start() error

Initializes all the servers and starts listening for connections.

func (*ServeProcess) StartEventChannel

func (this *ServeProcess) StartEventChannel() <-chan struct{}

Get a start-event channel from the server. The start event is fired after the Start() function is called, and after the server has started listening for incoming connections. An error here .

func (*ServeProcess) Stop

func (this *ServeProcess) Stop(timeout time.Duration) error

Stop will release the port, process any remaining requests up until the timeout duration is passed, at which point it will abort them and shut down.

func (*ServeProcess) StopEventChannel

func (this *ServeProcess) StopEventChannel() <-chan struct{}

Get a stop-event channel from the server. The event happens after the Stop() function has been called, and after the timeout has passed. When the timeout has passed it will wait for confirmation from the http.Server, which normally takes a very short time (milliseconds).

type Server

type Server interface {
	Start(*ServerConfig, *gin.Engine)
	Running() bool
	ShutDown()
}

A server serves a number of different http calls.

type ServerConfig

type ServerConfig struct {
	Bind      Bind      `toml:"bind"`
	TLS       TLS       `toml:"TLS"`
	CORS      CORS      `toml:"CORS"`
	HTTP      HTTP      `toml:"HTTP"`
	WebSocket WebSocket `toml:"web_socket"`
	Logging   Logging   `toml:"logging"`
}

Standard configuration file for the server.

func DefaultServerConfig

func DefaultServerConfig() *ServerConfig

func ReadServerConfig

func ReadServerConfig(filePath string) (*ServerConfig, error)

Read a TOML server configuration file.

type SessionManager

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

Session manager handles the adding, tracking and removing of session objects.

func NewSessionManager

func NewSessionManager(maxSessions uint, wss WebSocketService) *SessionManager

Create a new WebsocketManager.

func (*SessionManager) RemoveSessionCloseEventChannel

func (this *SessionManager) RemoveSessionCloseEventChannel(lChan chan *WSSession) bool

Remove a listener from session close events.

func (*SessionManager) RemoveSessionOpenEventChannel

func (this *SessionManager) RemoveSessionOpenEventChannel(lChan chan *WSSession) bool

Remove a listener from session open events.

func (*SessionManager) SessionCloseEventChannel

func (this *SessionManager) SessionCloseEventChannel() <-chan *WSSession

Add a listener to session close events

func (*SessionManager) SessionOpenEventChannel

func (this *SessionManager) SessionOpenEventChannel() <-chan *WSSession

Add a listener to session open events.

func (*SessionManager) Shutdown

func (this *SessionManager) Shutdown()

TODO

type SessionObserver

type SessionObserver interface {
	NotifyOpened(*WSSession)
	NotifyClosed(*WSSession)
}

Used to track sessions. Will notify when a session are opened and closed.

type TLS

type TLS struct {
	TLS      bool   `toml:"tls"`
	CertPath string `toml:"cert_path"`
	KeyPath  string `toml:"key_path"`
}

Standard configuration file for the server.

type WSSession

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

WSSession wraps a gorilla websocket.Conn, which in turn wraps a net.Conn object. Writing is done using the 'Write([]byte)' method, which passes the bytes on to the write pump over a channel.

func (*WSSession) Close

func (this *WSSession) Close()

Closes the net connection and cleans up. Notifies all the observers.

func (*WSSession) Closed

func (this *WSSession) Closed() bool

Has the session been closed?

func (*WSSession) Id

func (this *WSSession) Id() uint

Get the session id number.

func (*WSSession) Open

func (this *WSSession) Open()

Starts the read and write pumps. Blocks on the former. Notifies all the observers.

func (*WSSession) Opened

func (this *WSSession) Opened() bool

Has the session been opened?

func (*WSSession) Write

func (this *WSSession) Write(msg []byte) error

Write a text message to the client.

type WebSocket

type WebSocket struct {
	WebSocketEndpoint    string `toml:"websocket_endpoint"`
	MaxWebSocketSessions uint   `toml:"max_websocket_sessions"`
	ReadBufferSize       uint   `toml:"read_buffer_size"`
	WriteBufferSize      uint   `toml:"write_buffer_size"`
}

Standard configuration file for the server.

type WebSocketServer

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

The websocket server handles incoming websocket connection requests, upgrading, reading, writing, and session management. Handling the actual requests is delegated to a websocket service.

func NewWebSocketServer

func NewWebSocketServer(maxSessions uint, service WebSocketService) *WebSocketServer

Create a new server. maxSessions is the maximum number of active websocket connections that is allowed. NOTE: This is not the total number of connections allowed - only those that are upgraded to websockets. Requesting a websocket connection will fail with a 503 if the server is at capacity.

func (*WebSocketServer) Running

func (this *WebSocketServer) Running() bool

Is the server currently running.

func (*WebSocketServer) SessionManager

func (this *WebSocketServer) SessionManager() *SessionManager

Get the session-manager.

func (*WebSocketServer) ShutDown

func (this *WebSocketServer) ShutDown()

Shut the server down.

func (*WebSocketServer) Start

func (this *WebSocketServer) Start(config *ServerConfig, router *gin.Engine)

Start the server. Adds the handler to the router and sets everything up.

type WebSocketService

type WebSocketService interface {
	Process([]byte, *WSSession)
}

Services requests. Message bytes are passed along with the session object. The service is expected to write any response back using the Write function on WSSession, which passes the message over a channel to the write pump.

Jump to

Keyboard shortcuts

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