config

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package config 提供了加载配置项内容的各类方法

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Classic added in v0.34.0

func Classic(logConfigFile, configFile string) (*web.Server, error)

Classic 返回一个开箱即用的 Server 实例

func LoadFile added in v0.16.1

func LoadFile(path string, v interface{}) error

LoadFile 加载配置文件内容至 v

支持 yaml、xml 和 json 格式内容,以后缀名作为判断依据。

func LoadJSON added in v0.32.0

func LoadJSON(path string, v interface{}) error

LoadJSON 加载 JSON 的配置文件并转换成 v 对象的内容

func LoadXML added in v0.32.0

func LoadXML(path string, v interface{}) error

LoadXML 加载 XML 的配置文件并转换成 v 对象的内容

func LoadYAML added in v0.32.0

func LoadYAML(path string, v interface{}) error

LoadYAML 加载 YAML 的配置文件并转换成 v 对象的内容

Types

type Certificate added in v0.34.0

type Certificate struct {
	Cert string `yaml:"cert,omitempty" json:"cert,omitempty" xml:"cert,omitempty"`
	Key  string `yaml:"key,omitempty" json:"key,omitempty" xml:"key,omitempty"`
}

Certificate 证书管理

type Config added in v0.32.0

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

Config 管理配置项的加载和刷新

func New added in v0.32.0

func New(config string, v interface{}, f UnmarshalFunc, notify func()) *Config

New 注册配置项

config 表示配置项的配置内容,比如文件名、SQL 的 DSN 等; v 配置项导出对象的实例指针,最终 config 指向的数据会被解析到 v 中; f 从 config 导入到 v 的实现方法; notify 如果后期需要更新数据,则通过此函数通知用户数据已经被更新;

func (*Config) Refresh added in v0.32.0

func (cfg *Config) Refresh() error

Refresh 刷新指定配置项

type Duration added in v0.34.0

type Duration time.Duration

Duration 封装 time.Duration 以实现对 JSON、XML 和 YAML 的解析

func (Duration) Duration added in v0.34.0

func (d Duration) Duration() time.Duration

Duration 转换成 time.Duration

func (Duration) MarshalJSON added in v0.34.0

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON json.Marshaler 接口

func (Duration) MarshalXML added in v0.34.0

func (d Duration) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML xml.Marshaler 接口

func (Duration) MarshalXMLAttr added in v0.34.0

func (d Duration) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr xml.MarshalerAttr

func (Duration) MarshalYAML added in v0.34.0

func (d Duration) MarshalYAML() (interface{}, error)

MarshalYAML yaml.Marshaler 接口

func (*Duration) UnmarshalJSON added in v0.34.0

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON json.Unmarshaler 接口

func (*Duration) UnmarshalXML added in v0.34.0

func (d *Duration) UnmarshalXML(de *xml.Decoder, start xml.StartElement) error

UnmarshalXML xml.Unmarshaler 接口

func (*Duration) UnmarshalXMLAttr added in v0.34.0

func (d *Duration) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr xml.UnmarshalerAttr

func (*Duration) UnmarshalYAML added in v0.34.0

func (d *Duration) UnmarshalYAML(u func(interface{}) error) error

UnmarshalYAML yaml.Unmarshaler 接口

type ErrorHandler added in v0.34.0

type ErrorHandler struct {
	Status  []int
	Handler ErrorHandlerFunc
}

ErrorHandler 错误处理的配置

type ErrorHandlerFunc added in v0.34.0

type ErrorHandlerFunc = errorhandler.HandleFunc

ErrorHandlerFunc 错误状态码对应的处理函数原型

type FieldError added in v0.32.0

type FieldError struct {
	Field, Message string
}

FieldError 表示配置内容字段错误

func (*FieldError) Error added in v0.32.0

func (err *FieldError) Error() string

type HTTP added in v0.34.0

type HTTP struct {
	// 网站的域名证书
	//
	// 该设置并不总是生效的,具体的说明可参考 TLSConfig 字段的说明。
	Certificates []*Certificate `yaml:"certificates,omitempty" json:"certificates,omitempty" xml:"certificates,omitempty"`

	// 应用于 http.Server 的几个变量
	ReadTimeout       Duration `yaml:"readTimeout,omitempty" json:"readTimeout,omitempty" xml:"readTimeout,attr,omitempty"`
	WriteTimeout      Duration `yaml:"writeTimeout,omitempty" json:"writeTimeout,omitempty" xml:"writeTimeout,attr,omitempty"`
	IdleTimeout       Duration `yaml:"idleTimeout,omitempty" json:"idleTimeout,omitempty" xml:"idleTimeout,attr,omitempty"`
	ReadHeaderTimeout Duration `yaml:"readHeaderTimeout,omitempty" json:"readHeaderTimeout,omitempty" xml:"readHeaderTimeout,attr,omitempty"`
	MaxHeaderBytes    int      `yaml:"maxHeaderBytes,omitempty" json:"maxHeaderBytes,omitempty" xml:"maxHeaderBytes,attr,omitempty"`

	// 指定 https 模式下的证书配置项
	//
	// 如果用户指定了 Certificates 字段,则会根据此字段生成,
	// 用户也可以自已覆盖此值,比如采用 golang.org/x/crypto/acme/autocert.Manager.TLSConfig
	// 配置 Let's Encrypt。
	TLSConfig *tls.Config `yaml:"-" json:"-" xml:"-"`
}

HTTP 与 http 请求相关的设置

type Locale added in v0.34.0

type Locale struct {
	Key message.Reference
	// contains filtered or unexported fields
}

Locale 用于描述本地化信息

type Middleware added in v0.34.0

type Middleware = middleware.Middleware

Middleware 中间件的类型定义

type Router added in v0.34.0

type Router struct {
	// 是否禁用自动生成 OPTIONS 和 HEAD 请求的处理
	DisableOptions bool `yaml:"disableOptions,omitempty" json:"disableOptions,omitempty" xml:"disableOptions,attr,omitempty"`
	DisableHead    bool `yaml:"disableHead,omitempty" json:"disableHead,omitempty" xml:"disableHead,attr,omitempty"`
	SkipCleanPath  bool `yaml:"skipCleanPath,omitempty" json:"skipCleanPath,omitempty" xml:"skipCleanPath,attr,omitempty"`

	// 调试相关的路由设置项
	Pprof string `yaml:"pprof,omitempty" json:"pprof,omitempty" xml:"pprof,omitempty"`
	Vars  string `yaml:"vars,omitempty" json:"vars,omitempty" xml:"vars,omitempty"`
}

Router 路由的相关配置

type UnmarshalFunc

type UnmarshalFunc func(config string, v interface{}) error

UnmarshalFunc 定义了配置项的解码函数原型

config 为配置项的配置内容,可以是一个文件名或是数据库的 DSN 等, UnmarshalFunc 负责将其指向的内容解析并映身到 v 对象。

type Web added in v0.34.0

type Web struct {
	XMLName struct{} `yaml:"-" json:"-" xml:"web"`

	// 网站的根目录所在
	//
	// 比如 https://example.com/api/
	Root string `yaml:"root,omitempty" json:"root,omitempty" xml:"root,omitempty"`

	// 与路由设置相关的配置项
	Router *Router `yaml:"router,omitempty" json:"router,omitempty" xml:"router,omitempty"`

	// 与 HTTP 请求相关的设置项
	HTTP *HTTP `yaml:"http,omitempty" json:"http,omitempty" xml:"http,omitempty"`

	// 指定插件的搜索方式
	//
	// 通过 glob 语法搜索插件,比如:
	//  ~/plugins/*.so
	// 具体可参考:https://golang.org/pkg/path/filepath/#Glob
	// 为空表示没有插件。
	//
	// 当前仅支持部分系统,具体可查看:https://golang.org/pkg/plugin/
	Plugins string `yaml:"plugins,omitempty" json:"plugins,omitempty" xml:"plugins,omitempty"`

	// 指定关闭服务时的超时时间
	//
	// 如果此值不为 0,则在关闭服务时会调用 http.Server.Shutdown 函数等待关闭服务,
	// 否则直接采用 http.Server.Close 立即关闭服务。
	ShutdownTimeout Duration `yaml:"shutdownTimeout,omitempty" json:"shutdownTimeout,omitempty" xml:"shutdownTimeout,omitempty"`

	// 时区名称
	//
	// 可以是 Asia/Shanghai 等,具体可参考:
	// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
	//
	// 为空和 Local(注意大小写) 值都会被初始化本地时间。
	Timezone string `yaml:"timezone,omitempty" json:"timezone,omitempty" xml:"timezone,omitempty"`

	// Cache 指定缓存对象
	//
	// 可看查看 github.com/issue9/cache 中相关的实现,
	// 用户也可以自己实现 github.com/issue9/cache.Cache 接口。
	//
	// 如果用户未指定,则会采用以下方式提供默认值:
	//  github.com/issue9/cache/memory.New(24 * time.Hour)
	Cache cache.Cache `yaml:"-" json:"-" xml:"-"`

	// 本地化消息的管理组件
	//
	// 为空的情况下会引用 golang.org/x/text/message.DefaultCatalog 对象。
	//
	// golang.org/x/text/message/catalog 提供了 NewBuilder 和 NewFromMap
	// 等方式构建 Catalog 接口实例。
	Catalog catalog.Catalog `yaml:"-" json:"-" xml:"-"`

	// 指定中间件
	//
	// Middlewares 和 Filters 都表示中间件,两者的功能没有本质上的差别。
	// 之所以提供了两个类型,是因为 Middlewares 兼容 http.Handler 类型,
	// 可以对市面上大部分的中间件稍加改造即可使用,而 Filter
	// 则提供了部分 http.Handler 不存在的数据字段,且两者不能交替出现,
	// 干脆同时提供两种中间件。
	//
	// 在使用上,永远是 Middlewares 在 Filters 之前调用。
	Middlewares []Middleware `yaml:"-" json:"-" xml:"-"`
	Filters     []web.Filter `yaml:"-" json:"-" xml:"-"`

	// 指定各类媒体类型的编解码函数
	Marshalers   map[string]content.MarshalFunc   `yaml:"-" json:"-" xml:"-"`
	Unmarshalers map[string]content.UnmarshalFunc `yaml:"-" json:"-" xml:"-"`

	// 指定生成 Result 的方法
	//
	// 可以为空,表示采用 CTXServer 的默认值。
	ResultBuilder result.BuildFunc `yaml:"-" json:"-" xml:"-"`

	// 返回给用户的错误提示信息
	//
	// 对键名作了一定的要求:要求最高的三位数必须是一个 HTTP 状态码,
	// 比如 40001,在返回给客户端时,会将 400 作为状态码展示给用户,
	// 同时又会将 40001 和对应的消息发送给用户。
	//
	// 该数据最终由 web.Server.AddMessage 添加。
	Results map[int]Locale `yaml:"-" json:"-" xml:"-"`

	// 指定错误页面的处理方式
	ErrorHandlers []*ErrorHandler `yaml:"-" json:"-" xml:"-"`

	// 指定用于触发关闭服务的信号
	//
	// 如果为 nil,表示未指定任何信息,如果是长度为 0 的数组,则表示任意信号,
	// 如果指定了多个相同的值,则该信号有可能多次触发。
	ShutdownSignal []os.Signal `yaml:"-" json:"-" xml:"-"`
	// contains filtered or unexported fields
}

Web 提供了初始化 Server 对象的基本参数

func (*Web) NewServer added in v0.34.0

func (conf *Web) NewServer(l *logs.Logs) (*web.Server, error)

NewServer 返回 Server 对象

Jump to

Keyboard shortcuts

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