server

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package server provides an HTTP API for the iterion editor. It wraps the parser, compiler, and unparser to provide endpoints for parsing .iter files, validating workflows, and generating .iter text.

Index

Constants

View Source
const (
	EventFileCreated  = "file_created"
	EventFileModified = "file_modified"
	EventFileDeleted  = "file_deleted"
)

Variables

This section is empty.

Functions

func SetWebSocketOriginCheck

func SetWebSocketOriginCheck(fn func(origin string) bool)

SetWebSocketOriginCheck registers the origin allowlist function used by the WebSocket upgrader. Called by Server during routes() setup.

Types

type Config

type Config struct {
	Port        int    // HTTP port (default 4891)
	Bind        string // bind address (default "127.0.0.1"; use "0.0.0.0" only with explicit user opt-in)
	ExamplesDir string // path to examples directory
	WorkDir     string // root directory for file operations
	StoreDir    string // run store directory (default: <WorkDir>/.iterion)
	OpenBrowser bool   // open browser on start
}

Config holds the server configuration.

type DiagnosticDTO

type DiagnosticDTO struct {
	Code     string `json:"code,omitempty"`
	Severity string `json:"severity"`
	Message  string `json:"message"`
	NodeID   string `json:"node_id,omitempty"`
	EdgeID   string `json:"edge_id,omitempty"`
	Hint     string `json:"hint,omitempty"`
}

DiagnosticDTO is the wire-safe shape of an ir.Diagnostic. It carries the structured fields (code, severity, attribution, hint) so the editor can render inline badges without resorting to string-matching the message.

type FileEvent

type FileEvent struct {
	Type string `json:"type"` // file_created, file_modified, file_deleted
	Path string `json:"path"` // relative to WorkDir
}

FileEvent is the JSON message sent to WebSocket clients when a file changes.

type Hub

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

Hub maintains the set of active WebSocket clients and broadcasts messages to them.

func NewHub

func NewHub(logger *iterlog.Logger) *Hub

NewHub creates a new Hub.

func (*Hub) Broadcast

func (h *Hub) Broadcast(event FileEvent)

Broadcast marshals a FileEvent to JSON and sends it to all connected clients.

func (*Hub) HandleWebSocket

func (h *Hub) HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket upgrades an HTTP connection to WebSocket and registers the client.

Shutdown safety: if the hub has already been Stop()'d (h.done closed), the Run() goroutine has exited and `h.register` has no receiver — a naive `h.register <- c` here would block the HTTP handler goroutine forever and leak its conn. We short-circuit on that and refuse the upgrade. We also guard the register send itself with a select-on-done in case Stop fires concurrently with this handler.

func (*Hub) Run

func (h *Hub) Run()

Run processes register, unregister, and broadcast events. Call as a goroutine.

func (*Hub) Stop

func (h *Hub) Stop()

Stop signals the hub to shut down.

type Server

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

Server is the editor HTTP server.

func New

func New(cfg Config, logger *iterlog.Logger) *Server

New creates a new editor server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts the HTTP server.

func (*Server) Shutdown

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

Shutdown gracefully shuts down the server.

Order matters: HTTP-level shutdown (Server.Shutdown) drains in-flight requests, while the run console service drains in-process workflow goroutines. We do the workflow drain first so any cancel events reach the on-disk store before the file watcher stops broadcasting and clients drop. The drain ctx is the caller-supplied shutdown deadline.

Drain (rather than Stop) is intentional: it flips each in-flight run to failed_resumable and emits EventRunInterrupted so the next boot can offer one-click resume and clients can distinguish shutdown-induced termination from user-initiated cancel.

type Watcher

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

Watcher watches WorkDir for .iter file changes and pushes events to a Hub.

func NewWatcher

func NewWatcher(workDir string, hub *Hub, logger *iterlog.Logger) (*Watcher, error)

NewWatcher creates a new file watcher for the given directory.

func (*Watcher) IgnorePath

func (w *Watcher) IgnorePath(absPath string)

IgnorePath marks a path to be ignored for the next ignoreWindow.

func (*Watcher) Start

func (w *Watcher) Start()

Start walks the work directory, adds watches, and runs the event loop.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop shuts down the watcher.

Jump to

Keyboard shortcuts

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