```
refactor(print): 将 print 包重命名为 printer 并更新所有引用 将项目中的 print 包统一重命名为 printer,以避免与标准库或第三方库产生命名冲突。同时, 更新了所有相关模块对该包的引用,确保功能一致性和代码可维护性。 涉及文件包括: - conf/new.go - infra/service.go - service/register.go - service/service.go - with/databases.go - with/etcd.go - with/memory.go - with/redis.go 此外,删除了以下已废弃或未使用的代码文件: - cmd/cmd.go - data/map_float.go - data/map_string.go - oplog/new.go - oplog/types.go - print/print.go - utils/ext.go ```
This commit is contained in:
parent
21716c4340
commit
25386cf0e1
|
@ -1,34 +1,32 @@
|
|||
package data
|
||||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// Cache
|
||||
CacheMapFloat *MapFloat
|
||||
// sync map
|
||||
MapFloat *mapFloat
|
||||
)
|
||||
|
||||
// lock
|
||||
type MapFloat struct {
|
||||
type mapFloat struct {
|
||||
sync.RWMutex
|
||||
Data map[string]float64
|
||||
}
|
||||
|
||||
func NewMapFloat() *MapFloat {
|
||||
return &MapFloat{
|
||||
func NewMapFloat() *mapFloat {
|
||||
return &mapFloat{
|
||||
Data: make(map[string]float64),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MapFloat) All() map[string]float64 {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
func (c *mapFloat) Set(key string, val float64) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *MapFloat) Get(key string) float64 {
|
||||
func (c *mapFloat) Get(key string) float64 {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
|
@ -40,13 +38,14 @@ func (c *MapFloat) Get(key string) float64 {
|
|||
return vals
|
||||
}
|
||||
|
||||
func (c *MapFloat) Set(key string, val float64) {
|
||||
func (c *mapFloat) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *MapFloat) Keys() (keys []string) {
|
||||
func (c *mapFloat) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
|
@ -56,3 +55,10 @@ func (c *MapFloat) Keys() (keys []string) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *mapFloat) All() map[string]float64 {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// sync map
|
||||
MapInt *mapInt
|
||||
)
|
||||
|
||||
// lock
|
||||
type mapInt struct {
|
||||
sync.RWMutex
|
||||
Data map[string]int
|
||||
}
|
||||
|
||||
func NewMapInt() *mapInt {
|
||||
return &mapInt{
|
||||
Data: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mapInt) Set(key string, val int) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *mapInt) Get(key string) int {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *mapInt) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *mapInt) All() map[string]int {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
}
|
||||
func (c *mapInt) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// sync map
|
||||
MapString *mapString
|
||||
)
|
||||
|
||||
// lock
|
||||
type mapString struct {
|
||||
sync.RWMutex
|
||||
Data map[string]string
|
||||
}
|
||||
|
||||
func NewMapString() *mapString {
|
||||
return &mapString{
|
||||
Data: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mapString) Set(key, val string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
func (c *mapString) Get(key string) string {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *mapString) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *mapString) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *mapString) All() map[string]string {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
}
|
12
conf/new.go
12
conf/new.go
|
@ -11,7 +11,7 @@ import (
|
|||
"math/rand/v2"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
|
@ -36,8 +36,8 @@ func New(srvKey string, cfg any) {
|
|||
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
||||
}
|
||||
|
||||
print.Info("[BSM - %s] Config File: %s", srvKey, cfp)
|
||||
print.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
|
||||
printer.Info("[BSM - %s] Config File: %s", srvKey, cfp)
|
||||
printer.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
|
||||
|
||||
// 读取配置文件内容
|
||||
yamlFile, err := os.ReadFile(cfp)
|
||||
|
@ -69,9 +69,9 @@ func NotNil(values ...string) {
|
|||
}
|
||||
|
||||
func PrintInfo(addr string) {
|
||||
print.Success("[BSM - %s] Config Check Success.", vars.ServiceKey)
|
||||
print.Info("[BSM - %s] Service Name: %s", vars.ServiceKey, vars.ServiceKey)
|
||||
print.Info("[BSM - %s] Runtime Mode: %s", vars.ServiceKey, env.Runtime.Mode)
|
||||
printer.Success("[BSM - %s] Config Check Success.", vars.ServiceKey)
|
||||
printer.Info("[BSM - %s] Service Name: %s", vars.ServiceKey, vars.ServiceKey)
|
||||
printer.Info("[BSM - %s] Runtime Mode: %s", vars.ServiceKey, env.Runtime.Mode)
|
||||
}
|
||||
|
||||
func CheckPort(port string) string {
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// Cache
|
||||
CacheMapString *MapString
|
||||
)
|
||||
|
||||
// lock
|
||||
type MapString struct {
|
||||
sync.RWMutex
|
||||
Data map[string]string
|
||||
}
|
||||
|
||||
func NewMapString() *MapString {
|
||||
return &MapString{
|
||||
Data: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MapString) Get(key string) string {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *MapString) Set(key, val string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *MapString) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
|
@ -1,4 +1,10 @@
|
|||
package oplog
|
||||
package infra
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
type LogItem struct {
|
||||
OpID uint `json:"op_id"`
|
||||
|
@ -22,3 +28,10 @@ var (
|
|||
Type_Other string = "other"
|
||||
Type_Create string = "create"
|
||||
)
|
||||
|
||||
func NewLogs(endpoint string, data []*LogItem) {
|
||||
jsonBytes, _ := json.Marshal(data)
|
||||
|
||||
go utils.HttpPost(endpoint, nil, jsonBytes)
|
||||
|
||||
}
|
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
@ -37,10 +37,10 @@ func (s *service) Register(cli *clientv3.Client, serviceName string, port string
|
|||
return err
|
||||
}
|
||||
|
||||
print.Info("[BSM Register] Service Key: %s", key)
|
||||
print.Info("[BSM Register] Service Val: %s", serviceAddr)
|
||||
printer.Info("[BSM Register] Service Key: %s", key)
|
||||
printer.Info("[BSM Register] Service Val: %s", serviceAddr)
|
||||
|
||||
print.Success("[BSM Register] Service Register Complete.")
|
||||
printer.Success("[BSM Register] Service Register Complete.")
|
||||
|
||||
go func() {
|
||||
for keepAliveResp := range keepAliveChan {
|
||||
|
|
14
oplog/new.go
14
oplog/new.go
|
@ -1,14 +0,0 @@
|
|||
package oplog
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
func New(endpoint string, data []*LogItem) {
|
||||
jsonBytes, _ := json.Marshal(data)
|
||||
|
||||
go utils.HttpPost(endpoint, nil, jsonBytes)
|
||||
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package print
|
||||
package printer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -36,3 +38,11 @@ func Error(format string, a ...interface{}) {
|
|||
message := fmt.Sprintf("\033[31m[Error] "+format+"\033[0m\n", a...)
|
||||
logger.Print(message)
|
||||
}
|
||||
|
||||
func Json(v any) {
|
||||
jsonBy, _ := json.Marshal(v)
|
||||
var out bytes.Buffer
|
||||
json.Indent(&out, jsonBy, "", "\t")
|
||||
out.WriteTo(os.Stdout)
|
||||
fmt.Printf("\n")
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -5,7 +5,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
@ -96,7 +96,7 @@ func (s *ServiceRegister) SetAnonymous(key string, urls []string) {
|
|||
// remove reppeat, clear service all anonymous uri.
|
||||
anonymous, err := s.cli.Get(context.Background(), key)
|
||||
if err != nil {
|
||||
print.Error("[BSM Register] Get Anonymous Fail: %v", err)
|
||||
printer.Error("[BSM Register] Get Anonymous Fail: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -113,9 +113,9 @@ func (s *ServiceRegister) SetAnonymous(key string, urls []string) {
|
|||
_, err = s.cli.Put(context.Background(), key, newAnonymous)
|
||||
|
||||
if err != nil {
|
||||
print.Error("[BSM Register] Anonymous Fail.")
|
||||
printer.Error("[BSM Register] Anonymous Fail.")
|
||||
} else {
|
||||
print.Info("[BSM Register] Anonymous: %s", newAnonymous)
|
||||
printer.Info("[BSM Register] Anonymous: %s", newAnonymous)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"google.golang.org/grpc"
|
||||
|
@ -48,15 +48,15 @@ func Addr(ip string, port int) string {
|
|||
}
|
||||
|
||||
func (s *Service) Start() {
|
||||
print.Info("[BSM - %s] Service Starting ...", vars.ServiceKey)
|
||||
printer.Info("[BSM - %s] Service Starting ...", vars.ServiceKey)
|
||||
|
||||
// register to etcd.
|
||||
if s.Opts.MsConf != nil && s.Opts.MsConf.Enable {
|
||||
if s.Opts.EtcdClient == nil {
|
||||
print.Error("[BSM Register] Etcd Client is nil.")
|
||||
printer.Error("[BSM Register] Etcd Client is nil.")
|
||||
os.Exit(1)
|
||||
}
|
||||
print.Info("[BSM - %s] Registering Service to Etcd ...", vars.ServiceKey)
|
||||
printer.Info("[BSM - %s] Registering Service to Etcd ...", vars.ServiceKey)
|
||||
// get methods
|
||||
methods := FoundGrpcMethods(s.GrpcSrv)
|
||||
|
||||
|
@ -88,13 +88,13 @@ func (s *Service) Start() {
|
|||
panic(err)
|
||||
}
|
||||
}()
|
||||
print.Success("[BSM - %s] Grpc %s Runing Success !", vars.ServiceKey, s.Opts.Addr)
|
||||
printer.Success("[BSM - %s] Grpc %s Runing Success !", vars.ServiceKey, s.Opts.Addr)
|
||||
|
||||
if s.Opts.GatewayConf != nil && s.Opts.GatewayConf.Enable {
|
||||
addr := Addr("0.0.0.0", s.Opts.GatewayConf.Port)
|
||||
go s.Gateway(s.Opts.Addr, addr)
|
||||
|
||||
print.Success("[BSM - %s] Http %s Runing Success!", vars.ServiceKey, addr)
|
||||
printer.Success("[BSM - %s] Http %s Runing Success!", vars.ServiceKey, addr)
|
||||
}
|
||||
|
||||
select {}
|
||||
|
@ -111,7 +111,7 @@ func (s *Service) Gateway(grpcAddr string, httpAddr string) {
|
|||
func (s *Service) Use(initFunc func() error) {
|
||||
err := (initFunc)()
|
||||
if err != nil {
|
||||
print.Error(err.Error())
|
||||
printer.Error(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
12
utils/ext.go
12
utils/ext.go
|
@ -1,10 +1,6 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
@ -37,11 +33,3 @@ func ParseParams(in map[string]string) map[string]interface{} {
|
|||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func PrintJson(v any) {
|
||||
jsonBy, _ := json.Marshal(v)
|
||||
var out bytes.Buffer
|
||||
json.Indent(&out, jsonBy, "", "\t")
|
||||
out.WriteTo(os.Stdout)
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package with
|
|||
import (
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
|
@ -15,12 +15,12 @@ func Databases(cfg *conf.DBConf, opts *types.SqlOptions) *gorm.DB {
|
|||
}
|
||||
|
||||
// print inform.
|
||||
print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
||||
printer.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
||||
|
||||
var err error
|
||||
db, err := database.NewDatabase(cfg.Driver, cfg.Source, opts)
|
||||
if err != nil {
|
||||
print.Error("Database Init Failed !")
|
||||
printer.Error("Database Init Failed !")
|
||||
panic(err)
|
||||
}
|
||||
return db
|
||||
|
|
10
with/etcd.go
10
with/etcd.go
|
@ -5,13 +5,13 @@ import (
|
|||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"go.etcd.io/etcd/client/pkg/v3/transport"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
func Etcd(cfg *conf.EtcdConf) (cli *clientv3.Client){
|
||||
func Etcd(cfg *conf.EtcdConf) (cli *clientv3.Client) {
|
||||
if cfg == nil || len(cfg.Endpoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func Etcd(cfg *conf.EtcdConf) (cli *clientv3.Client){
|
|||
}
|
||||
tlsConfig, err := tlsInfo.ClientConfig()
|
||||
if err != nil {
|
||||
print.Error(errcode.ErrEtcd.Error())
|
||||
printer.Error(errcode.ErrEtcd.Error())
|
||||
panic(err)
|
||||
}
|
||||
etcdCfg.TLS = tlsConfig
|
||||
|
@ -44,11 +44,11 @@ func Etcd(cfg *conf.EtcdConf) (cli *clientv3.Client){
|
|||
var err error
|
||||
cli, err = clientv3.New(etcdCfg)
|
||||
if err != nil {
|
||||
print.Error(errcode.ErrEtcd.Error())
|
||||
printer.Error(errcode.ErrEtcd.Error())
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// print inform.
|
||||
print.Info("[BSM - %s] Service Center: %v", vars.ServiceKey, cfg.Endpoints)
|
||||
printer.Info("[BSM - %s] Service Center: %v", vars.ServiceKey, cfg.Endpoints)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"github.com/allegro/bigcache/v3"
|
||||
)
|
||||
|
@ -27,10 +27,10 @@ func Memory(opts *bigcache.Config) (cli *bigcache.BigCache) {
|
|||
var err error
|
||||
cli, err = bigcache.New(context.Background(), *opts)
|
||||
if err != nil {
|
||||
print.Error("Memory Cache Fatal Error")
|
||||
printer.Error("Memory Cache Fatal Error")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
print.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
|
||||
printer.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package with
|
|||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
|
@ -11,7 +11,7 @@ func RedisCache(cfg string) (cli *redis.RedisClient) {
|
|||
cli = redis.New(cfg, vars.ServiceKey)
|
||||
|
||||
// print inform.
|
||||
print.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, cfg, cli.DB)
|
||||
printer.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, cfg, cli.DB)
|
||||
}
|
||||
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue