server

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2023 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package server implements all methods required to process messages.

Index

Constants

View Source
const (
	LivezEndpoint  = "/livez"
	ReadyzEndpoint = "/readyz"
)

Variables

View Source
var (
	ErrServerShutdown = errors.New("server was shutdown or already started")
)
View Source
var (
	Handlers = map[string]WSMsgHandler{}
)

Functions

func WSBinanceHandler

func WSBinanceHandler(s *Server, w *WSConnection, msg []byte) error

WSBinanceHandler process message from the binance stream.

func WSDebugHandler

func WSDebugHandler(s *Server, w *WSConnection, msg []byte) error

WSDebugHandler just prints received message.

func WSTinkoffHandler

func WSTinkoffHandler(s *Server, w *WSConnection, msg []byte) error

WSTinkoffHandler handles messages from tinkoff.

Types

type Backend

type Backend struct {
	pb.UnimplementedMonitorServer
	// contains filtered or unexported fields
}

Backend is used to implement pb.Monitor.

func (*Backend) IsRunning

func (b *Backend) IsRunning(ctx context.Context, in *emptypb.Empty) (*wrapperspb.BoolValue, error)

IsRunning returns whether service is in running state.

type BinanceCandle

type BinanceCandle struct {
	EventType string       `json:"e"`
	EventTime int64        `json:"E"`
	Symbol    string       `json:"s"`
	Kline     BinanceKline `json:"k"`
}

type BinanceError

type BinanceError struct {
	Code    int    `json:"code"`
	Message string `json:"msg"`
}

type BinanceKline

type BinanceKline struct {
	StartTime            int64  `json:"t"`
	EndTime              int64  `json:"T"`
	Symbol               string `json:"s"`
	Interval             string `json:"i"`
	FirstTradeID         int64  `json:"f"`
	LastTradeID          int64  `json:"L"`
	Open                 string `json:"o"`
	Close                string `json:"c"`
	High                 string `json:"h"`
	Low                  string `json:"l"`
	Volume               string `json:"v"`
	TradeNum             int64  `json:"n"`
	IsFinal              bool   `json:"x"`
	QuoteVolume          string `json:"q"`
	ActiveBuyVolume      string `json:"V"`
	ActiveBuyQuoteVolume string `json:"Q"`
	Ignore               string `json:"B"`
}

type BinanceMessage

type BinanceMessage struct {
	ID        *int    `json:"id"`
	EventType *string `json:"e"`
	EventTime *int64  `json:"E"`
	ErrorCode *int    `json:"code"`
}

type BinanceOrderBook

type BinanceOrderBook struct {
	EventName     string     `json:"e"`
	EventType     int64      `json:"E"`
	Symbol        string     `json:"s"`
	FirstUpdateID int64      `json:"U"`
	LastUpdateID  int64      `json:"u"`
	Bids          [][]string `json:"b"`
	Asks          [][]string `json:"a"`
}

type BinanceResult

type BinanceResult struct {
	Result *interface{} `json:"result"`
	ID     int          `json:"id"`
}

type Candle

type Candle struct {
	MessageHeader

	Interval string `json:"interval"`
	Open     string `json:"open"`
	High     string `json:"high"`
	Low      string `json:"low"`
	Close    string `json:"close"`
	Volume   string `json:"volume"`
}

func (*Candle) NATSSubject

func (m *Candle) NATSSubject() string

NATSSubject returns the subject for the candle message

type GRPCConfig

type GRPCConfig struct {
	Bind           string `xml:"Bind"`
	TLS            bool   `xml:"TLS"`
	TLSCertificate string `xml:"TLSCertificate"`
	TLSKey         string `xml:"TLSKey"`
}

GRPC Configuration

func DefaultGRPCConfig

func DefaultGRPCConfig() GRPCConfig

DefaultGRPCConfig returns default GRPC config

type Header struct {
	Name string `xml:"Name,attr"`
	Text string `xml:",chardata"`
}

Header represents HTTP header

type HealthStatus

type HealthStatus struct {
	Status string `json:"status"`
	Error  string `json:"error,omitempty"`
}

HealthStatus represents server health status.

type LoggerConfig

type LoggerConfig struct {
	Debug bool `xml:"Debug"`
}

Logger Configuration

func DefaultLoggerConfig

func DefaultLoggerConfig() LoggerConfig

DefaultLoggerConfig returns default Logger config.

type MessageHeader

type MessageHeader struct {
	Symbol  string `json:"symbol"`
	Source  string `json:"source"`
	Time    int64  `json:"time"`
	TimeSrv int64  `json:"time_srv"`
	TimeRcv int64  `json:"time_rcv"`
}

MessageHeader represents common fields for each message

type MonitorConfig

type MonitorConfig struct {
	Bind           string   `xml:"Bind"`
	TLS            bool     `xml:"TLS"`
	TLSCertificate string   `xml:"TLSCertificate"`
	TLSKey         string   `xml:"TLSKey"`
	Headers        []Header `xml:"Header"`
}

Monitor Configuration

func DefaultMonitorConfig

func DefaultMonitorConfig() MonitorConfig

DefaultMonitorConfig returns default Monitor config

type NATSConfig

type NATSConfig struct {
	Name        string `xml:"Name"`
	URL         string `xml:"URL"`
	RetryDelay  int    `xml:"RetryDelay"`
	NoReconnect bool   `xml:"NoReconnect"`
}

NATS Configuration

func DefaultNATSConfig

func DefaultNATSConfig() NATSConfig

DefaultNATSConfig returns default NATS config

func (*NATSConfig) NATSOptions

func (c *NATSConfig) NATSOptions() []nats.Option

NATSOptions returns a list of NATS connection options.

type Quote

type Quote struct {
	MessageHeader

	BidsDepth int        `json:"bids_depth"`
	Bids      [][]string `json:"bids"`
	AsksDepth int        `json:"asks_depth"`
	Asks      [][]string `json:"asks"`
}

func (*Quote) NATSSubject

func (m *Quote) NATSSubject() string

NATSSubject returns the subject for the candle message

type Server

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

func NewServer

func NewServer(config ServerConfig) (*Server, error)

NewServer will setup a new server instance struct.

func (*Server) CloseNATS

func (s *Server) CloseNATS()

CloseNATS closes the NATS connection.

func (*Server) Debugf

func (s *Server) Debugf(format string, v ...any)

Debugf prints the debug message to the log.

func (*Server) Errorf

func (s *Server) Errorf(format string, v ...any)

Errorf prints the error message to the log.

func (*Server) GRPCConfig

func (s *Server) GRPCConfig() GRPCConfig

GRPCConfig returns GRPC configuration.

func (*Server) HandleLivez

func (s *Server) HandleLivez(w http.ResponseWriter, r *http.Request)

HandleLivez returns liveness check.

func (*Server) HandleNATSError

func (s *Server) HandleNATSError(err error)

HandleNATSError handles NATS errors.

func (*Server) HandleReadyz

func (s *Server) HandleReadyz(w http.ResponseWriter, r *http.Request)

HandleReadyz returns readiness check.

func (*Server) HandleSignals

func (s *Server) HandleSignals()

HandleSignals runs a goroutine to handle signals.

func (*Server) IsNATSReconnecting

func (s *Server) IsNATSReconnecting() bool

IsNATSReconnecting returns whether NATS is scheduled to reconnect.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns whether service is running.

func (*Server) IsShutdown

func (s *Server) IsShutdown() bool

IsShutdown returns whether server is performing shutdown.

func (*Server) LoggerConfig

func (s *Server) LoggerConfig() LoggerConfig

LoggerConfig returns Logger configuration.

func (*Server) MonitorConfig

func (s *Server) MonitorConfig() MonitorConfig

MonitorConfig returns MonitorConfig.

func (*Server) NATSConfig

func (s *Server) NATSConfig() NATSConfig

NATSConfig returns NATS configuration.

func (*Server) NATSSend

func (s *Server) NATSSend(subject string, object interface{})

NATSSend sends message to the NATS.

func (*Server) Noticef

func (s *Server) Noticef(format string, v ...any)

Noticef prints the notice message to the log.

func (*Server) ProcessCandle

func (s *Server) ProcessCandle(name string, c *Candle) error

ProcessCandle processes the candle.

func (*Server) ProcessQuote

func (s *Server) ProcessQuote(name string, c *Quote) error

ProcessQuote processes the quote.

func (*Server) ResponseHandler

func (s *Server) ResponseHandler(w http.ResponseWriter, r *http.Request, code int, data interface{})

ResponseHandler handles responses for monitor routes (JSONP and JSON).

func (*Server) ServerConfig

func (s *Server) ServerConfig() ServerConfig

Config returns a copy of Server configuration.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown will shutdown the server instance.

func (*Server) Start

func (s *Server) Start() error

Start the server

func (*Server) StartGRPC

func (s *Server) StartGRPC() error

StartGRPC starts the GRPC server.

func (*Server) StartMonitor

func (s *Server) StartMonitor()

StartMonitor starts the HTTP or HTTPs server if needed.

func (*Server) StartNATS

func (s *Server) StartNATS()

StartNATS starts the NATS client.

func (*Server) StartWS

func (s *Server) StartWS(conn *WSConnection)

StartWS establishes websocket connection.

func (*Server) WSHandleError

func (s *Server) WSHandleError(conn *WSConnection, err error)

WSHandleError handles the error.

func (*Server) WSKeepAlive

func (s *Server) WSKeepAlive(cfg WSConfig, c *websocket.Conn)

WSKeepAlive enabled Ping-Pong with given timeout.

func (*Server) WaitForShutdown

func (s *Server) WaitForShutdown()

WaitForShutdown will block until the server has been fully shutdown.

func (*Server) Warnf

func (s *Server) Warnf(format string, v ...any)

Warnf prints the warning message to the log.

type ServerConfig

type ServerConfig struct {
	Logger    LoggerConfig  `xml:"Logger"`
	Monitor   MonitorConfig `xml:"Monitor"`
	NATS      NATSConfig    `xml:"NATS"`
	GRPC      GRPCConfig    `xml:"GRPC"`
	WebSocket []WSConfig    `xml:"WebSocket"`
}

Server Configuration

func DefaultConfig

func DefaultConfig() ServerConfig

DefaultConfig returns default ServerConfig.

type TinkoffCandle

type TinkoffCandle struct {
	Open     float64   `json:"o"`
	Close    float64   `json:"c"`
	High     float64   `json:"h"`
	Low      float64   `json:"l"`
	Volume   int       `json:"v"`
	Time     time.Time `json:"time"`
	Interval string    `json:"interval"`
	Figi     string    `json:"figi"`
}

type TinkoffError

type TinkoffError struct {
	RequestID string `json:"request_id"`
	Error     string `json:"error"`
}

type TinkoffEvent

type TinkoffEvent struct {
	Event   string          `json:"event"`
	Time    time.Time       `json:"time"`
	Payload json.RawMessage `json:"payload"`
}

type TinkoffOrderBook

type TinkoffOrderBook struct {
	Figi  string      `json:"figi"`
	Depth int         `json:"depth"`
	Bids  [][]float64 `json:"bids"`
	Asks  [][]float64 `json:"asks"`
}

type WSConfig

type WSConfig struct {
	Name         string   `xml:"Name"`
	Enabled      bool     `xml:"Enabled"`
	URL          string   `xml:"URL"`
	Handler      string   `xml:"Handler"`
	DialTimeout  int      `xml:"DialTimeout"`
	RetryDelay   int      `xml:"RetryDelay"`
	PingTimeout  int      `xml:"PingTimeout"`
	ReadLimit    int64    `xml:"ReadLimit"`
	Headers      []Header `xml:"Header"`
	InitMessages []string `xml:"InitMessage"`
}

WebSocket Configuration

type WSConnection

type WSConnection struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*WSConnection) IsWSReconnecting

func (c *WSConnection) IsWSReconnecting() bool

IsWSReconnecting returns whether websocket is scheduled to reconnect.

type WSMsgHandler

type WSMsgHandler func(s *Server, w *WSConnection, msg []byte) error

Jump to

Keyboard shortcuts

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