httpMiddleware

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 22 Imported by: 0

README

http中间件

获取ip

httpMiddleware.GetIP(r *http.Request) (ip string)


CorsHandler 跨域中间件

httpMiddleware.CorsHandler()


启动多个web服务进程,做本地负载

httpMiddleware.SetSockOptInt() net.ListenConfig

func RunHttpService() {
	var lc = httpMiddleware.SetSockOptInt()
	// 启动五个进程
	for i := 0; i < 5; i++ {
		go func(i int) {
			gin.SetMode(gin.ReleaseMode)
			s := Routers(fmt.Sprintf("%d", i))
			lis, err := lc.Listen(context.Background(), "tcp", "0.0.0.0:14444")
			if err != nil {
				panic("启动 http api 失败, err =  " + err.Error())
			}
			logger.Info("启动 Http API , ID:", i)
			err = s.RunListener(lis)
			if err != nil {
				panic("启动 http api 失败, err =  " + err.Error())
			}
		}(i)
	}
}

Documentation

Index

Constants

View Source
const (
	BodyJSON       = "application/json; charset=utf-8"
	BodyAsciiJSON  = "application/json"
	BodyHTML       = "text/html; charset=utf-8"
	BodyJavaScript = "application/javascript; charset=utf-8"
	BodyXML        = "application/xml; charset=utf-8"
	BodyPlain      = "text/plain; charset=utf-8"
	BodyYAML       = "application/x-yaml; charset=utf-8"
	BodyDownload   = "application/octet-stream; charset=utf-8"
	BodyPDF        = "application/pdf"
	BodyJPG        = "image/jpeg"
	BodyPNG        = "image/png"
	BodyGif        = "image/gif"
	BodyWord       = "application/msword"
	BodyOctet      = "application/octet-stream"
)
View Source

Variables

View Source
var Path, _ = os.Getwd()

Functions

func Base

func Base(next http.Handler) http.Handler

Base Http基础中间件,日志

func BaseFunc

func BaseFunc(next http.Handler) http.HandlerFunc

BaseFunc Base Http基础中间件,日志

func CorsHandler

func CorsHandler() gin.HandlerFunc

CorsHandler 跨域中间件

func GetClientIp

func GetClientIp(r *http.Request) string

func GetCookie

func GetCookie(r *http.Request, name string) (*http.Cookie, error)

func GetCookieVal

func GetCookieVal(r *http.Request, name string) string

func GetFromArg

func GetFromArg(r *http.Request, name string) string

func GetFromFile

func GetFromFile(r *http.Request, name string) (multipart.File, *multipart.FileHeader, error)

func GetHeader

func GetHeader(r *http.Request, name string) string

func GetIP

func GetIP(r *http.Request) (ip string)

GetIP 获取ip - X-Real-IP:只包含客户端机器的一个IP,如果为空,某些代理服务器(如Nginx)会填充此header。 - X-Forwarded-For:一系列的IP地址列表,以,分隔,每个经过的代理服务器都会添加一个IP。 - RemoteAddr:包含客户端的真实IP地址。 这是Web服务器从其接收连接并将响应发送到的实际物理IP地址。 但是,如果客户端通过代理连接,它将提供代理的IP地址。

RemoteAddr是最可靠的,但是如果客户端位于代理之后或使用负载平衡器或反向代理服务器时,它将永远不会提供正确的IP地址,因此顺序是先是X-REAL-IP, 然后是X-FORWARDED-FOR,然后是 RemoteAddr。 请注意,恶意用户可以创建伪造的X-REAL-IP和X-FORWARDED-FOR标头。

func GetIp

func GetIp(r *http.Request) (ip string)

func GetJsonParam

func GetJsonParam(r *http.Request, param interface{})

func GetUrlArg

func GetUrlArg(r *http.Request, name string) string

GetUrlArg 获取URL的GET参数

func GetUrlArgInt

func GetUrlArgInt(r *http.Request, name string) int

func GetUrlArgInt64

func GetUrlArgInt64(r *http.Request, name string) int64

func Health

func Health(w http.ResponseWriter, r *http.Request)

func Hello

func Hello(w http.ResponseWriter, r *http.Request)

func Out404

func Out404(w http.ResponseWriter)

func OutErrBody

func OutErrBody(w http.ResponseWriter, code int, err error)

func OutJPG

func OutJPG(w http.ResponseWriter, path string)

func OutPdf

func OutPdf(w http.ResponseWriter, path string)

func OutStaticFile

func OutStaticFile(w http.ResponseWriter, path string)

OutStaticFile 输出静态文件

func OutSucceedBody

func OutSucceedBody(w http.ResponseWriter, data interface{})

func OutSucceedBodyJsonP

func OutSucceedBodyJsonP(w http.ResponseWriter, data interface{})

func OutUploadFile

func OutUploadFile(w http.ResponseWriter, path, fileName string)

OutUploadFile 给客户端下载的静态文件

func ReqLimit

func ReqLimit(ipv *IpVisitor, nextHeader http.Handler) http.Handler

ReqLimit 基础中间件 IP限流, IP黑白名单

func SetCookie

func SetCookie(w http.ResponseWriter, name, value string, t int)

func SetCookieMap

func SetCookieMap(w http.ResponseWriter, data map[string]string, t int)

func SetSockOptInt

func SetSockOptInt() net.ListenConfig

SetSockOptInt 启动多个web服务进程,做本地负载

Types

type Engine

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

func NewEngine

func NewEngine() *Engine

func SimpleEngine

func SimpleEngine() *Engine

func (*Engine) GetMux

func (engine *Engine) GetMux() *http.ServeMux

func (*Engine) OpenPprof

func (engine *Engine) OpenPprof()

func (*Engine) Router

func (engine *Engine) Router(path string, f func(w http.ResponseWriter, r *http.Request))

func (*Engine) RouterFunc

func (engine *Engine) RouterFunc(path string, f func(w http.ResponseWriter, r *http.Request))

func (*Engine) Run

func (engine *Engine) Run(port string)

type HttpOutBody

type HttpOutBody struct {
	Code      int         `json:"code"`
	Timestamp int64       `json:"timestamp"`
	Msg       string      `json:"msg"`
	Data      interface{} `json:"data"`
}

HttpOutBody HTTP 输出 json body 定义 [Code] - 0 成功 - 1001 参数错误 - 2001 程序错误

func (*HttpOutBody) JsonStr

func (m *HttpOutBody) JsonStr() (string, error)

type IpVisitor

type IpVisitor struct {
	BlackList map[string]struct{}
	WhiteList map[string]struct{}
	// contains filtered or unexported fields
}

func NewIpVisitor

func NewIpVisitor() *IpVisitor

func (*IpVisitor) AddBlackList

func (ipv *IpVisitor) AddBlackList(ip string)

func (*IpVisitor) AddVisitor

func (ipv *IpVisitor) AddVisitor(ip string) *rate.Limiter

func (*IpVisitor) AddWhiteList

func (ipv *IpVisitor) AddWhiteList(ip string)

func (*IpVisitor) CleanupVisitors

func (ipv *IpVisitor) CleanupVisitors()

CleanupVisitors 启动一个协成 10分钟查一下ip限流数据,看看有没有超过1小时删除记录,有就删除 主要目的的为了释放内存空间

func (*IpVisitor) DelBlackList

func (ipv *IpVisitor) DelBlackList(ip string)

func (*IpVisitor) DelWhiteList

func (ipv *IpVisitor) DelWhiteList(ip string)

func (*IpVisitor) GetVisitor

func (ipv *IpVisitor) GetVisitor(ip string) *rate.Limiter

func (*IpVisitor) IsBlackList

func (ipv *IpVisitor) IsBlackList(ip string) (ok bool)

func (*IpVisitor) IsWhiteList

func (ipv *IpVisitor) IsWhiteList(ip string) (ok bool)

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	StatusCode int
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) WriteHeader

func (lrw *ResponseWriter) WriteHeader(code int)

Jump to

Keyboard shortcuts

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