muxutil

package
v6.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 17 Imported by: 1

Documentation

Overview

Package muxutil 为 mux 提供的一些额外工具

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndMatcher

func AndMatcher(m ...mux.Matcher) mux.Matcher

AndMatcher 按顺序符合每一个要求

前一个对象返回的实例将作为下一个对象的输入参数。

func AndMatcherFunc

func AndMatcherFunc(f ...func(*http.Request) (params.Params, bool)) mux.Matcher

AndMatcherFunc 需同时符合每一个要求

func Debug

func Debug(p string, w http.ResponseWriter, r *http.Request) error

Debug 输出调试信息

p 是指路由中的参数名,比如以下示例中,p 的值为 debug:

r.Get("/test{debug}", func(w http.ResponseWriter, r *http.Request) {
    p := mux.GetParams(r).String("debug")
    Debug(p, w, r)
}

p 所代表的路径包含了前缀的 /。

func OrMatcher

func OrMatcher(m ...mux.Matcher) mux.Matcher

OrMatcher 仅需符合一个要求

func OrMatcherFunc

func OrMatcherFunc(f ...func(*http.Request) (params.Params, bool)) mux.Matcher

OrMatcherFunc 仅需符合一个要求

func ServeFile

func ServeFile(fsys fs.FS, p, index string, w http.ResponseWriter, r *http.Request) error

ServeFile 提供对静态文件的服务

p 表示需要读取的文件名; index 表示 p 为目录时,默认读取的文件,为空表示 index.html;

func Trace

func Trace(w http.ResponseWriter, r *http.Request, body bool) error

Trace 简单的 Trace 请求方法实现

NOTE: 并不是百分百原样返回,具体可参考 net/http/httputil.DumpRequest 的说明。 如果内容包含特殊的 HTML 字符会被 html.EscapeString 转码。

Types

type HeaderVersion

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

HeaderVersion 匹配报头的版本号

匹配报头 Accept 中的报头信息。

func NewHeaderVersion

func NewHeaderVersion(param, key string, errlog *log.Logger, version ...string) *HeaderVersion

NewHeaderVersion 声明 HeaderVersion 实例

param 将版本号作为参数保存到上下文中时的名称,如果不需要保存参数,可以设置为空值; errlog 错误日志输出通道,如果为空则采用 log.Default(); key 表示在 accept 报头中的表示版本号的参数名,如果为空则采用 version; version 版本的值,可能为空,表示匹配任意值;

func (*HeaderVersion) Match

func (v *HeaderVersion) Match(r *http.Request) (ret params.Params, ok bool)

type Hosts

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

Hosts 限定域名的匹配工具

func NewHosts

func NewHosts(lock bool, domain ...string) *Hosts

NewHosts 声明新的 Hosts 实例

func (*Hosts) Add

func (hs *Hosts) Add(domain ...string)

Add 添加新的域名

域名的格式和路由的语法格式是一样的,比如:

api.example.com
{sub:[a-z]+}.example.com

如果存在命名参数,也可以通过 syntax.GetParams 获取。 当语法错误时,会触发 panic,可通过 CheckSyntax 检测语法的正确性。

func (*Hosts) Delete

func (hs *Hosts) Delete(domain string)

Delete 删除域名

func (*Hosts) Match

func (hs *Hosts) Match(r *http.Request) (params.Params, bool)

func (*Hosts) RegisterInterceptor

func (hs *Hosts) RegisterInterceptor(f mux.InterceptorFunc, name ...string)

RegisterInterceptor 注册拦截器

NOTE: 拦截器只有在注册之后添加的域名才有效果。

type PathVersion

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

PathVersion 匹配路径中的版本号

会修改 http.Request.URL.Path 的值,去掉匹配的版本号路径部分,比如:

/v1/path.html

如果匹配 v1 版本,会修改为:

/path.html

func NewPathVersion

func NewPathVersion(param string, version ...string) *PathVersion

NewPathVersion 声明 PathVersion 实例

param 将版本号作为参数保存到上下文中是的名称,如果不需要保存参数,可以设置为空值; version 版本的值,可以为空,表示匹配任意值;

func (*PathVersion) Match

func (v *PathVersion) Match(r *http.Request) (ps params.Params, ok bool)

Jump to

Keyboard shortcuts

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