api

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachLocalhostClient

func AttachLocalhostClient(ctx context.Context, deviceExportMeta *usbip.ExportMeta, usbipServerPort uint16, useNativeIOCTL bool, logger *slog.Logger) error

func CheckAutoAttachPrerequisites

func CheckAutoAttachPrerequisites(_ bool, logger *slog.Logger) bool

CheckAutoAttachPrerequisites checks if auto-attach prerequisites are met on Linux. Returns true if all requirements are satisfied, false otherwise with helpful log messages.

func ErrBadRequest

func ErrBadRequest(detail string) *apitypes.ApiError

Factory helpers returning *apitypes.ApiError (single canonical error type).

func ErrConflict

func ErrConflict(detail string) *apitypes.ApiError

func ErrInternal

func ErrInternal(detail string) *apitypes.ApiError

func ErrNotFound

func ErrNotFound(detail string) *apitypes.ApiError

func ListDeviceTypes added in v0.3.2

func ListDeviceTypes() []string

ListDeviceTypes returns a list of all registered device type names.

func RegisterDevice

func RegisterDevice(name string, reg DeviceRegistration)

RegisterDevice registers a device type for dynamic creation and handler dispatch. This should be called from device package init() functions. The name is case-insensitive and will be lowercased.

func WrapError

func WrapError(err error) *apitypes.ApiError

WrapError normalizes any error into *apitypes.ApiError.

Types

type DeviceRegistration

type DeviceRegistration interface {
	// CreateDevice returns a new device instance of this type.
	CreateDevice(o *device.CreateOptions) usb.Device
	// StreamHandler returns the handler function for long-lived connections.
	StreamHandler() StreamHandlerFunc
}

DeviceRegistration describes a device type, providing both device creation and stream handler registration.

func GetRegistration

func GetRegistration(name string) DeviceRegistration

GetRegistration retrieves a registered device handler by name for device creation. Returns nil if not found. Name lookup is case-insensitive.

type HandlerFunc

type HandlerFunc func(req *Request, res *Response, logger *slog.Logger) error

HandlerFunc processes a request and populates the response. Returns an error on failure. The logger provided is a connection-scoped logger enriched with remote address metadata by the API server.

type Request

type Request struct {
	Ctx     context.Context
	Params  map[string]string
	Payload string
}

Request contains route parameters and additional args from the command.

type Response

type Response struct {
	JSON string
}

Response holds the JSON string to return to the client.

type Router

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

Router implements simple path pattern matching with placeholders in {name}.

func NewRouter

func NewRouter() *Router

NewRouter returns a new Router instance.

func (*Router) Match

func (r *Router) Match(path string) (HandlerFunc, map[string]string)

Match returns the HandlerFunc and params if the given path matches any registered pattern. Returns nil if none match.

func (*Router) MatchStream

func (r *Router) MatchStream(path string) (StreamHandlerFunc, map[string]string)

MatchStream returns the StreamHandler and params if the given path matches any registered stream pattern. Returns nil if none match.

func (*Router) Register

func (r *Router) Register(pattern string, handler HandlerFunc)

Register registers a handler for a path pattern like "bus/{id}/list".

func (*Router) RegisterStream

func (r *Router) RegisterStream(pattern string, handler StreamHandlerFunc)

RegisterStream registers a StreamHandler for long-lived TCP connections.

type Server

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

Server implements a small TCP API for managing virtual bus topology.

func New

func New(s *usb.Server, addr string, config ServerConfig, logger *slog.Logger) *Server

New creates a new ApiServer bound to a server.Server instance.

func (*Server) Addr added in v0.3.2

func (a *Server) Addr() string

Addr returns the actual address the server is listening on. If Start hasn't been called yet, it returns the configured address.

func (*Server) Close

func (a *Server) Close()

Close stops the API server.

func (*Server) Config

func (a *Server) Config() ServerConfig

Config returns the server configuration.

func (*Server) Router

func (a *Server) Router() *Router

Router returns the router used by the API server so callers can register handlers.

func (*Server) Start

func (a *Server) Start() error

Start listens on the configured address and serves incoming API commands.

func (*Server) USB

func (a *Server) USB() *usb.Server

USB returns the underlying USB server.

type ServerConfig

type ServerConfig struct {
	Addr                        string        `help:"API server listen address" default:":3242" env:"VIIPER_API_ADDR"`
	DeviceHandlerConnectTimeout time.Duration `` /* 136-byte string literal not displayed */
	AutoAttachLocalClient       bool          `` /* 146-byte string literal not displayed */
	ConnectionTimeout           time.Duration `kong:"-"`
	// contains filtered or unexported fields
}

ServerConfig represents the server subcommand configuration.

type StreamHandlerFunc

type StreamHandlerFunc func(conn net.Conn, dev *usb.Device, logger *slog.Logger) error

StreamHandlerFunc handles long-lived TCP connections for bidirectional streaming. The handler takes ownership of the connection and should close it when done. The logger provided is connection-scoped. Returning a non-nil error indicates the handler encountered a terminal failure; the dispatcher/server will log it.

func DeviceStreamHandler

func DeviceStreamHandler(srv *usb.Server) StreamHandlerFunc

DeviceStreamHandler returns a stream handler func that dynamically dispatches to device-specific handlers based on device type.

func GetStreamHandler

func GetStreamHandler(name string) StreamHandlerFunc

GetStreamHandler retrieves the stream handler for a registered device type. Returns nil if not found. Name lookup is case-insensitive.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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