feat 初始化
This commit is contained in:
21
module/cos.go
Normal file
21
module/cos.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/service-common/config"
|
||||
"git.hlsq.asia/alex/service-common/cos"
|
||||
)
|
||||
|
||||
// Cos 对象存储模块
|
||||
type Cos struct {
|
||||
DefaultModule
|
||||
Cfg map[string]*config.CosConfig
|
||||
}
|
||||
|
||||
func (m *Cos) Init() error {
|
||||
if cfg, ok := m.Cfg["tencent"]; ok {
|
||||
if err := cos.InitTencent(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
10
module/db.go
10
module/db.go
@@ -4,6 +4,7 @@ import (
|
||||
"git.hlsq.asia/alex/service-common/config"
|
||||
"git.hlsq.asia/alex/service-common/db/etcd"
|
||||
"git.hlsq.asia/alex/service-common/db/kafka"
|
||||
"git.hlsq.asia/alex/service-common/db/mongo"
|
||||
"git.hlsq.asia/alex/service-common/db/mysql"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
@@ -41,6 +42,12 @@ func (m *DB) Init() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// MONGO
|
||||
if m.Cfg.Mongo != nil {
|
||||
if err := mongo.Init(m.Cfg.Mongo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -57,5 +64,8 @@ func (m *DB) Stop() error {
|
||||
if err := kafka.Close(); err != nil {
|
||||
log.Errorf("close kafka failed: %v", err)
|
||||
}
|
||||
if err := mongo.Close(); err != nil {
|
||||
log.Errorf("close mongo failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
155
module/sensitive.go
Normal file
155
module/sensitive.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/alex/service-common/config"
|
||||
"git.hlsq.asia/alex/service-common/cos"
|
||||
)
|
||||
|
||||
var SensitiveWordIns *SensitiveWord
|
||||
|
||||
type SensitiveWord struct {
|
||||
DefaultModule
|
||||
cfg *config.SensitiveWordConfig
|
||||
}
|
||||
|
||||
func NewSensitiveWord(cfg *config.SensitiveWordConfig) Module {
|
||||
SensitiveWordIns = &SensitiveWord{
|
||||
cfg: cfg,
|
||||
}
|
||||
return SensitiveWordIns
|
||||
}
|
||||
|
||||
func (m *SensitiveWord) Init() error {
|
||||
if m.cfg == nil {
|
||||
//下载文件
|
||||
contentBytes, err := cos.DownloadSimple(context.Background(), "server_file/sensitive_words.txt", "sensitive-word.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var sensitiveWords map[string]struct{}
|
||||
err = json.Unmarshal(contentBytes, &sensitiveWords)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensitiveWordsList := make([]string, 0, len(sensitiveWords))
|
||||
for word := range sensitiveWords {
|
||||
sensitiveWordsList = append(sensitiveWordsList, word)
|
||||
}
|
||||
sensitiveTrie = newTrie(sensitiveWordsList)
|
||||
}
|
||||
// todo 根据配置文件
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainsSensitiveWords 判断是否包含敏感词
|
||||
func (m *SensitiveWord) ContainsSensitiveWords(text string) bool {
|
||||
return sensitiveTrie.containsSensitiveWords(text)
|
||||
}
|
||||
|
||||
// FilterSensitiveWords 过滤敏感词
|
||||
func (m *SensitiveWord) FilterSensitiveWords(text string) string {
|
||||
return sensitiveTrie.filterSensitiveWords(text)
|
||||
}
|
||||
|
||||
var sensitiveTrie *Trie
|
||||
|
||||
type trieNode struct {
|
||||
children map[rune]*trieNode
|
||||
isEnd bool
|
||||
}
|
||||
|
||||
type Trie struct {
|
||||
root *trieNode
|
||||
}
|
||||
|
||||
func newTrie(worlds []string) *Trie {
|
||||
t := &Trie{
|
||||
root: &trieNode{
|
||||
children: make(map[rune]*trieNode),
|
||||
isEnd: false,
|
||||
},
|
||||
}
|
||||
for _, str := range worlds {
|
||||
t.insert(str)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
const replace = '*'
|
||||
|
||||
func (sensitiveTrie *Trie) insert(word string) {
|
||||
node := sensitiveTrie.root
|
||||
for _, c := range word {
|
||||
if _, ok := node.children[c]; !ok {
|
||||
node.children[c] = &trieNode{
|
||||
children: make(map[rune]*trieNode),
|
||||
isEnd: false,
|
||||
}
|
||||
}
|
||||
node = node.children[c]
|
||||
}
|
||||
node.isEnd = true
|
||||
}
|
||||
|
||||
func (sensitiveTrie *Trie) containsSensitiveWords(text string) bool {
|
||||
if len(text) == 0 {
|
||||
return false
|
||||
}
|
||||
length := len([]rune(text))
|
||||
runes := make([]rune, length)
|
||||
index := 0
|
||||
for _, char := range text {
|
||||
runes[index] = char
|
||||
index++
|
||||
}
|
||||
for pointIndex := 0; pointIndex < length; pointIndex++ {
|
||||
//判断
|
||||
node := sensitiveTrie.root
|
||||
for currentIndex := pointIndex; currentIndex < length; currentIndex++ {
|
||||
tn := node.children[runes[currentIndex]]
|
||||
if tn == nil {
|
||||
break
|
||||
}
|
||||
if tn.isEnd {
|
||||
return true
|
||||
}
|
||||
node = tn
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (sensitiveTrie *Trie) filterSensitiveWords(text string) string {
|
||||
if len(text) == 0 {
|
||||
return text
|
||||
}
|
||||
length := len([]rune(text))
|
||||
runes := make([]rune, length)
|
||||
index := 0
|
||||
for _, char := range text {
|
||||
runes[index] = char
|
||||
index++
|
||||
}
|
||||
for pointIndex := 0; pointIndex < length; pointIndex++ {
|
||||
//判断
|
||||
node := sensitiveTrie.root
|
||||
for currentIndex := pointIndex; currentIndex < length; currentIndex++ {
|
||||
tn := node.children[runes[currentIndex]]
|
||||
if tn == nil {
|
||||
break
|
||||
}
|
||||
if tn.isEnd {
|
||||
//替换字符
|
||||
for i := pointIndex; i < currentIndex+1; i++ {
|
||||
runes[i] = replace
|
||||
}
|
||||
pointIndex = currentIndex
|
||||
break
|
||||
}
|
||||
node = tn
|
||||
}
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
25
module/server_status.go
Normal file
25
module/server_status.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/service-common/server"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ServerStatus struct {
|
||||
DefaultModule
|
||||
}
|
||||
|
||||
func (s *ServerStatus) Init() error {
|
||||
server.InitStatus()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServerStatus) Start(ready *sync.WaitGroup) error {
|
||||
server.ListenStatus(ready)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServerStatus) Stop() error {
|
||||
server.CloseStatus()
|
||||
return nil
|
||||
}
|
||||
25
module/server_whitelist.go
Normal file
25
module/server_whitelist.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/service-common/server"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ServerWhitelist struct {
|
||||
DefaultModule
|
||||
}
|
||||
|
||||
func (s *ServerWhitelist) Init() error {
|
||||
server.InitWhitelist()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServerWhitelist) Start(ready *sync.WaitGroup) error {
|
||||
server.ListenWhitelist(ready)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServerWhitelist) Stop() error {
|
||||
server.CloseWhitelist()
|
||||
return nil
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.hlsq.asia/alex/service-common/db/etcd"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"go.etcd.io/etcd/client/v3/concurrency"
|
||||
@@ -25,7 +24,6 @@ func (m *Snowflake) Init() error {
|
||||
}
|
||||
m.snowflakeSession = session
|
||||
utils.InitSnowflake(node)
|
||||
log.Infof("snowflake node: %v", node)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,7 +39,7 @@ func acquire(ctx context.Context, min, max int) (int64, *concurrency.Session, er
|
||||
}
|
||||
|
||||
for _, n := range nums {
|
||||
key := fmt.Sprintf("node/num/%d", n)
|
||||
key := fmt.Sprintf("/node/num/%d", n)
|
||||
|
||||
session, err := concurrency.NewSession(
|
||||
etcd.GetClient().Raw(),
|
||||
|
||||
@@ -29,13 +29,17 @@ func (m *Tracer) Init() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
samplingRate := 1.0
|
||||
if 0 <= m.Cfg.Jaeger.SamplingRate && m.Cfg.Jaeger.SamplingRate <= 1 {
|
||||
samplingRate = m.Cfg.Jaeger.SamplingRate
|
||||
}
|
||||
m.tp = sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exporter),
|
||||
sdktrace.WithResource(resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceNameKey.String(m.ServiceName),
|
||||
)),
|
||||
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(samplingRate))),
|
||||
)
|
||||
otel.SetTracerProvider(m.tp)
|
||||
otel.SetTextMapPropagator(propagation.TraceContext{})
|
||||
|
||||
Reference in New Issue
Block a user