Compare commits

..

20 Commits

Author SHA1 Message Date
yanweidong fe3e02370c fix,upd module 2024-12-03 18:53:11 +08:00
yanweidong 3dfadd7a30 dev 2024-12-02 11:41:17 +08:00
yanweidong 175848c47e dev 2024-12-02 11:28:37 +08:00
yanweidong 839d85d493 add err code 2024-10-15 12:53:06 +08:00
yanweidong 3b5719bc91 更新 types/etcd.go 2024-10-03 20:52:35 +08:00
yanweidong ec2bf11981 更新 types/etcd.go 2024-10-03 20:47:47 +08:00
yanweidong 9bb2a1d730 更新 env/env.go 2024-10-03 20:39:18 +08:00
yanweidong d645a49c97 更新 types/etcd.go 2024-10-03 20:35:48 +08:00
yanweidong a8efe29c7f fix env prefix die 2024-09-29 11:58:44 +08:00
yanweidong 3d609bfba8 remove nanoid,uuid,replace to ulid 2024-09-20 14:46:46 +08:00
yanweidong 8537320513 dev 2024-09-20 14:45:34 +08:00
david 0be9490840 dev 2024-09-17 21:57:37 +08:00
david 84d6a9d034 dev 2024-09-17 21:27:12 +08:00
david 59118cf8f2 dev 2024-09-17 18:54:13 +08:00
yanweidong aada90ea78 fix types.db 2024-07-02 11:01:37 +08:00
yanweidong 28ad46d403 fix jwt 2024-06-28 11:29:46 +08:00
david 31c602dcc5 Merge branch 'master' of https://git.apinb.com/bsm-sdk/engine 2024-06-24 16:24:38 +08:00
david 52e78a321e add vars.Status 2024-06-24 16:24:33 +08:00
yanweidong 6cac60f15e add ULID 2024-06-14 17:23:24 +08:00
yanweidong 2d40fc38ce add exception code 2024-06-05 15:46:57 +08:00
15 changed files with 112 additions and 60 deletions

30
database/sql/ext.go Normal file
View File

@ -0,0 +1,30 @@
package sql
import "strings"
// key,value To like sql
func Like(key, val string) string {
if val == "" {
return ""
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
return key + " LIKE '%" + val + "%'"
}
// map strings to like sqls
func Likes(in map[string]string) string {
var ar []string
for key, val := range in {
sql := Like(key, val)
if sql != "" {
ar = append(ar, sql)
}
}
if len(ar) == 0 {
return ""
}
return strings.Join(ar, " AND ")
}

View File

@ -24,14 +24,14 @@ func NewPostgreSql(dsn string, options *types.SqlOptions) (*gorm.DB, error) {
}
//控制台和文件同时输出日志
var newLogger = setLogger(vars.ServiceKey, options.LogStdout)
//var newLogger = setLogger(vars.ServiceKey, options.LogStdout)
gormDb, err := gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
// PreferSimpleProtocol: true, disables implicit prepared statement usage
}), &gorm.Config{
Logger: newLogger,
//Logger:newLogger,
DisableForeignKeyConstraintWhenMigrating: true,
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`

View File

@ -21,8 +21,8 @@ var (
JwtSecretLen int
)
func New() {
JwtSecret = []byte(env.MeshEnv.JwtSecretKey)
func New(token string) {
JwtSecret = []byte(token)
JwtSecretLen = len(env.MeshEnv.JwtSecretKey)
}
@ -34,6 +34,7 @@ func GenerateTokenAes(id uint, identity, client, role string, owner any, extend
claims := types.JwtClaims{
ID: id,
Identity: identity,
Client: client,
Extend: extend,
Owner: owner,
Role: role,

25
env/env.go vendored
View File

@ -14,11 +14,32 @@ var MeshEnv *types.MeshEnv = nil
func NewEnv() *types.MeshEnv {
if MeshEnv == nil {
MeshEnv = &types.MeshEnv{
Workspace: GetEnvDefault("BlocksMesh_Workspace", "bsm"),
Prefix: GetEnvDefault("BlocksMesh_Prefix", utils.GetCurrentPath()),
Workspace: GetEnvDefault("BlocksMesh_Workspace", "def"),
JwtSecretKey: GetEnvDefault("BlocksMesh_JwtSecretKey", "Cblocksmesh2022C"),
RuntimeMode: strings.ToLower(GetEnvDefault("BlocksMesh_RuntimeMode", "dev")),
}
if MeshEnv.RuntimeMode == "dev" {
MeshEnv.Prefix = GetEnvDefault("BlocksMesh_Prefix", utils.GetCurrentPath())
} else {
MeshEnv.Prefix = GetEnvDefault("BlocksMesh_Prefix", "/usr/local/bsm")
}
}
return MeshEnv
}
// get system base env.
func NewBaseEnv() *types.MeshEnv {
if MeshEnv == nil {
MeshEnv = &types.MeshEnv{
RuntimeMode: strings.ToLower(GetEnvDefault("BlocksMesh_RuntimeMode", "dev")),
}
if MeshEnv.RuntimeMode == "dev" {
MeshEnv.Prefix = GetEnvDefault("BlocksMesh_Prefix", utils.GetCurrentPath())
} else {
MeshEnv.Prefix = GetEnvDefault("BlocksMesh_Prefix", "/usr/local/bsm")
}
}
return MeshEnv
}

View File

@ -14,4 +14,5 @@ var (
ErrAuthClient = Errorf(209, "Auth Token Client Not Passed")
ErrJsonDecode = Errorf(210, "Auth JSON Decode Error")
ErrJsonEncode = Errorf(211, "Auth JSON Encode Error")
ErrAccountNotFound = Errorf(202, "Account Not Found")
)

View File

@ -30,7 +30,9 @@ var (
ErrJSONUnmarshal = Errorf(18, "Unmarshal JSON")
ErrPasswd = Errorf(19, "Password Error")
ErrSmsCode = Errorf(20, "SMS Code Invalid")
ErrSmsCode = Errorf(20, "SMS Code Invalid")
ErrIdArgument = Errorf(30, "ID Invalid Argument")
ErrIdentityArgument = Errorf(31, "Identity Invalid Argument")
// coustom error status
)

2
go.mod
View File

@ -1,3 +1,3 @@
module git.apinb.com/bsm-sdk/engine
go 1.22
go 1.23.3

View File

@ -2,9 +2,9 @@ package service
import (
"context"
"encoding/json"
"strings"
"git.apinb.com/bsm-sdk/engine/encipher"
"git.apinb.com/bsm-sdk/engine/exception"
"git.apinb.com/bsm-sdk/engine/types"
"google.golang.org/grpc/metadata"
@ -23,14 +23,15 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*types.JwtClaims, er
return nil, exception.ErrAuthNotFound
}
var Authorizations []string = md.Get("authorization")
var Authorizations []string = md.Get("authorization_claims")
if len(Authorizations) == 0 || Authorizations[0] == "" {
return nil, exception.ErrAuthNotFound
}
claims, err := encipher.ParseTokenAes(Authorizations[0])
var claims types.JwtClaims
err := json.Unmarshal([]byte(Authorizations[0]), &claims)
if err != nil {
return nil, err
return nil, exception.ErrAuthParseFail
}
if opts != nil {
@ -39,6 +40,6 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*types.JwtClaims, er
}
}
return claims, nil
return &claims, nil
}

View File

@ -6,7 +6,6 @@ import (
"git.apinb.com/bsm-sdk/engine/cmd"
"git.apinb.com/bsm-sdk/engine/configure"
"git.apinb.com/bsm-sdk/engine/encipher"
"git.apinb.com/bsm-sdk/engine/env"
"git.apinb.com/bsm-sdk/engine/print"
"git.apinb.com/bsm-sdk/engine/vars"
@ -23,8 +22,6 @@ func Register(srvKey string, cfg any) {
// open side cmd.
cmd.NewCmd()
encipher.New()
print.Info("[Blocks Service] %s Starting...", vars.ServiceKey)
// set config args.
@ -37,7 +34,7 @@ func Register(srvKey string, cfg any) {
// get config file.
cf := fmt.Sprintf("%s_%s.yaml", srvKey, env.MeshEnv.RuntimeMode)
cf = filepath.Join(env.MeshEnv.Prefix, "etc", cf)
print.Info("[Blocks Service] %s Config File: %s", vars.ServiceKey, cf)
configure.LoadYamlFile(cf, args, cfg)
return

View File

@ -18,48 +18,54 @@ type (
Debug bool
}
// standard ID,Identity definition.
Std_IDIdentity struct {
ID uint `gorm:"primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为ULID
}
// standard ID,Created,Updated,Deleted definition.
Std_IICUDS struct {
ID uint `gorm:"primarykey;"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;"` // 唯一标识24位NanoID,36位为UUID
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index";`
Status int8 `gorm:"default:0;index;"` // 状态默认为0-1禁止1为正常
ID uint `gorm:"primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为ULID
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
Status int8 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
}
// standard ID,Identity,Created,Updated,Deleted,Status definition.
Std_ICUD struct {
ID uint `gorm:"primarykey;"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
ID uint `gorm:"primarykey;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
}
// standard ID,Created definition.
Std_IdCreated struct {
ID uint `gorm:"primarykey;"`
CreatedAt time.Time
ID uint `gorm:"primarykey;" json:"id"`
CreatedAt time.Time `json:"created_at"`
}
// standard PassportID,PassportIdentity definition.
Std_Passport struct {
PassportID uint `gorm:"column:passport_id;Index;"`
PassportIdentity string `gorm:"column:passport_identity;type:varchar(36);Index;"` // 用户唯一标识24位NanoID,36位为UUID
PassportID uint `gorm:"column:passport_id;Index;" json:"passport_id"`
PassportIdentity string `gorm:"column:passport_identity;type:varchar(36);Index;" json:"passport_identity"` // 用户唯一标识24位NanoID,36位为ULID
}
// standard ID definition.
Std_ID struct {
ID uint `gorm:"primarykey;"`
ID uint `gorm:"primarykey;" json:"id"`
}
// standard Identity definition.
Std_Identity struct {
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;"` // 唯一标识24位NanoID,36位为UUID
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为ULID
}
// standard Status definition.
Std_Status struct {
Status int8 `gorm:"default:0;index;"` // 状态默认为0-1禁止1为正常
Status int8 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
}
)

View File

@ -9,6 +9,7 @@ type JwtClaims struct {
ID uint `json:"id"`
Identity string `json:"identity"`
Extend map[string]string `json:"extend"`
Client string `json:"client"`
Owner any `json:"owner"`
Role string `json:"role"`
ExpiresAt int64 `json:"exp"`

View File

@ -1,12 +1,12 @@
package types
type Etcd struct {
Endpoints []string `json:"endpoints"`
Tls EtcdTls `json:",optional"`
Endpoints []string `json:"Endpoints"`
Tls EtcdTls `json:"Tls,optional"`
}
type EtcdTls struct {
Ca string `json:"ca,optional"`
Cert string `json:"cert,optional"`
CertKey string `json:"cert_key,optional" `
Ca string `json:"Ca,optional"`
Cert string `json:"Cert,optional"`
CertKey string `json:"CertKey,optional" `
}

View File

@ -1,26 +1,10 @@
package utils
import (
"strconv"
"github.com/google/uuid"
"github.com/jaevor/go-nanoid"
ulid "github.com/oklog/ulid/v2"
)
func NanoID() string {
nanoid, _ := nanoid.CustomASCII("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 21)
return nanoid()
}
func NanoIDInt() (id int64, err error) {
decenaryID, err := nanoid.CustomASCII("0123456789", 18)
if err != nil {
return
}
id, err = strconv.ParseInt(decenaryID(), 10, 64)
return
}
func UUID() string {
return uuid.NewString()
// remove nanoid,uuid,replace to ulid
func ULID() string {
return ulid.Make().String()
}

View File

@ -99,7 +99,7 @@ func HttpPost(url string, header map[string]string, data []byte) ([]byte, error)
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
request.Header.Set("Request-Id", NanoID())
request.Header.Set("Request-Id", ULID())
for key, val := range header {
request.Header.Set(key, val)

8
vars/status.go Normal file
View File

@ -0,0 +1,8 @@
package vars
const (
// NormalStatus .
NormalStatus = 1
// DisabledStatus .
DisabledStatus = -1
)