Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad505b8f3c |
No files matched your search
@@ -1,3 +1,6 @@
|
|||||||
|
### v3.1.3
|
||||||
|
* Замена в конфигурации одного массива скриптов на кастомные и остальные
|
||||||
|
* Добавлена новая ошибка с дупликатами id
|
||||||
### v3.1.1
|
### v3.1.1
|
||||||
* Обновлены зависимости
|
* Обновлены зависимости
|
||||||
* Замена `integration-system` на `txix-open`
|
* Замена `integration-system` на `txix-open`
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ func (a *Assembly) ReceiveConfig(ctx context.Context, remoteConfig []byte) error
|
|||||||
a.logger.SetLevel(newCfg.LogLevel)
|
a.logger.SetLevel(newCfg.LogLevel)
|
||||||
|
|
||||||
locator := NewLocator(a.logger, a.router)
|
locator := NewLocator(a.logger, a.router)
|
||||||
handler := locator.Handler(newCfg)
|
handler, err := locator.Handler(newCfg)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Fatal(ctx, errors.WithMessage(err, "new handler"))
|
||||||
|
}
|
||||||
|
|
||||||
a.server.Upgrade(handler)
|
a.server.Upgrade(handler)
|
||||||
|
|
||||||
|
|||||||
+35
-3
@@ -2,12 +2,14 @@
|
|||||||
package assembly
|
package assembly
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/txix-open/isp-kit/grpc"
|
"github.com/txix-open/isp-kit/grpc"
|
||||||
"github.com/txix-open/isp-kit/grpc/client"
|
"github.com/txix-open/isp-kit/grpc/client"
|
||||||
"github.com/txix-open/isp-kit/grpc/endpoint"
|
"github.com/txix-open/isp-kit/grpc/endpoint"
|
||||||
"github.com/txix-open/isp-kit/log"
|
"github.com/txix-open/isp-kit/log"
|
||||||
"isp-script-service/conf"
|
"isp-script-service/conf"
|
||||||
"isp-script-service/controller"
|
"isp-script-service/controller"
|
||||||
|
"isp-script-service/domain"
|
||||||
"isp-script-service/repository"
|
"isp-script-service/repository"
|
||||||
"isp-script-service/routes"
|
"isp-script-service/routes"
|
||||||
"isp-script-service/service"
|
"isp-script-service/service"
|
||||||
@@ -25,9 +27,14 @@ func NewLocator(logger log.Logger, grpcCli *client.Client) Locator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Locator) Handler(cfg conf.Remote) *grpc.Mux {
|
func (l Locator) Handler(cfg conf.Remote) (*grpc.Mux, error) {
|
||||||
|
scripts, err := MergeScripts(cfg.Scripts, cfg.CustomScripts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessage(err, "merge scripts")
|
||||||
|
}
|
||||||
|
|
||||||
router := repository.NewRouter(l.grpcCli)
|
router := repository.NewRouter(l.grpcCli)
|
||||||
scriptService := service.NewScript(router, l.logger, cfg.Scripts, cfg.SharedScript, cfg.ScriptExecutionTimeoutMs)
|
scriptService := service.NewScript(router, l.logger, scripts, cfg.SharedScript, cfg.ScriptExecutionTimeoutMs)
|
||||||
scriptController := controller.NewScript(scriptService)
|
scriptController := controller.NewScript(scriptService)
|
||||||
|
|
||||||
c := routes.Controllers{
|
c := routes.Controllers{
|
||||||
@@ -35,5 +42,30 @@ func (l Locator) Handler(cfg conf.Remote) *grpc.Mux {
|
|||||||
}
|
}
|
||||||
mapper := endpoint.DefaultWrapper(l.logger, endpoint.BodyLogger(l.logger))
|
mapper := endpoint.DefaultWrapper(l.logger, endpoint.BodyLogger(l.logger))
|
||||||
handler := routes.Handler(mapper, c)
|
handler := routes.Handler(mapper, c)
|
||||||
return handler
|
return handler, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func MergeScripts(scripts []conf.ScriptDefinition, customScripts []conf.ScriptDefinition) ([]conf.ScriptDefinition, error) {
|
||||||
|
uniqueScripts := make(map[string]conf.ScriptDefinition)
|
||||||
|
|
||||||
|
for _, script := range scripts {
|
||||||
|
if _, exists := uniqueScripts[script.Id]; exists {
|
||||||
|
return nil, domain.ErrDuplicateScriptId
|
||||||
|
}
|
||||||
|
uniqueScripts[script.Id] = script
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, customScript := range customScripts {
|
||||||
|
if _, exists := uniqueScripts[customScript.Id]; exists {
|
||||||
|
return nil, domain.ErrDuplicateScriptId
|
||||||
|
}
|
||||||
|
uniqueScripts[customScript.Id] = customScript
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedScripts := make([]conf.ScriptDefinition, 0, len(uniqueScripts))
|
||||||
|
for _, script := range uniqueScripts {
|
||||||
|
mergedScripts = append(mergedScripts, script)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mergedScripts, nil
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"logLevel": "debug",
|
"logLevel": "debug",
|
||||||
"scripts": [
|
"scripts": [],
|
||||||
],
|
"customScripts": [],
|
||||||
"sharedScript": "",
|
"sharedScript": "",
|
||||||
"scriptExecutionTimeoutMs": 1000
|
"scriptExecutionTimeoutMs": 1000
|
||||||
}
|
}
|
||||||
+5
-4
@@ -17,10 +17,11 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Remote struct {
|
type Remote struct {
|
||||||
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования"`
|
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования"`
|
||||||
Scripts []ScriptDefinition
|
Scripts []ScriptDefinition `schema:"Скрипты"`
|
||||||
SharedScript string
|
CustomScripts []ScriptDefinition `schema:"Кастомные скрипты"`
|
||||||
ScriptExecutionTimeoutMs int `validate:"required"`
|
SharedScript string `schema:"Общий скрипт"`
|
||||||
|
ScriptExecutionTimeoutMs int `validate:"required" schema:"Время выполнения скрипта"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ScriptDefinition struct {
|
type ScriptDefinition struct {
|
||||||
|
|||||||
+4
-1
@@ -4,4 +4,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrScriptNotFound = errors.New("script not found")
|
var (
|
||||||
|
ErrScriptNotFound = errors.New("script not found")
|
||||||
|
ErrDuplicateScriptId = errors.New("duplicate script id")
|
||||||
|
)
|
||||||
@@ -45,10 +45,11 @@ func (s *ApiScript) SetupSuite() {
|
|||||||
Script: "return false",
|
Script: "return false",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
CustomScripts: []conf.ScriptDefinition{},
|
||||||
}
|
}
|
||||||
|
|
||||||
locator := assembly.NewLocator(s.test.Logger(), cli)
|
locator := assembly.NewLocator(s.test.Logger(), cli)
|
||||||
handler := locator.Handler(cfg)
|
handler, _ := locator.Handler(cfg)
|
||||||
|
|
||||||
_, s.apiCli = grpct.TestServer(s.test, handler)
|
_, s.apiCli = grpct.TestServer(s.test, handler)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package tests_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
"github.com/txix-open/isp-kit/grpc/client"
|
||||||
|
"github.com/txix-open/isp-kit/test"
|
||||||
|
"github.com/txix-open/isp-kit/test/grpct"
|
||||||
|
"isp-script-service/assembly"
|
||||||
|
"isp-script-service/conf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMergeScriptTestSuite(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
suite.Run(t, &MergeScript{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type MergeScript struct {
|
||||||
|
suite.Suite
|
||||||
|
test *test.Test
|
||||||
|
require *require.Assertions
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MergeScript) TestMergeScriptsDuplicateId() {
|
||||||
|
s.test, s.require = test.New(s.T())
|
||||||
|
|
||||||
|
cli := s.initMockServer()
|
||||||
|
|
||||||
|
cfg := conf.Remote{
|
||||||
|
ScriptExecutionTimeoutMs: 10000,
|
||||||
|
Scripts: []conf.ScriptDefinition{
|
||||||
|
{
|
||||||
|
Id: "1",
|
||||||
|
Script: "return true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
CustomScripts: []conf.ScriptDefinition{
|
||||||
|
{
|
||||||
|
Id: "1",
|
||||||
|
Script: "return false",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
locator := assembly.NewLocator(s.test.Logger(), cli)
|
||||||
|
_, err := locator.Handler(cfg)
|
||||||
|
|
||||||
|
s.Require().Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MergeScript) TestMergeScriptsHappyPath() {
|
||||||
|
s.test, s.require = test.New(s.T())
|
||||||
|
|
||||||
|
cli := s.initMockServer()
|
||||||
|
|
||||||
|
cfg := conf.Remote{
|
||||||
|
ScriptExecutionTimeoutMs: 10000,
|
||||||
|
Scripts: []conf.ScriptDefinition{
|
||||||
|
{
|
||||||
|
Id: "1",
|
||||||
|
Script: "return true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "2",
|
||||||
|
Script: "return true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
CustomScripts: []conf.ScriptDefinition{
|
||||||
|
{
|
||||||
|
Id: "3",
|
||||||
|
Script: "return false",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
locator := assembly.NewLocator(s.test.Logger(), cli)
|
||||||
|
_, err := locator.Handler(cfg)
|
||||||
|
|
||||||
|
s.Require().NoError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MergeScript) initMockServer() *client.Client {
|
||||||
|
server, cli := grpct.NewMock(s.test)
|
||||||
|
server.Mock("mock", func(req bool) bool {
|
||||||
|
return req
|
||||||
|
})
|
||||||
|
|
||||||
|
return cli
|
||||||
|
}
|
||||||
Reference in new issue
Block a user