web

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2021 License: MIT Imports: 28 Imported by: 7

Documentation

Index

Constants

View Source
const (
	//PARAM 如果路由有参数,则值存储在 Kind 中
	PARAM = 2
	//SUB 如果路由是子路由器,则值存储在 Kind 中
	SUB = 4
	//WC 如果路由有通配符,则值存储在 Kind 中
	WC = 8
	//REGEX 如果路由包含正则表达式,则值存储在 Kind 中
	REGEX = 16
)
View Source
const DefaultTplSetName = "DEFAULT"
View Source
const SupportPackageIsVersion1 = true

SupportPackageIsVersion1 不应从任何其他代码中引用这些常量.

Variables

View Source
var (
	TemplateEnv = "development"
)

Functions

func CheckResponse

func CheckResponse(_ context.Context, res *http.Response) error

CheckResponse 如果响应状态代码不是2xx,则返回错误(*Error类型).

func Do

func Do(client *http.Client, req *http.Request, target interface{}) error

Do 发送HTTP请求并将响应主体解码为目标. 如果响应状态代码不是2xx,则返回错误(type *Error).

func NewClient

func NewClient(ctx context.Context, opts ...ClientOption) (*http.Client, error)

NewClient 返回一个HTTP客户端.

func NewClientContext

func NewClientContext(ctx context.Context, info ClientInfo) context.Context

NewClientContext 返回一个带有值的新Context.

func NewServerContext

func NewServerContext(ctx context.Context, info ServerInfo) context.Context

NewServerContext 返回一个带有值的新Context.

func NewTransport

func NewTransport(ctx context.Context, opts ...ClientOption) (http.RoundTripper, error)

NewTransport 创建一个http.RoundTripper.

func PathValue added in v0.0.4

func PathValue(req *http.Request, key string) string

PathValue 通过当前 *http.Request 返回指定key的值

func Process

func Process(h HandleOptions, w http.ResponseWriter, r *http.Request, next func(ctx context.Context, req interface{}) (interface{}, error))

func Vars added in v0.0.4

func Vars(req *http.Request) map[string]string

Vars 返回请求参数

Types

type ClientInfo

type ClientInfo struct {
	Request *http.Request
}

ClientInfo 表示HTTP客户端信息.

func FromClientContext

func FromClientContext(ctx context.Context) (info ClientInfo, ok bool)

FromClientContext 返回存储在ctx中的传输值(如果有).

type ClientOption

type ClientOption func(*clientOptions)

ClientOption 是HTTP客户端选项.

func WithMiddleware

func WithMiddleware(m middleware.Middleware) ClientOption

WithMiddleware 与客户端中间件.

func WithTimeout

func WithTimeout(d time.Duration) ClientOption

WithTimeout 客户请求超时.

func WithTransport

func WithTransport(trans http.RoundTripper) ClientOption

WithTransport 与客户传输层.

func WithUserAgent

func WithUserAgent(ua string) ClientOption

WithUserAgent 与客户用户代理.

type DecodeErrorFunc

type DecodeErrorFunc func(ctx context.Context, w *http.Response) error

DecodeErrorFunc 是解码错误功能.

type DecodeRequestFunc

type DecodeRequestFunc func(*http.Request, interface{}) error

DecodeRequestFunc 是解码请求函数.

type Delims

type Delims struct {
	Left  string // 左定界符,默认为{{
	Right string // 右定界符,默认为}}
}

Delims 代表用于HTML模板渲染的一组左右定界符

type EncodeErrorFunc

type EncodeErrorFunc func(http.ResponseWriter, *http.Request, error)

EncodeErrorFunc 是编码错误处理函数.

type EncodeResponseFunc

type EncodeResponseFunc func(http.ResponseWriter, *http.Request, interface{}) error

EncodeResponseFunc 是编码响应函数.

type HTMLOptions

type HTMLOptions struct {
	Layout string // 布局模板名称.覆盖Options.Layout.
}

HTMLOptions 是用于覆盖特定HTML调用的某些呈现选项的结构

type HandleOption

type HandleOption func(*HandleOptions)

HandleOption 是句柄选项.

func ErrorEncoder

func ErrorEncoder(en EncodeErrorFunc) HandleOption

ErrorEncoder 带有错误编码器.

func Middleware

func Middleware(m ...middleware.Middleware) HandleOption

Middleware 带中间件选项.

func RequestDecoder

func RequestDecoder(dec DecodeRequestFunc) HandleOption

RequestDecoder 带请求解码器.

func ResponseEncoder

func ResponseEncoder(en EncodeResponseFunc) HandleOption

ResponseEncoder 带有响应编码器.

type HandleOptions

type HandleOptions struct {
	Decode     DecodeRequestFunc
	Encode     EncodeResponseFunc
	Error      EncodeErrorFunc
	Middleware middleware.Middleware
}

HandleOptions 是处理器选项集.

func DefaultHandleOptions

func DefaultHandleOptions() HandleOptions

DefaultHandleOptions 返回默认的处理器选项.

type HealthFunc

type HealthFunc func(ctx context.Context) error

HealthFunc 包装心跳检查方法.

HealthFunc 如果资源状况良好,则返回nil;如果资源状况不健康,则返回non-nil错误. HealthFunc 必须安全地从多个goroutine调用.

type HealthHandler

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

HealthHandler 是一个HTTP处理器,报告了一组Checkers的成功.零值代表始终健康.

func NewHealthHandler

func NewHealthHandler() *HealthHandler

NewHealthHandler 新的处理器.

func (*HealthHandler) AddChecker

func (h *HealthHandler) AddChecker(name string, c HealthFunc)

AddChecker 向处理程序添加新的检查.

func (*HealthHandler) AddObserver

func (h *HealthHandler) AddObserver(name string, c HealthFunc)

AddObserver 向处理程序添加新的检查,但不会使整个状态失败.

func (*HealthHandler) ServeHTTP

func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 如果健康,则返回200,否则返回500.

type Node added in v0.0.4

type Node struct {
	Path   string // 是节点 URL
	Method string // 在路由上定义 HTTP 方法
	Size   int    //是路径的长度
	Kind   int

	Token   Token          // 是路径每个部分的值,分隔符为`/`
	Pattern map[int]string // 是关于路由的内容信息, 如果它有一个路由变量
	Compile map[int]*regexp.Regexp
	Tag     map[int]string
	Handler http.Handler // 是这条路由的处理者
	// contains filtered or unexported fields
}

Node 有效路由所需的信息

func CurrentRoute added in v0.0.4

func CurrentRoute(req *http.Request) *Node

CurrentRoute 返回当前请求的匹配路由(如果有). 这仅在匹配路由的处理程序内部调用时才有效,因为匹配的路由存储在请求上下文中,该上下文在处理程序返回后被清除.

func NewNode added in v0.0.4

func NewNode(mux *Router, url string, h http.Handler) *Node

NewNode 返回一个指向 Node 实例的指针并对其调用save方法

func (*Node) Match added in v0.0.4

func (r *Node) Match(req *http.Request) bool

Match 检查请求是否与路由规则匹配

func (*Node) ServeHTTP added in v0.0.4

func (r *Node) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type RenderOptions

type RenderOptions struct {
	Directory          string             // 加载模板目标.默认为"templates".
	AppendDirectories  []string           // 附加目录会覆盖默认模板.
	Layout             string             // 布局模板名称. 如果为""代表不会渲染布局.默认是"".
	Extensions         []string           // 用于从中解析模板文件的扩展. 默认值为[".tmpl", ".html"].
	Funcs              []template.FuncMap // Funcs是FuncMap的一部分,可在编译时应用于模板. 这对于助手功能很有用. 默认是[].
	Delims             Delims             // 将定界符设置为Delims结构中的指定字符串.
	Charset            string             // 将给定的字符集附加到Content-Type标头.默认是"UTF-8".
	HTMLContentType    string             // 允许将输出更改为XHTML而不是HTML.默认是"text/html".
	TemplateFileSystem                    // TemplateFileSystem是用于支持任何模板文件系统实现的接口.
}

RenderOptions 表示用于指定Render中间件的配置选项的结构。.

type Router added in v0.0.4

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

Router 是一个 http.Handler 可用于通过可配置的路由将请求分派到不同的处理程序函数

func NewRouter added in v0.0.4

func NewRouter(routers ...router) *Router

NewRouter 创建一个指向 Router 实例

func (*Router) DELETE added in v0.0.4

func (m *Router) DELETE(path string, handler http.HandlerFunc)

DELETE 使用 DELETE 方法向路由器添加新路由

func (*Router) GET added in v0.0.4

func (m *Router) GET(path string, handler http.HandlerFunc)

GET 使用 GET 方法向路由器添加新路由

func (*Router) GetRequestRoute added in v0.0.4

func (m *Router) GetRequestRoute(req *http.Request) string

GetRequestRoute returns the route of given Request

func (*Router) HEAD added in v0.0.4

func (m *Router) HEAD(path string, handler http.HandlerFunc)

HEAD 使用 HEAD 方法向路由器添加新路由

func (*Router) Handle added in v0.0.4

func (m *Router) Handle(path string, handler http.Handler)

Handle 在没有HTTP方法的情况下向路由器添加新路由

func (*Router) HandleFunc added in v0.0.4

func (m *Router) HandleFunc(path string, handler http.HandlerFunc)

HandleFunc 用于传递 func(http.ResponseWriter, *Http.Request) 而不是 http.Handler

func (*Router) NotFound added in v0.0.4

func (m *Router) NotFound(handler http.Handler)

NotFound 路由器自定义404处理程序

func (*Router) NotFoundFunc added in v0.0.4

func (m *Router) NotFoundFunc(handler http.HandlerFunc)

NotFoundFunc 路由器自定义 404 处理程序

func (*Router) OPTIONS added in v0.0.4

func (m *Router) OPTIONS(path string, handler http.HandlerFunc)

OPTIONS 使用 OPTIONS 方法向路由器添加新路由

func (*Router) PATCH added in v0.0.4

func (m *Router) PATCH(path string, handler http.HandlerFunc)

PATCH 使用 PATCH 方法向路由器添加新路由

func (*Router) POST added in v0.0.4

func (m *Router) POST(path string, handler http.HandlerFunc)

POST 使用 POST 方法向路由器添加新路由

func (*Router) PUT added in v0.0.4

func (m *Router) PUT(path string, handler http.HandlerFunc)

PUT 使用 PUT 方法向路由器添加新路由

func (*Router) Prefix added in v0.0.4

func (m *Router) Prefix(p string) *Router

Prefix set a default prefix for all routes registered on the router

func (*Router) ServeHTTP added in v0.0.4

func (m *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP 是默认的 http 请求处理程序

func (*Router) SubRoute added in v0.0.4

func (m *Router) SubRoute(path string, router http.Handler)

SubRoute 将路由器注册为子路由器

type Server

type Server struct {
	*http.Server
	// contains filtered or unexported fields
}

Server 是一个HTTP服务器包装器.

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer 通过选项创建HTTP服务器.

func (*Server) Endpoint

func (s *Server) Endpoint() (*url.URL, error)

Endpoint 返回真实地址到注册表端点. 例子:

http://127.0.0.1:8080

func (*Server) Handle

func (s *Server) Handle(path string, h http.Handler)

Handle 使用URL的匹配器注册新路由.

func (*Server) HandleFunc

func (s *Server) HandleFunc(path string, h http.HandlerFunc)

HandleFunc 使用URL的匹配器注册新路由.

func (*Server) PathPrefix

func (s *Server) PathPrefix(prefix string, h http.Handler)

PathPrefix 使用匹配器为URL路径前缀注册新路由.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP 应该将回复标头和数据写入ResponseWriter,然后返回.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start 启动Web服务器.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop 停止Web服务器.

type ServerInfo

type ServerInfo struct {
	Request  *http.Request
	Response http.ResponseWriter
}

ServerInfo 表示HTTP服务器信息.

func FromServerContext

func FromServerContext(ctx context.Context) (info ServerInfo, ok bool)

FromServerContext 返回存储在ctx中的传输值(如果有).

type ServerOption

type ServerOption func(*Server)

ServerOption 是HTTP服务器选项.

func Address

func Address(addr string) ServerOption

Address 与服务器地址.

func Logger

func Logger(logger log.Logger) ServerOption

Logger 使用服务器记录器.

func Network

func Network(network string) ServerOption

Network 与服务器网络.

func Timeout

func Timeout(timeout time.Duration) ServerOption

Timeout 与服务器超时.

type TemplateFile

type TemplateFile interface {
	Name() string
	Data() []byte
	Ext() string
}

TemplateFile 表示具有名称且可以读取的模板文件的接口.

type TemplateFileSystem

type TemplateFileSystem interface {
	ListFiles() []TemplateFile
	Get(string) (io.Reader, error)
}

TemplateFileSystem 表示能够列出所有文件的模板文件系统的接口.

type TemplateSet

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

TemplateSet 表示类型为*template.Template的模板集。

func NewTemplateSet

func NewTemplateSet() *TemplateSet

NewTemplateSet 初始化一个新的空模板集.

func (*TemplateSet) Get

func (ts *TemplateSet) Get(name string) *template.Template

func (*TemplateSet) GetDir

func (ts *TemplateSet) GetDir(name string) string

func (*TemplateSet) Set

func (ts *TemplateSet) Set(name string, opt *RenderOptions) *template.Template

type Token added in v0.0.4

type Token struct {
	Tokens []string //每个令牌的字符串值
	Size   int      // 令牌数量
	// contains filtered or unexported fields
}

Token 分割路由路径的所有值

type TplFile

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

TplFile 实现TemplateFile接口.

func NewTplFile

func NewTplFile(name string, data []byte, ext string) *TplFile

NewTplFile 创建具有给定名称和数据的新模板文件.

func (*TplFile) Data

func (f *TplFile) Data() []byte

func (*TplFile) Ext

func (f *TplFile) Ext() string

func (*TplFile) Name

func (f *TplFile) Name() string

type TplFileSystem

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

TplFileSystem 实现TemplateFileSystem接口.

func NewTemplateFileSystem

func NewTemplateFileSystem(opt RenderOptions, omitData bool) TplFileSystem

NewTemplateFileSystem 使用给定的选项创建新的模板文件系统.

func (TplFileSystem) Get

func (fs TplFileSystem) Get(name string) (io.Reader, error)

func (TplFileSystem) ListFiles

func (fs TplFileSystem) ListFiles() []TemplateFile

type TplRender

type TplRender struct {
	http.ResponseWriter
	*TemplateSet
	Opt     *RenderOptions
	Charset string
	// contains filtered or unexported fields
}

func (*TplRender) HTML

func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)

func (*TplRender) HTMLSet

func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)

func (*TplRender) HTMLSetBytes

func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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