whttp

package module
v0.0.0-...-c9734a9 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMarshal func(any) ([]byte, error) = json.Marshal

DefaultMarshal 缺省JSON编码器

View Source
var DefaultUnmarshal func([]byte, any) error = json.Unmarshal

DefaultUnmarshal 缺省JSON解码器

View Source
var HTTPContextPool = sync.Pool{
	New: func() interface{} {
		return &HTTPContext{}
	},
}
View Source
var IPAdmissioneEmpty struct{}

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func ClientPublicIP

func ClientPublicIP(r *http.Request) string

ClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func HasLocalIPAddr

func HasLocalIPAddr(ip net.IP) bool

HasLocalIPAddr 检测 IP 地址是否是内网地址 通过直接对比ip段范围效率更高,详见:https://github.com/thinkeridea/go-extend/issues/2

func LoggerMiddleware

func LoggerMiddleware() func(*HTTPContext)

LoggerMiddleware 日志

func RemoteIP

func RemoteIP(r *http.Request) string

RemoteIP 通过 RemoteAddr 获取 IP 地址。

func ValidatorMiddleware

func ValidatorMiddleware(a ...string) func(*HTTPContext)

ValidatorMiddleware 输入参数验证

Types

type HTTPContext

type HTTPContext struct {
	Writer  http.ResponseWriter
	Request *http.Request
	// contains filtered or unexported fields
}

HTTPContext 上下文

func (*HTTPContext) BindJSON

func (c *HTTPContext) BindJSON(v any) error

BindJSON 绑定JSON数据

func (*HTTPContext) Debug

func (c *HTTPContext) Debug(msg string, args ...any)

func (*HTTPContext) Error

func (c *HTTPContext) Error(msg string, args ...any)

func (*HTTPContext) Get

func (c *HTTPContext) Get(k string) (v any, b bool)

Get 上下文key-value值

func (*HTTPContext) Info

func (c *HTTPContext) Info(msg string, args ...any)

func (*HTTPContext) JSON

func (c *HTTPContext) JSON(status int, v any)

JSON 带有状态码的JSON 数据

func (*HTTPContext) Next

func (c *HTTPContext) Next()

Next 下一个

func (*HTTPContext) Set

func (c *HTTPContext) Set(k string, v any)

Set 上下文key-value值

func (*HTTPContext) String

func (c *HTTPContext) String(status int, msg string)

String 带有状态码的纯文本响应

func (*HTTPContext) Warn

func (c *HTTPContext) Warn(msg string, args ...any)

type IPAdmission

type IPAdmission struct {
	sync.Map
}

IPAdmission IP准入

func NewIPAdmission

func NewIPAdmission() *IPAdmission

func (*IPAdmission) BlacklistMiddleware

func (f *IPAdmission) BlacklistMiddleware() func(*HTTPContext)

BlacklistMiddleware 黑名单。

func (*IPAdmission) Check

func (f *IPAdmission) Check(line string) any

Check 检查某个ip在不在设置的规则里

func (*IPAdmission) ParseNode

func (f *IPAdmission) ParseNode(line string) error

ParseNode 添加规则

func (*IPAdmission) RemoveNode

func (f *IPAdmission) RemoveNode(line string) error

RemoveNode 移除规则

func (*IPAdmission) WhitelistMiddleware

func (f *IPAdmission) WhitelistMiddleware() func(*HTTPContext)

WhitelistMiddleware 白名单。

type JWT

type JWT struct {
	TokenSigningKey []byte
	TokenExpires    time.Duration
}

JWT

func (JWT) CreateToken

func (j JWT) CreateToken(key []string, obj []any) (string, error)

CreateToken 生成

func (JWT) JWTMiddleware

func (j JWT) JWTMiddleware() func(*HTTPContext)

JWTMiddleware 中间件

func (JWT) TokenParse

func (j JWT) TokenParse(tokenString string) (jwt.MapClaims, error)

TokenParse 令牌解析

type MemoryFile

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

func (*MemoryFile) CacheTemplate

func (mf *MemoryFile) CacheTemplate(parseFiles, key string, data any) error

CacheTemplate 缓存模板

func (*MemoryFile) DeleteFile

func (mf *MemoryFile) DeleteFile(file string)

DelFile 删除缓存文件

func (*MemoryFile) WriteTo

func (mf *MemoryFile) WriteTo(key string, w io.Writer) (int, error)

type WRoute

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

WRoute 路由

func NewRoute

func NewRoute(v utils.IValidator, l *slog.Logger) *WRoute

NewRoute 新建

func (*WRoute) CacheFS

func (r *WRoute) CacheFS(dir string, mf *MemoryFile, group ...func(*HTTPContext)) error

func (*WRoute) CacheFile

func (r *WRoute) CacheFile(file string, mf *MemoryFile, group ...func(*HTTPContext)) error

CacheFile 缓存单个静态文件

func (*WRoute) DELETE

func (r *WRoute) DELETE(pattern string, fn ...func(*HTTPContext))

DELETE d

func (*WRoute) GET

func (r *WRoute) GET(pattern string, fn ...func(*HTTPContext))

GET g

func (*WRoute) POST

func (r *WRoute) POST(pattern string, fn ...func(*HTTPContext))

POST p

func (*WRoute) SetDebugMode

func (r *WRoute) SetDebugMode(b bool)

Jump to

Keyboard shortcuts

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