From dd7e9f54405d7ea06cdfd1356b59f6953345faf0 Mon Sep 17 00:00:00 2001 From: jager Date: Tue, 18 Jan 2022 19:20:03 +0800 Subject: [PATCH] dirty --- cmd/idata/main.go | 11 +++++++++ conf.d/config.yaml | 3 +++ internal/api/api.go | 11 ++++++++- internal/apity/apity.go | 1 + internal/service/dirty/dirty.go | 40 +++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 internal/service/dirty/dirty.go diff --git a/cmd/idata/main.go b/cmd/idata/main.go index 827e7b3..440f0c2 100644 --- a/cmd/idata/main.go +++ b/cmd/idata/main.go @@ -20,6 +20,7 @@ import ( "github.com/jageros/hawox/mysql" "github.com/jageros/hawox/redis" "idata/internal/api" + "idata/internal/service/dirty" "idata/internal/service/soup" "idata/internal/service/user" "math/rand" @@ -43,6 +44,11 @@ func main() { datePath = "date.json" } + dictPath := flags.GetValStr("dict.path") + if dictPath == "" { + dictPath = "dict.txt" + } + err := astro.InitFromJsonFile(datePath) if err != nil { logx.Fatalf("astro init err: %v", err) @@ -53,6 +59,11 @@ func main() { logx.Fatalf("soup init err:%v", err) } + err = dirty.InitDirtyWord(dictPath) + if err != nil { + logx.Fatalf("dirty init err:%v", err) + } + redis.Initialize(ctx, func(rdb redis.RdbConfig) { rdb.SetAddrs(flags.Options.RedisAddrs) rdb.SetUsername(flags.Options.RedisUser) diff --git a/conf.d/config.yaml b/conf.d/config.yaml index cd8ebe9..965fd4a 100644 --- a/conf.d/config.yaml +++ b/conf.d/config.yaml @@ -36,3 +36,6 @@ soup: date: path: "conf.d/date.json" + +dict: + path: "conf.d/dict.txt" diff --git a/internal/api/api.go b/internal/api/api.go index f368c9b..c65c961 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -15,22 +15,31 @@ package api import ( "github.com/jageros/hawox/jwt" "idata/internal/service/almanac" + "idata/internal/service/dirty" "idata/internal/service/soup" "idata/internal/service/user" + "net/http" "github.com/gin-gonic/gin" ) func Registry(engine *gin.Engine) { + engine.GET("/my-ip", myIp) r := engine.Group("/haw") ur := r.Group("/user") ur.POST("/register", user.Register) ur.POST("/login", user.Login) ur.GET("/info", jwt.CheckToken, user.Info) ur.GET("/secret", jwt.CheckToken, user.Secret) - ur.GET("/open", jwt.CheckToken, user.OpenApi) + ur.GET("/openapi", jwt.CheckToken, user.OpenApi) apiR := r.Group("/api") apiR.GET("/soup", soup.GetRandSoup) apiR.GET("/almanac", almanac.Almanac) + apiR.GET("/dirty_word", dirty.IsDirtyWord) +} + +func myIp(ctx *gin.Context) { + addr := ctx.Request.RemoteAddr + ctx.String(http.StatusOK, addr) } diff --git a/internal/apity/apity.go b/internal/apity/apity.go index 4753ea7..319510e 100644 --- a/internal/apity/apity.go +++ b/internal/apity/apity.go @@ -21,6 +21,7 @@ const ( Weather // 2:天气 Obscenity // 3:脏词检测 Almanac // 4:黄历 + Dirty // 5:脏词检测 ) func Type(ty string) ApiType { diff --git a/internal/service/dirty/dirty.go b/internal/service/dirty/dirty.go new file mode 100644 index 0000000..39580b7 --- /dev/null +++ b/internal/service/dirty/dirty.go @@ -0,0 +1,40 @@ +/** + * @Author: jager + * @Email: lhj168os@gmail.com + * @File: dirty + * @Date: 2022/1/18 11:07 上午 + * @package: dirty + * @Version: v1.0.0 + * + * @Description: + * + */ + +package dirty + +import ( + "github.com/gin-gonic/gin" + "github.com/jageros/hawox/httpx" + "github.com/jageros/hawox/sensitive" + "idata/internal/apity" + "idata/internal/service/user" +) + +var filter *sensitive.Filter + +func InitDirtyWord(path string) error { + filter = sensitive.New() + return filter.LoadWordDict(path) +} + +func IsDirtyWord(ctx *gin.Context) { + if !user.Auth(ctx, apity.Dirty) { + return + } + word, ok := httpx.DecodeUrlVal(ctx, "word") + if !ok { + return + } + dirty, w := filter.Validate(word) + httpx.PkgMsgWrite(ctx, map[string]interface{}{"dirty": !dirty, "dirty_word": w}) +}