config

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

users.go 是 serve.users 用户表的语义校验与生效前缀计算。

校验覆盖 MERC-11 冻结契约的不变量 I4(互斥,由 cmd 层在合并后调用)、 I6(生效前缀互不嵌套/不相等)、密码非空与 quota 语法;热加载的 reload 校验复用同一入口,保证启动与 reload 语义一致。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigPath

func ConfigPath() (string, error)

ConfigPath 返回配置文件默认路径 ~/.config/sail/config.yaml。 刻意不读 $XDG_CONFIG_HOME:行为可预测优先(见 README「配置」)。

func EffectivePrefix added in v0.1.3

func EffectivePrefix(base, rel string) string

EffectivePrefix 计算用户的生效前缀:base(serve.prefix)拼接相对段 user.prefix,两侧均按 "/" 归一化。结果不含首尾 "/",空 base + 空段 = 桶根。

func EnvVarName added in v0.0.7

func EnvVarName(profile, field string) string

EnvVarName 按 profile 派生环境变量名:SAIL_<清洗后PROFILE>_<FIELD>。 profile 名大写后仅保留 [A-Z0-9],其余字符视为分隔符并压缩为单个 _(去首尾); 清洗后为空则回退为 SAIL_<FIELD>。结果只含 [A-Z0-9_],必可被 envVarPattern 展开。

func ParseQuota added in v0.1.3

func ParseQuota(raw string) (int64, error)

ParseQuota 解析配额字符串(如 "10GB"、"500MB"、"1024")为字节。 与 cmd.parseSize 不同,配额刻意只收 MB/GB/TB 三档单位(见 quotaUnits), 不接受二进制单位。空串不是合法配额——调用方以「非空才校验」表达 「省略 = 不限额」。

func ValidateUsers added in v0.1.3

func ValidateUsers(basePrefix string, users []UserConfig) error

ValidateUsers 校验用户表语义。basePrefix 是 serve.prefix 的归一化值, 所有前缀嵌套判定都在生效前缀(EffectivePrefix(basePrefix, u.Prefix))上做。 通过则返回 nil;任何失败都是 fail-loud,调用方必须拒绝启动/保留旧表。

Types

type Config

type Config struct {
	DefaultProfile string             `mapstructure:"default-profile"`
	Lang           string             `mapstructure:"lang"`
	Profiles       map[string]Profile `mapstructure:"profiles"`
}

Config 是 ~/.config/sail/config.yaml 的整体结构

func Load

func Load(path string) (*Config, error)

Load 从给定路径加载配置;path 为空则用默认路径。 返回未解析环境变量的原始 Config。

func (*Config) Resolve

func (c *Config) Resolve(profile string) (*Resolved, error)

Resolve 根据 profile 名解析出最终配置。优先级:环境变量 > 配置文件。 profile 为空时取 default-profile。

type Profile

type Profile struct {
	Endpoint  string `mapstructure:"endpoint"`
	AccessKey string `mapstructure:"access-key"`
	SecretKey string `mapstructure:"secret-key"`
	Bucket    string `mapstructure:"bucket"`
	Region    string `mapstructure:"region"`
	PathStyle bool   `mapstructure:"path-style"`
	CDNDomain string `mapstructure:"cdn-domain"`
	// CDNBucketPath 显式声明 cdn-domain 是否已含 bucket 路径。
	// nil = 自动检测;true = 已含(不再追加);false = 未含(总是追加)。
	CDNBucketPath *bool `mapstructure:"cdn-bucket-path"`
	// Serve 是 sail serve webdav 的部署参数。与连接参数同属一个 profile,
	// 由 serve 命令按「flag > serve.* > flag 默认」的优先级合并。
	Serve ServeConfig `mapstructure:"serve"`
}

Profile 是单个存储服务的配置档 (prod / test / internal)

type Resolved

type Resolved struct {
	ProfileName   string
	Endpoint      string
	AccessKey     string
	SecretKey     string
	Bucket        string
	Region        string
	PathStyle     bool
	CDNDomain     string
	CDNBucketPath *bool
	Serve         ServeConfig
}

Resolved 是最终生效的、供 client 使用的配置

type SMBConfig added in v0.1.5

type SMBConfig struct {
	// Listen 是 SMB 的监听地址。刻意不继承 serve.listen:两种协议各跑各的
	// 进程,把它们塞到同一个端口上只会更意外。
	Listen string `mapstructure:"listen"`
	// Share 是单用户模式下的共享名(默认 "sail")。
	Share string `mapstructure:"share"`
	// ServerName 是 NTLM 挑战里服务端自称的名字(默认 "SAIL")。
	ServerName string `mapstructure:"server-name"`
}

SMBConfig 是 serve.smb 块:`sail serve smb` 的专属参数落点。字段语义与 cmd/serve_smb.go 的同名 flag 一一对应。

type ServeConfig added in v0.1.1

type ServeConfig struct {
	Listen         string       `mapstructure:"listen"`
	Prefix         string       `mapstructure:"prefix"`
	User           string       `mapstructure:"user"`
	Password       string       `mapstructure:"password"`
	Users          []UserConfig `mapstructure:"users"`
	TLSCert        string       `mapstructure:"tls-cert"`
	TLSKey         string       `mapstructure:"tls-key"`
	StagingDir     string       `mapstructure:"staging-dir"`
	BackendMaxSize string       `mapstructure:"backend-max-object-size"`
	MaxUploadSize  string       `mapstructure:"max-upload-size"`
	ChunkedUpload  bool         `mapstructure:"chunked-upload"`
	ChunkSize      string       `mapstructure:"chunk-size"`
	// DirCacheTTL 是目录列表缓存时长(flag --dir-cache-ttl);空 = 用 flag
	// 默认(60s),"0" = 关闭缓存。
	DirCacheTTL string `mapstructure:"dir-cache-ttl"`
	// Prewarm 是后台保热的目录清单(flag --prewarm);空 = 不预热。
	// 多用户模式下同一清单会在每个用户自己的空间内生效。
	Prewarm []string `mapstructure:"prewarm"`
	// SMB 是 `serve smb` 独有的参数。协议无关的那些(prefix / users /
	// staging-dir / 上限 / 分片 / dir-cache-ttl / prewarm)两种协议共用上面
	// 同名字段 —— 同一个 bucket 的共享方式换协议时,用户表与空间划分不该跟着
	// 重写一遍;只有端口、共享名这类协议特有的才分家。
	SMB SMBConfig `mapstructure:"smb"`
}

ServeConfig 是 serve webdav 的全部 flag 参数的配置落点。字段语义与 cmd/serve.go 里的同名 flag 一一对应;大小类字段保持 flag 的字符串格式 (如 "5TiB"),合并后在 cmd 层统一 parseSize。

注意:新增 serve flag 时必须同步加到这里——config.Load 按结构体反序列化, 结构体之外的键会在 config setup 重写文件时被静默丢弃。

type UserConfig added in v0.1.3

type UserConfig struct {
	Name     string `mapstructure:"name"`
	Password string `mapstructure:"password"`
	// Prefix 是相对 serve.prefix 的空间段;省略 = base 前缀本身。
	Prefix string `mapstructure:"prefix"`
	// Quota 是空间配额(如 "10GB",支持 MB/GB/TB);省略 = 不限额。语法校验见 users.go,
	// 配额执行由 P2 的 quotafs 落地。
	Quota string `mapstructure:"quota"`
}

UserConfig 是 serve.users 用户表里的一名用户。字段支持 ${VAR} 环境变量 展开(与 serve 其余字段一致),密码可留环境变量、不落明文。

Jump to

Keyboard shortcuts

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