完善 中间件 reload
This commit is contained in:
parent
dc871c1488
commit
f2dcd9f81e
|
@ -5,12 +5,15 @@ a simple front of WordPress build with golang.
|
||||||
|
|
||||||
#### 特色功能
|
#### 特色功能
|
||||||
|
|
||||||
- 缓存配置
|
- 多种缓存配置
|
||||||
- 添加评论或panic时发邮件通知,包涵栈调用和请求信息
|
- 添加评论或panic时发邮件通知,包涵栈调用和请求信息
|
||||||
- 简单的流量限制中间件
|
- 简单的流量限制中间件
|
||||||
- 除配置文件外将所有静态资源都打包到执行文件中
|
- 除配置文件外将所有静态资源都打包到执行文件中
|
||||||
- 支持密码查看,且cookie信息可被php版所验证
|
- 支持密码查看,且cookie信息可被php版所验证
|
||||||
- 支持rss2订阅
|
- 支持rss2订阅
|
||||||
|
- 热更新配置、清空缓存
|
||||||
|
- kill -SIGUSR1 PID 更新配置和清空缓存
|
||||||
|
- kill -SIGUSR2 PID 清空缓存
|
||||||
|
|
||||||
#### 其它
|
#### 其它
|
||||||
用的gin框架和sqlx,在外面封装了层查询的方法。后台可以设置的比较少,大部分设置还没打通。
|
用的gin框架和sqlx,在外面封装了层查询的方法。后台可以设置的比较少,大部分设置还没打通。
|
|
@ -10,15 +10,19 @@ import (
|
||||||
type IpLimitMap struct {
|
type IpLimitMap struct {
|
||||||
mux *sync.RWMutex
|
mux *sync.RWMutex
|
||||||
m map[string]*int64
|
m map[string]*int64
|
||||||
limitNum int64
|
limitNum *int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func IpLimit(num int64) func(ctx *gin.Context) {
|
func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) {
|
||||||
m := IpLimitMap{
|
m := IpLimitMap{
|
||||||
mux: &sync.RWMutex{},
|
mux: &sync.RWMutex{},
|
||||||
m: make(map[string]*int64),
|
m: make(map[string]*int64),
|
||||||
limitNum: num,
|
limitNum: new(int64),
|
||||||
}
|
}
|
||||||
|
fn := func(num int64) {
|
||||||
|
atomic.StoreInt64(m.limitNum, num)
|
||||||
|
}
|
||||||
|
fn(num)
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
ip := c.ClientIP()
|
ip := c.ClientIP()
|
||||||
|
@ -45,7 +49,7 @@ func IpLimit(num int64) func(ctx *gin.Context) {
|
||||||
m.mux.Unlock()
|
m.mux.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.limitNum > 0 && atomic.LoadInt64(i) >= m.limitNum {
|
if atomic.LoadInt64(m.limitNum) > 0 && atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) {
|
||||||
c.Status(http.StatusForbidden)
|
c.Status(http.StatusForbidden)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
|
@ -53,5 +57,5 @@ func IpLimit(num int64) func(ctx *gin.Context) {
|
||||||
atomic.AddInt64(i, 1)
|
atomic.AddInt64(i, 1)
|
||||||
s = true
|
s = true
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}, fn
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package middleware
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func SearchLimit(num int64) func(ctx *gin.Context) {
|
func SearchLimit(num int64) (func(ctx *gin.Context), func(int64)) {
|
||||||
fn := IpLimit(num)
|
fn, reFn := IpLimit(num)
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if c.Query("s") != "" {
|
if c.Query("s") != "" {
|
||||||
fn(c)
|
fn(c)
|
||||||
|
@ -11,5 +11,5 @@ func SearchLimit(num int64) func(ctx *gin.Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}, reFn
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,8 @@ func SetupRouter() (*gin.Engine, func()) {
|
||||||
}))
|
}))
|
||||||
store := cookie.NewStore([]byte("secret"))
|
store := cookie.NewStore([]byte("secret"))
|
||||||
r.Use(sessions.Sessions("go-wp", store))
|
r.Use(sessions.Sessions("go-wp", store))
|
||||||
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.Index)
|
sl, slRload := middleware.SearchLimit(c.SingleIpSearchNum)
|
||||||
|
r.GET("/", sl, actions.Index)
|
||||||
r.GET("/page/:page", actions.Index)
|
r.GET("/page/:page", actions.Index)
|
||||||
r.GET("/p/category/:category", actions.Index)
|
r.GET("/p/category/:category", actions.Index)
|
||||||
r.GET("/p/category/:category/page/:page", actions.Index)
|
r.GET("/p/category/:category/page/:page", actions.Index)
|
||||||
|
@ -86,6 +87,7 @@ func SetupRouter() (*gin.Engine, func()) {
|
||||||
reloadValidServerNameFn()
|
reloadValidServerNameFn()
|
||||||
c := config.Conf.Load()
|
c := config.Conf.Load()
|
||||||
flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
|
flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
|
||||||
|
slRload(c.SingleIpSearchNum)
|
||||||
}
|
}
|
||||||
return r, fn
|
return r, fn
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user