tokenizer
This commit is contained in:
38
keywords-filter/filter-server/interceptor/auth.go
Normal file
38
keywords-filter/filter-server/interceptor/auth.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package interceptor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"keywords-filter/pkg/config"
|
||||
"keywords-filter/pkg/zerror"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func UnaryAuthInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||||
if info.FullMethod != "/grpc.health.v1.Health/Check" {
|
||||
err = oauth2Valid(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
func oauth2Valid(ctx context.Context) error {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return zerror.NewByMsg("元数据获取失败")
|
||||
}
|
||||
authorization := md["authorization"]
|
||||
|
||||
if len(authorization) < 1 {
|
||||
return zerror.NewByMsg("元数据获取失败")
|
||||
}
|
||||
|
||||
token := strings.TrimPrefix(authorization[0], "Bearer ")
|
||||
cnf := config.GetConfig()
|
||||
if token != cnf.Server.AccessToken {
|
||||
return zerror.NewByMsg("鉴权失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
55
keywords-filter/filter-server/main.go
Normal file
55
keywords-filter/filter-server/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/health"
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
"keywords-filter/filter-server/interceptor"
|
||||
"keywords-filter/filter-server/server"
|
||||
"keywords-filter/pkg/config"
|
||||
"keywords-filter/pkg/filter"
|
||||
"keywords-filter/pkg/log"
|
||||
"keywords-filter/proto"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
configFile = flag.String("config", "dev.config.yaml", "")
|
||||
dictFile = flag.String("dict", "dict.txt", "")
|
||||
formatDict = flag.Bool("format", false, "")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if *formatDict {
|
||||
filter.OverwriteDict(*dictFile)
|
||||
return
|
||||
}
|
||||
|
||||
//初始化配置文件
|
||||
config.InitConfig(*configFile)
|
||||
cnf := config.GetConfig()
|
||||
//初始化日志
|
||||
log.SetLevel(cnf.Log.Level)
|
||||
log.SetOutput(log.GetRotateWriter(cnf.Log.LogPath))
|
||||
log.SetPrintCaller(true)
|
||||
//初始话filter
|
||||
filter.InitFilter(*dictFile)
|
||||
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cnf.Server.IP, cnf.Server.Port))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
s := grpc.NewServer(grpc.UnaryInterceptor(interceptor.UnaryAuthInterceptor))
|
||||
service := server.NewFilterService(filter.GetFilter())
|
||||
proto.RegisterFilterServer(s, service)
|
||||
|
||||
healthCheckSrv := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(s, healthCheckSrv)
|
||||
|
||||
if err = s.Serve(lis); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
32
keywords-filter/filter-server/server/server.go
Normal file
32
keywords-filter/filter-server/server/server.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"keywords-filter/pkg/filter"
|
||||
"keywords-filter/proto"
|
||||
)
|
||||
|
||||
type filterService struct {
|
||||
proto.UnimplementedFilterServer
|
||||
filter filter.IFilter
|
||||
}
|
||||
|
||||
func NewFilterService(filter filter.IFilter) proto.FilterServer {
|
||||
return &filterService{
|
||||
filter: filter,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *filterService) Validate(_ context.Context, in *proto.FilterReq) (*proto.ValidateRes, error) {
|
||||
ok, word := s.filter.Validate(in.Text)
|
||||
return &proto.ValidateRes{
|
||||
Ok: ok,
|
||||
Keyword: word,
|
||||
}, nil
|
||||
}
|
||||
func (s *filterService) FindAll(_ context.Context, in *proto.FilterReq) (*proto.FindAllRes, error) {
|
||||
words := s.filter.FindAll(in.Text)
|
||||
return &proto.FindAllRes{
|
||||
Keywords: words,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user