optimize code of config
This commit is contained in:
parent
3374eec047
commit
2126aa7b06
|
|
@ -44,10 +44,11 @@ var baseCurrentFunc = func(archorValue float64, args ...float64) float64 {
|
||||||
// SelectAnchorCalculateFuncAndParams define select anchor func and anchor calculate value by component type 、 anchor name and component data
|
// SelectAnchorCalculateFuncAndParams define select anchor func and anchor calculate value by component type 、 anchor name and component data
|
||||||
func SelectAnchorCalculateFuncAndParams(componentType int, anchorName string, componentData map[string]interface{}) (func(archorValue float64, args ...float64) float64, []float64) {
|
func SelectAnchorCalculateFuncAndParams(componentType int, anchorName string, componentData map[string]interface{}) (func(archorValue float64, args ...float64) float64, []float64) {
|
||||||
if componentType == constants.DemoType {
|
if componentType == constants.DemoType {
|
||||||
if anchorName == "voltage" {
|
switch anchorName {
|
||||||
|
case "voltage":
|
||||||
resistance := componentData["resistance"].(float64)
|
resistance := componentData["resistance"].(float64)
|
||||||
return baseVoltageFunc, []float64{resistance}
|
return baseVoltageFunc, []float64{resistance}
|
||||||
} else if anchorName == "current" {
|
case "current":
|
||||||
resistance := componentData["resistance"].(float64)
|
resistance := componentData["resistance"].(float64)
|
||||||
return baseCurrentFunc, []float64{resistance}
|
return baseCurrentFunc, []float64{resistance}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,8 @@ func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.L
|
||||||
func ParseDataIdentifierToken(ctx context.Context, tx *gorm.DB, identToken string) (model.IndentityTokenModelInterface, error) {
|
func ParseDataIdentifierToken(ctx context.Context, tx *gorm.DB, identToken string) (model.IndentityTokenModelInterface, error) {
|
||||||
identSlice := strings.Split(identToken, ".")
|
identSlice := strings.Split(identToken, ".")
|
||||||
identSliceLen := len(identSlice)
|
identSliceLen := len(identSlice)
|
||||||
if identSliceLen == 4 {
|
switch identSliceLen {
|
||||||
|
case 4:
|
||||||
// token1.token2.token3.token4.token7
|
// token1.token2.token3.token4.token7
|
||||||
shortIndentModel := &model.ShortIdentityTokenModel{
|
shortIndentModel := &model.ShortIdentityTokenModel{
|
||||||
GridTag: identSlice[0],
|
GridTag: identSlice[0],
|
||||||
|
|
@ -67,7 +68,7 @@ func ParseDataIdentifierToken(ctx context.Context, tx *gorm.DB, identToken strin
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return shortIndentModel, nil
|
return shortIndentModel, nil
|
||||||
} else if identSliceLen == 7 {
|
case 7:
|
||||||
// token1.token2.token3.token4.token5.token6.token7
|
// token1.token2.token3.token4.token5.token6.token7
|
||||||
longIndentModel := &model.LongIdentityTokenModel{
|
longIndentModel := &model.LongIdentityTokenModel{
|
||||||
GridTag: identSlice[0],
|
GridTag: identSlice[0],
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken, clientToken str
|
||||||
|
|
||||||
attrSlice := strings.Split(attrToken, ".")
|
attrSlice := strings.Split(attrToken, ".")
|
||||||
attrLen := len(attrSlice)
|
attrLen := len(attrSlice)
|
||||||
if attrLen == 4 {
|
switch attrLen {
|
||||||
|
case 4:
|
||||||
short := &model.ShortAttrInfo{
|
short := &model.ShortAttrInfo{
|
||||||
AttrGroupName: attrSlice[2],
|
AttrGroupName: attrSlice[2],
|
||||||
AttrKey: attrSlice[3],
|
AttrKey: attrSlice[3],
|
||||||
|
|
@ -35,7 +36,7 @@ func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken, clientToken str
|
||||||
}
|
}
|
||||||
short.AttrValue = attrValue
|
short.AttrValue = attrValue
|
||||||
return short, nil
|
return short, nil
|
||||||
} else if attrLen == 7 {
|
case 7:
|
||||||
long := &model.LongAttrInfo{
|
long := &model.LongAttrInfo{
|
||||||
AttrGroupName: attrSlice[5],
|
AttrGroupName: attrSlice[5],
|
||||||
AttrKey: attrSlice[6],
|
AttrKey: attrSlice[6],
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"modelRT/logger"
|
"modelRT/logger"
|
||||||
|
|
||||||
|
|
@ -27,17 +25,15 @@ func GetPostgresDBClient() *gorm.DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitPostgresDBInstance return instance of PostgresDB client
|
// InitPostgresDBInstance return instance of PostgresDB client
|
||||||
func InitPostgresDBInstance(ctx context.Context, PostgresDBURI string) *gorm.DB {
|
func InitPostgresDBInstance(PostgresDBURI string) *gorm.DB {
|
||||||
postgresOnce.Do(func() {
|
postgresOnce.Do(func() {
|
||||||
_globalPostgresClient = initPostgresDBClient(ctx, PostgresDBURI)
|
_globalPostgresClient = initPostgresDBClient(PostgresDBURI)
|
||||||
})
|
})
|
||||||
return _globalPostgresClient
|
return _globalPostgresClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// initPostgresDBClient return successfully initialized PostgresDB client
|
// initPostgresDBClient return successfully initialized PostgresDB client
|
||||||
func initPostgresDBClient(ctx context.Context, PostgresDBURI string) *gorm.DB {
|
func initPostgresDBClient(PostgresDBURI string) *gorm.DB {
|
||||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
db, err := gorm.Open(postgres.Open(PostgresDBURI), &gorm.Config{Logger: logger.NewGormLogger()})
|
db, err := gorm.Open(postgres.Open(PostgresDBURI), &gorm.Config{Logger: logger.NewGormLogger()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
||||||
21
main.go
21
main.go
|
|
@ -15,6 +15,7 @@ import (
|
||||||
|
|
||||||
"modelRT/alert"
|
"modelRT/alert"
|
||||||
"modelRT/config"
|
"modelRT/config"
|
||||||
|
"modelRT/constants"
|
||||||
"modelRT/database"
|
"modelRT/database"
|
||||||
"modelRT/diagram"
|
"modelRT/diagram"
|
||||||
"modelRT/logger"
|
"modelRT/logger"
|
||||||
|
|
@ -98,14 +99,14 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceToken, err := util.GenerateClientToken(hostName, modelRTConfig.ServiceConfig.ServiceName, modelRTConfig.ServiceConfig.SecretKey)
|
serviceToken, err := util.GenerateClientToken(hostName, modelRTConfig.ServiceName, modelRTConfig.SecretKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(ctx, "generate client token failed", "error", err)
|
logger.Error(ctx, "generate client token failed", "error", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// init postgresDBClient
|
// init postgresDBClient
|
||||||
postgresDBClient = database.InitPostgresDBInstance(ctx, modelRTConfig.PostgresDBURI)
|
postgresDBClient = database.InitPostgresDBInstance(modelRTConfig.PostgresDBURI)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
sqlDB, err := postgresDBClient.DB()
|
sqlDB, err := postgresDBClient.DB()
|
||||||
|
|
@ -127,13 +128,17 @@ func main() {
|
||||||
defer parsePool.Release()
|
defer parsePool.Release()
|
||||||
|
|
||||||
searchPool, err := util.NewRedigoPool(modelRTConfig.StorageRedisConfig)
|
searchPool, err := util.NewRedigoPool(modelRTConfig.StorageRedisConfig)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(ctx, "init redigo pool failed", "error", err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
defer searchPool.Close()
|
defer searchPool.Close()
|
||||||
model.InitAutocompleterWithPool(searchPool)
|
model.InitAutocompleterWithPool(searchPool)
|
||||||
|
|
||||||
storageClient := diagram.InitRedisClientInstance(modelRTConfig.StorageRedisConfig, *&modelRTConfig.ServiceConfig.DeployEnv)
|
storageClient := diagram.InitRedisClientInstance(modelRTConfig.StorageRedisConfig, modelRTConfig.DeployEnv)
|
||||||
defer storageClient.Close()
|
defer storageClient.Close()
|
||||||
|
|
||||||
lockerClient := locker.InitClientInstance(modelRTConfig.LockerRedisConfig, *&modelRTConfig.ServiceConfig.DeployEnv)
|
lockerClient := locker.InitClientInstance(modelRTConfig.LockerRedisConfig, modelRTConfig.DeployEnv)
|
||||||
defer lockerClient.Close()
|
defer lockerClient.Close()
|
||||||
|
|
||||||
// init anchor param ants pool
|
// init anchor param ants pool
|
||||||
|
|
@ -204,8 +209,10 @@ func main() {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// use release mode in productio
|
// use release mode in production
|
||||||
// gin.SetMode(gin.ReleaseMode)
|
if modelRTConfig.DeployEnv == constants.ProductionDeployMode {
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
}
|
||||||
engine := gin.New()
|
engine := gin.New()
|
||||||
router.RegisterRoutes(engine, serviceToken)
|
router.RegisterRoutes(engine, serviceToken)
|
||||||
|
|
||||||
|
|
@ -223,7 +230,7 @@ func main() {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
server := http.Server{
|
server := http.Server{
|
||||||
Addr: modelRTConfig.ServiceConfig.ServiceAddr,
|
Addr: modelRTConfig.ServiceAddr,
|
||||||
Handler: engine,
|
Handler: engine,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue