server

package
v0.86.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 35 Imported by: 2

Documentation

Overview

Package server 服务端实现

Index

Constants

View Source
const DefaultConfigDir = "@.config"

DefaultConfigDir 默认的配置目录地址

View Source
const RequestIDKey = header.RequestIDKey

RequestIDKey 报头中传递 request id 的报头名称

Variables

This section is empty.

Functions

func New

func New(name, version string, o *Options) (web.Server, error)

New 新建 http 服务

name, version 表示服务的名称和版本号; o 指定了一些带有默认值的参数;

Types

type Compression added in v0.86.0

type Compression struct {
	// Compressor 压缩算法
	Compressor web.Compressor

	// Types 该压缩对象允许使用的为 content-type 类型
	//
	// 如果是 * 或是空值表示适用所有类型。
	Types []string
}

Compression 有关压缩的设置项

func BestCompressionCompressions added in v0.86.0

func BestCompressionCompressions(contentType ...string) []*Compression

BestCompressionCompressions 提供当前框架内置的所有压缩算法

如果有性能参数,则选择最快压缩比作为初始化条件。

func BestSpeedCompressions added in v0.86.0

func BestSpeedCompressions(contentType ...string) []*Compression

BestSpeedCompressions 提供当前框架内置的所有压缩算法

如果有性能参数,则选择最快速度作为初始化条件。

func DefaultCompressions added in v0.86.0

func DefaultCompressions(contentType ...string) []*Compression

DefaultCompressions 提供当前框架内置的所有压缩算法

contentType 指定所有算法应用的媒体类型,为空则表示对所有的内容都进行压缩。

type Config added in v0.75.0

type Config struct {
	// Dir 项目配置目录
	//
	// 如果涉及到需要读取配置文件的,可以指定此对象,之后可通过此对象统一处理各类配置文件。
	// 如果为空,则会采用 [DefaultConfigDir]。
	Dir string

	// Serializers 支持的序列化方法列表
	//
	// 如果为空,则会默认支持 yaml、json 两种方式;
	Serializers []*FileSerializer
}

Config 项目配置文件的配置

type FileSerializer added in v0.86.0

type FileSerializer struct {
	// Exts 支持的扩展名
	Exts []string

	// Marshal 序列化方法
	Marshal config.MarshalFunc

	// Unmarshal 反序列化方法
	Unmarshal config.UnmarshalFunc
}

FileSerializer 对于文件序列化的配置

type IDGenerator added in v0.81.0

type IDGenerator = func() string

IDGenerator 生成唯一 ID 的函数

type Mimetype added in v0.70.0

type Mimetype struct {
	// Mimetype 的名称
	//
	// 比如:application/json
	Name string

	// 对应的错误状态下的 mimetype 值
	//
	// 比如:application/problem+json。
	// 可以为空,表示与 Type 相同。
	Problem string

	// 生成编码方法
	Marshal web.MarshalFunc

	// 解码方法
	Unmarshal web.UnmarshalFunc
}

Mimetype 有关 mimetype 的设置项

func APIMimetypes added in v0.86.0

func APIMimetypes() []*Mimetype

APIMimetypes 返回以 XML 和 JSON 作为数据交换格式的配置项

func JSONMimetypes added in v0.86.0

func JSONMimetypes() []*Mimetype

JSONMimetypes 返回以 JSON 作为数据交换格式的配置项

func XMLMimetypes added in v0.86.0

func XMLMimetypes() []*Mimetype

XMLMimetypes 返回以 XML 作为数据交换格式的配置项

type Options

type Options struct {
	// 项目的配置项
	Config *Config

	// 服务器的时区
	//
	// 默认值为 [time.Local]
	Location *time.Location

	// 缓存系统
	//
	// 内置的几种驱动实现位于 [github.com/issue9/web/cache/caches] 之下。
	// 如果为空,采用 [caches.NewMemory] 作为默认值。
	Cache cache.Driver

	// 日志的相关设置
	//
	// 如果此值为空,表示不会输出任何信息。
	Logs *logs.Options

	// http.Server 实例的值
	//
	// 可以为零值。
	HTTPServer *http.Server

	// 生成唯一字符串的方法
	//
	// 供 [Server.UniqueID] 使用。
	//
	// 如果为空,将采用 [unique.NewDate] 作为生成方法。
	IDGenerator IDGenerator

	// 路由选项
	RoutersOptions []web.RouterOption

	// 指定获取 x-request-id 内容的报头名
	//
	// 如果为空,则采用 [RequestIDKey] 作为默认值
	RequestIDKey string

	// 可用的压缩类型
	//
	// 默认为空。表示不需要该功能。
	Compressions []*Compression

	// 指定可用的 mimetype
	//
	// 默认采用 [JSONMimetypes]。
	Mimetypes []*Mimetype

	// 默认的语言标签
	//
	// 在用户请求的报头中没有匹配的语言标签时,会采用此值作为该用户的本地化语言,
	// 同时也用来初始化 [Server.Locale.Printer]。
	//
	// 框架中的日志输出时,如果该信息实现了 [LocaleStringer] 接口,
	// 将会转换成此设置项的语言。
	//
	// 如果为空,则会尝试读取当前系统的本地化信息。
	Language language.Tag

	// 本地化的数据
	//
	// 如果为空,则会被初始化成一个空对象。
	// Catalog 中会强行插入一条 tag 与 [Options.Language] 相同的翻译项,
	// 以保证能正确构建 [web.Server.Printer] 对象。
	Catalog *catalog.Builder

	// ProblemTypePrefix 所有 type 字段的前缀
	//
	// 如果该值为 [ProblemAboutBlank],将不输出 ID 值;其它值则作为前缀添加。
	ProblemTypePrefix string

	// Init 其它的一些初始化操作
	//
	// 在此可以在用户能实际操作 [Server] 之前对 Server 进行一些操作。
	Init []func(web.Server)
	// contains filtered or unexported fields
}

Options web.Server 的初始化参数

这些参数都有默认值,且无法在 web.Server 初始化之后进行更改。

Directories

Path Synopsis
Package servertest 为 [web.Server] 提供一些简便的测试方
Package servertest 为 [web.Server] 提供一些简便的测试方

Jump to

Keyboard shortcuts

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