server

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Overview

Package server 是 Cloudpath 中心服务:chi REST + WS hub(edge 接入 / 浏览器 fan-out) + SQLite 持久化 + 内嵌 webui。

并发不变量(改代码前先读):

  1. s.mu 只保护内存态(devices/edges/browsers/限流器),**锁内绝不做磁盘 I/O**; 需要落库时在锁内收集数据、锁外调用 persistXxx。
  2. broadcast 只读 browsers 集合,慢消费者丢帧不阻塞(历史可从 REST 补)。
  3. Store 可为 nil(API-only 模式):所有落库路径必须先判空。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Store    *store.Store
	Token    string // 共享令牌;空 = 无鉴权(仅限本机/内网)
	Version  string
	WebUIDir string // 开发模式:从磁盘目录服务前端(优先于内嵌)
	// RetentionDays 是事件/命令保留天数,超期由 sweeper 清理。<=0 用默认 30 天。
	RetentionDays int
	// CmdRatePerMin 是单设备每分钟命令下发上限(防跑飞的 UI/脚本刷串口)。<=0 用默认 20。
	CmdRatePerMin int
	// AllowedOrigins 是 WS 握手允许的浏览器 Origin 模式(如 "console.example.com"、
	// "*.example.com:8443")。留空 = 开发策略:同源 + localhost/127.0.0.1 任意端口。
	// 公网部署必须显式配置(非浏览器客户端不带 Origin,不受影响)。
	AllowedOrigins []string
	// RequireAuth 在无用户时也强制读/写鉴权(L2 公网形态,配合服务令牌)。
	RequireAuth bool
	// LoginRatePerMin 是单 IP 每分钟登录尝试上限。<=0 用默认 5。
	LoginRatePerMin int
	// SessionDays 是服务端会话 TTL(天)。<=0 用默认 7。
	SessionDays int
	// SetupToken 是一次性首装令牌:非本机直连来源执行 /api/auth/setup 时必须携带
	// (同机反代转发的请求也算非本机直连,见 setupFromLocalClient);
	// 首次成功 setup 后进程内失效。空 = 仅允许本机直连 setup。
	SetupToken string
	// TrustedProxies 是可信反代 CIDR allowlist(已解析);nil = 不采信任何转发头。
	TrustedProxies *auth.TrustedProxies
	// PluginCatalog 是只读插件目录(可选)。nil 时默认用 Server 自己的插件控制面
	// 投影构造(真实数据源),因此插件端点永不返回内置/静态样例。
	PluginCatalog plugincatalog.Catalog
	// PluginStore 是插件控制面持久化端口(internal/server/storeport,§3 契约)。
	// nil = 未接线:插件写 API 返回 503,WS plugin_* 消息按旧协议忽略,读面为空。
	// 生产必须由 SQLite 适配器注入(见 storeport/adapter_sqlite.go)。
	PluginStore storeport.PluginStore
	// PluginStaleAfter 是 observed 投影过期阈值;<=0 用默认 2 分钟。
	PluginStaleAfter time.Duration
}

Config 是服务配置。

type Server

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

Server 持有全部运行态。

func New

func New(cfg Config) *Server

New 创建服务并从数据库水合上次已知状态(重启后面板不空白,离线标记)。

func (*Server) CloseAll

func (s *Server) CloseAll()

CloseAll 断开全部 WS 客户端(优雅停机用,避免 Shutdown 等长连接挂死)。 用 cancel 而非 close(chan):与 broadcast 的并发发送零竞态。

func (*Server) PluginControlPlaneWired

func (s *Server) PluginControlPlaneWired() bool

PluginControlPlaneWired 报告插件控制面持久化是否已接线(启动日志/健康自检用)。

func (*Server) Routes

func (s *Server) Routes() http.Handler

Routes 组装 chi 路由树。

func (*Server) RunSweeper

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

RunSweeper 后台维护协程:命令超时标记(30s)+ 保留期清理(1h)。ctx 取消即退出。

Directories

Path Synopsis
Package storeadapter 是 Captain 在 STORE / SERVER 两条 lane 合并后的唯一接线点: 把 *store.Store 接到 internal/server/storeport.PluginStore 端口。
Package storeadapter 是 Captain 在 STORE / SERVER 两条 lane 合并后的唯一接线点: 把 *store.Store 接到 internal/server/storeport.PluginStore 端口。
Package storeport 是 Server lane 对插件控制面持久化的本地端口(依赖倒置接缝)。
Package storeport 是 Server lane 对插件控制面持久化的本地端口(依赖倒置接缝)。

Jump to

Keyboard shortcuts

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