diff --git a/README.md b/README.md index a72b8fa..08a9ba1 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,15 @@ a simple front of WordPress build with golang. #### 特色功能 -- 缓存配置 +- 多种缓存配置 - 添加评论或panic时发邮件通知,包涵栈调用和请求信息 - 简单的流量限制中间件 - 除配置文件外将所有静态资源都打包到执行文件中 - 支持密码查看,且cookie信息可被php版所验证 - 支持rss2订阅 +- 热更新配置、清空缓存 + - kill -SIGUSR1 PID 更新配置和清空缓存 + - kill -SIGUSR2 PID 清空缓存 #### 其它 用的gin框架和sqlx,在外面封装了层查询的方法。后台可以设置的比较少,大部分设置还没打通。 \ No newline at end of file diff --git a/middleware/iplimit.go b/middleware/iplimit.go index 940356f..feb58ae 100644 --- a/middleware/iplimit.go +++ b/middleware/iplimit.go @@ -10,15 +10,19 @@ import ( type IpLimitMap struct { mux *sync.RWMutex 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{ mux: &sync.RWMutex{}, 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) { ip := c.ClientIP() @@ -45,7 +49,7 @@ func IpLimit(num int64) func(ctx *gin.Context) { 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.Abort() return @@ -53,5 +57,5 @@ func IpLimit(num int64) func(ctx *gin.Context) { atomic.AddInt64(i, 1) s = true c.Next() - } + }, fn } diff --git a/middleware/searchlimit.go b/middleware/searchlimit.go index 52a52cd..eda4ab8 100644 --- a/middleware/searchlimit.go +++ b/middleware/searchlimit.go @@ -2,8 +2,8 @@ package middleware import "github.com/gin-gonic/gin" -func SearchLimit(num int64) func(ctx *gin.Context) { - fn := IpLimit(num) +func SearchLimit(num int64) (func(ctx *gin.Context), func(int64)) { + fn, reFn := IpLimit(num) return func(c *gin.Context) { if c.Query("s") != "" { fn(c) @@ -11,5 +11,5 @@ func SearchLimit(num int64) func(ctx *gin.Context) { c.Next() } - } + }, reFn } diff --git a/route/route.go b/route/route.go index 121b7d9..44cbbd2 100644 --- a/route/route.go +++ b/route/route.go @@ -64,7 +64,8 @@ func SetupRouter() (*gin.Engine, func()) { })) store := cookie.NewStore([]byte("secret")) 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("/p/category/:category", actions.Index) r.GET("/p/category/:category/page/:page", actions.Index) @@ -86,6 +87,7 @@ func SetupRouter() (*gin.Engine, func()) { reloadValidServerNameFn() c := config.Conf.Load() flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) + slRload(c.SingleIpSearchNum) } return r, fn }