完善 中间件 reload

This commit is contained in:
xing 2022-11-16 11:09:25 +08:00
parent dc871c1488
commit f2dcd9f81e
4 changed files with 19 additions and 10 deletions

View File

@ -5,12 +5,15 @@ a simple front of WordPress build with golang.
#### 特色功能
- 缓存配置
- 多种缓存配置
- 添加评论或panic时发邮件通知包涵栈调用和请求信息
- 简单的流量限制中间件
- 除配置文件外将所有静态资源都打包到执行文件中
- 支持密码查看且cookie信息可被php版所验证
- 支持rss2订阅
- 热更新配置、清空缓存
- kill -SIGUSR1 PID 更新配置和清空缓存
- kill -SIGUSR2 PID 清空缓存
#### 其它
用的gin框架和sqlx,在外面封装了层查询的方法。后台可以设置的比较少,大部分设置还没打通。

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}