web

package
v0.0.0-...-1f1a93d Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WebSocketUpgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {

		return true
	},
}

WebSocketUpgrader handles the WebSocket upgrade

Functions

func BroadcastToSession

func BroadcastToSession(sessionMgr *SessionManager, sessionID string, role, content string) error

BroadcastToSession sends a message to a specific session using the new protocol.

func HandleWebSocket

func HandleWebSocket(session *Session, sessionMgr *SessionManager, messageChan chan<- IncomingMessage, authToken string) error

HandleWebSocket handles a WebSocket connection with thread-safe sending

func IsNewProtocol

func IsNewProtocol(raw []byte) bool

IsNewProtocol checks whether raw JSON uses the new three-level format by looking for the presence of a non-empty "module" field.

func StaticFiles

func StaticFiles() (fs.FS, error)

StaticFiles returns the static files filesystem. Returns an error if the static directory is not properly embedded.

Types

type CORSConfig

type CORSConfig struct {
	AllowedOrigins    []string `json:"allowed_origins"`
	AllowedMethods    []string `json:"allowed_methods"`
	AllowedHeaders    []string `json:"allowed_headers"`
	AllowCredentials  bool     `json:"allow_credentials"`
	MaxAge            int      `json:"max_age"`
	AllowLocalhost    bool     `json:"allow_localhost"`
	DevelopmentMode   bool     `json:"development_mode"`
	AllowedCDNDomains []string `json:"allowed_cdn_domains"`
	AllowNoOrigin     bool     `json:"allow_no_origin"` // Allow requests without Origin header (e.g., mobile apps, curl)
}

CORSConfig represents CORS configuration

type CORSManager

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

CORSManager manages CORS configuration and validation

func NewCORSManager

func NewCORSManager(configPath string) (*CORSManager, error)

NewCORSManager creates a new CORS manager

func (*CORSManager) AddOrigin

func (m *CORSManager) AddOrigin(origin string) error

AddOrigin adds an allowed origin

func (*CORSManager) CheckCORS

func (m *CORSManager) CheckCORS(r *http.Request) bool

CheckCORS validates if the request origin is allowed

func (*CORSManager) Close

func (m *CORSManager) Close() error

Close stops the CORS manager

func (*CORSManager) ListOrigins

func (m *CORSManager) ListOrigins() []string

ListOrigins returns all allowed origins

func (*CORSManager) RemoveOrigin

func (m *CORSManager) RemoveOrigin(origin string) error

RemoveOrigin removes an allowed origin

func (*CORSManager) SetDevelopmentMode

func (m *CORSManager) SetDevelopmentMode(enabled bool) error

SetDevelopmentMode enables or disables development mode

type Event

type Event struct {
	Type string      // Event type: log, status, security-alert, scanner-progress, cluster-event, heartbeat
	Data interface{} // Event payload
}

Event represents a server-sent event

type EventHub

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

EventHub manages SSE subscribers and broadcasts events

func NewEventHub

func NewEventHub() *EventHub

NewEventHub creates a new EventHub

func (*EventHub) Publish

func (h *EventHub) Publish(eventType string, data interface{})

Publish sends an event to all subscribers

func (*EventHub) Subscribe

func (h *EventHub) Subscribe() chan Event

Subscribe creates and returns a new event channel

func (*EventHub) SubscriberCount

func (h *EventHub) SubscriberCount() int

SubscriberCount returns the number of active subscribers

func (*EventHub) Unsubscribe

func (h *EventHub) Unsubscribe(ch chan Event)

Unsubscribe removes a subscriber channel

type HistoryMessage

type HistoryMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

HistoryMessage represents a single message in chat history.

type HistoryPage

type HistoryPage struct {
	Messages    []HistoryMessage `json:"messages"`
	HasMore     bool             `json:"has_more"`
	OldestIndex int              `json:"oldest_index"`
	TotalCount  int              `json:"total_count"`
}

HistoryPage represents a page of history messages returned to the client.

type HistoryRequestData

type HistoryRequestData struct {
	RequestID   string `json:"request_id"`
	Limit       int    `json:"limit,omitempty"`
	BeforeIndex *int   `json:"before_index,omitempty"`
}

HistoryRequestData represents the data payload of a history_request message.

type IncomingMessage

type IncomingMessage struct {
	SessionID string
	SenderID  string
	ChatID    string
	Content   string
	Timestamp time.Time
	Metadata  map[string]string // Additional metadata (e.g. request_type)
}

IncomingMessage represents a message received from a WebSocket client

type ProtocolMessage

type ProtocolMessage struct {
	Type      string          `json:"type"`
	Module    string          `json:"module"`
	Cmd       string          `json:"cmd"`
	Data      json.RawMessage `json:"data,omitempty"`
	Timestamp string          `json:"timestamp,omitempty"`
}

ProtocolMessage represents a three-level dispatch protocol message. Format: type → module → cmd, with data and timestamp.

func NewProtocolMessage

func NewProtocolMessage(typeName, module, cmd string, data interface{}) (*ProtocolMessage, error)

NewProtocolMessage constructs a new three-level protocol message.

func ParseProtocolMessage

func ParseProtocolMessage(raw []byte) (*ProtocolMessage, error)

ParseProtocolMessage parses raw JSON into a ProtocolMessage.

func (*ProtocolMessage) DecodeData

func (m *ProtocolMessage) DecodeData(v interface{}) error

DecodeData decodes the Data field into the provided value.

func (*ProtocolMessage) ToJSON

func (m *ProtocolMessage) ToJSON() ([]byte, error)

ToJSON serializes the message to JSON bytes.

type Server

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

Server represents the HTTP server for the web channel

func NewServer

func NewServer(config ServerConfig) *Server

NewServer creates a new HTTP server

func (*Server) GetEventHub

func (s *Server) GetEventHub() *EventHub

GetEventHub returns the SSE event hub for external integration

func (*Server) GetSessionManager

func (s *Server) GetSessionManager() *SessionManager

GetSessionManager returns the session manager

func (*Server) HandleAPIConfigForTest

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

func (*Server) HandleAPILogsForTest

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

func (*Server) HandleAPIScannerStatusForTest

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

func (*Server) HandleAPIStatusForTest

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

Expose handlers for testing

func (*Server) HandleEventsStreamForTest

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

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns whether the server is running

func (*Server) PublishStatusLoopForTest

func (s *Server) PublishStatusLoopForTest(ctx context.Context)

func (*Server) SendHistoryToSession

func (s *Server) SendHistoryToSession(sessionID, jsonContent string) error

SendHistoryToSession sends a history response to a specific session.

func (*Server) SendToSession

func (s *Server) SendToSession(sessionID, role, content string) error

SendToSession sends a message to a specific session

func (*Server) SetModelName

func (s *Server) SetModelName(name string)

SetModelName sets the current LLM model name for status reporting

func (*Server) SetWorkspace

func (s *Server) SetWorkspace(ws string)

SetWorkspace sets the workspace path for API handlers

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the HTTP server

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop stops the HTTP server

type ServerConfig

type ServerConfig struct {
	Host       string
	Port       int
	WSPath     string
	AuthToken  string
	SessionMgr *SessionManager
	Bus        *bus.MessageBus
	Workspace  string // Workspace path for config files
	Version    string // Application version
}

ServerConfig holds the server configuration

type Session

type Session struct {
	ID         string
	Conn       *websocket.Conn
	SenderID   string
	ChatID     string
	CreatedAt  time.Time
	LastActive time.Time
	// contains filtered or unexported fields
}

Session represents an active WebSocket client session

type SessionManager

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

SessionManager manages all active WebSocket sessions

func NewSessionManager

func NewSessionManager(timeout time.Duration) *SessionManager

NewSessionManager creates a new session manager

func (*SessionManager) Broadcast

func (sm *SessionManager) Broadcast(sessionID string, message []byte) error

Broadcast sends a message to a specific session

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(conn *websocket.Conn) *Session

CreateSession creates a new session with a unique ID

func (*SessionManager) GetActiveCount

func (sm *SessionManager) GetActiveCount() int

GetActiveCount returns the number of active sessions

func (*SessionManager) GetAllSessions

func (sm *SessionManager) GetAllSessions() []*Session

GetAllSessions returns all active sessions

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(sessionID string) (*Session, bool)

GetSession retrieves a session by ID

func (*SessionManager) RemoveSession

func (sm *SessionManager) RemoveSession(sessionID string)

RemoveSession removes a session

func (*SessionManager) Shutdown

func (sm *SessionManager) Shutdown()

Shutdown closes all sessions and stops cleanup

func (*SessionManager) Stats

func (sm *SessionManager) Stats() map[string]interface{}

Stats returns statistics about active sessions

Jump to

Keyboard shortcuts

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