gateway

package
v0.0.0-...-8acab51 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package gateway provides a WebSocket-based gateway for real-time communication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Enabled indicates if the gateway is enabled.
	Enabled bool `yaml:"enabled"`
	// ReadBufferSize is the WebSocket read buffer size.
	ReadBufferSize int `yaml:"read_buffer_size"`
	// WriteBufferSize is the WebSocket write buffer size.
	WriteBufferSize int `yaml:"write_buffer_size"`
	// MaxMessageSize is the maximum message size in bytes.
	MaxMessageSize int64 `yaml:"max_message_size"`
	// PingInterval is the interval for ping messages.
	PingIntervalSeconds int `yaml:"ping_interval_seconds"`
	// PongTimeout is the timeout for pong responses.
	PongTimeoutSeconds int `yaml:"pong_timeout_seconds"`
	// WriteTimeout is the timeout for write operations.
	WriteTimeoutSeconds int `yaml:"write_timeout_seconds"`
	// RequestTimeout is the timeout for a single gateway request.
	// 0 disables hard request timeout and relies on connection cancellation.
	RequestTimeoutSeconds int `yaml:"request_timeout_seconds"`
	// MaxConnections is the maximum number of connections.
	MaxConnections int `yaml:"max_connections"`
}

Config contains gateway configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default gateway configuration.

type Connection

type Connection struct {
	ID     string
	UserID string

	// Metadata
	Metadata map[string]interface{}

	// Statistics
	MessagesSent     atomic.Int64
	MessagesReceived atomic.Int64
	ConnectedAt      time.Time
	// contains filtered or unexported fields
}

Connection represents a WebSocket connection.

func NewConnection

func NewConnection(ws *websocket.Conn, cfg Config, logger *zap.Logger) *Connection

NewConnection creates a new connection.

func (*Connection) Close

func (c *Connection) Close()

Close closes the connection.

func (*Connection) Info

func (c *Connection) Info() map[string]interface{}

Info returns connection information.

func (*Connection) Send

func (c *Connection) Send(msg *Message) error

Send sends a message to the connection.

func (*Connection) SetUserID

func (c *Connection) SetUserID(userID string)

SetUserID sets the user ID for the connection.

type ErrorPayload

type ErrorPayload struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ErrorPayload represents an error in a message.

type Gateway

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

Gateway manages WebSocket connections.

func NewGateway

func NewGateway(cfg Config, logger *zap.Logger) *Gateway

NewGateway creates a new gateway.

func (*Gateway) Broadcast

func (g *Gateway) Broadcast(msg *Message)

Broadcast sends a message to all connections.

func (*Gateway) BroadcastToUser

func (g *Gateway) BroadcastToUser(userID string, msg *Message)

BroadcastToUser sends a message to all connections for a user.

func (*Gateway) Config

func (g *Gateway) Config() Config

Stats returns gateway statistics.

func (*Gateway) GetConnection

func (g *Gateway) GetConnection(id string) (*Connection, bool)

GetConnection returns a connection by ID.

func (*Gateway) GetConnections

func (g *Gateway) GetConnections() []*Connection

GetConnections returns all connections.

func (*Gateway) HandleWebSocket

func (g *Gateway) HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket handles WebSocket upgrade requests.

func (*Gateway) Methods

func (g *Gateway) Methods() []string

Methods returns the registered gateway method names.

func (*Gateway) RegisterHandler

func (g *Gateway) RegisterHandler(method string, handler RequestHandler)

RegisterHandler registers a request handler.

func (*Gateway) Stats

func (g *Gateway) Stats() map[string]interface{}

Stats returns gateway statistics.

func (*Gateway) Stop

func (g *Gateway) Stop()

Stop stops the gateway.

type Handler

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

Handler provides HTTP handlers for gateway management.

func NewHandler

func NewHandler(gateway *Gateway, logger *zap.Logger) *Handler

NewHandler creates a new gateway handler.

func NewHandlerWithAuth

func NewHandlerWithAuth(gateway *Gateway, logger *zap.Logger, jwtService *auth.JWTService) *Handler

NewHandlerWithAuth creates a new gateway handler with JWT authentication.

func (*Handler) CloseConnection

func (h *Handler) CloseConnection(c echo.Context) error

CloseConnection closes a connection. @Summary Close gateway connection @Tags gateway @Param id path string true "Connection ID" @Success 200 {object} map[string]string @Failure 404 {object} map[string]string @Router /api/v1/gateway/connections/{id} [delete]

func (*Handler) GetConnection

func (h *Handler) GetConnection(c echo.Context) error

GetConnection returns a connection by ID. @Summary Get gateway connection @Tags gateway @Produce json @Param id path string true "Connection ID" @Success 200 {object} map[string]interface{} @Failure 404 {object} map[string]string @Router /api/v1/gateway/connections/{id} [get]

func (*Handler) ListConnections

func (h *Handler) ListConnections(c echo.Context) error

ListConnections returns all connections. @Summary List gateway connections @Tags gateway @Produce json @Success 200 {array} map[string]interface{} @Router /api/v1/gateway/connections [get]

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(e *echo.Echo, g *echo.Group)

RegisterRoutes registers gateway routes.

func (*Handler) Status

func (h *Handler) Status(c echo.Context) error

Status returns gateway status. @Summary Get gateway status @Tags gateway @Produce json @Success 200 {object} map[string]interface{} @Router /api/v1/gateway/status [get]

func (*Handler) WebSocket

func (h *Handler) WebSocket(c echo.Context) error

WebSocket handles WebSocket upgrade requests. Security: Validates JWT token from query parameter or Authorization header. @Summary WebSocket gateway @Tags gateway @Param token query string false "JWT token for authentication" @Router /ws [get]

type Message

type Message struct {
	ID        string          `json:"id"`
	Type      MessageType     `json:"type"`
	Method    string          `json:"method,omitempty"`
	Payload   json.RawMessage `json:"payload,omitempty"`
	Error     *ErrorPayload   `json:"error,omitempty"`
	Timestamp int64           `json:"timestamp"`
}

Message represents a gateway message.

type MessageType

type MessageType string

MessageType represents the type of gateway message.

const (
	// TypeRequest is a request message.
	TypeRequest MessageType = "request"
	// TypeResponse is a response message.
	TypeResponse MessageType = "response"
	// TypeEvent is an event message.
	TypeEvent MessageType = "event"
	// TypeError is an error message.
	TypeError MessageType = "error"
)

type RequestHandler

type RequestHandler func(ctx context.Context, conn *Connection, msg *Message) (*Message, error)

RequestHandler handles gateway requests.

Jump to

Keyboard shortcuts

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