Implement api
This commit is contained in:
1 parent
80a99c425b
commit
b2e34c00a4
30 files changed
+708
-69
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-config-service/domain"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
}
|
||||
|
||||
func (c Config) GetActiveConfigByModuleName(ctx context.Context, moduleName string) (*domain.Config, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c Config) GetConfigsByModuleId(ctx context.Context, moduleId string) ([]domain.Config, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c Config) CreateUpdateConfig(ctx context.Context, moduleId string) (*domain.Config, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c Config) GetConfigById(ctx context.Context, configId string) (*domain.Config, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c Config) MarkConfigAsActive(ctx context.Context, configId string) error {
|
||||
|
||||
}
|
||||
|
||||
func (c Config) DeleteConfigs(ctx context.Context, idList []string) error {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-config-service/domain"
|
||||
)
|
||||
|
||||
type ConfigHistory struct {
|
||||
}
|
||||
|
||||
func (c ConfigHistory) GetAllVersion(ctx context.Context, configId string) ([]domain.ConfigVersion, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c ConfigHistory) DeleteConfigVersion(ctx context.Context, id string) error {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-config-service/domain"
|
||||
)
|
||||
|
||||
type ConfigSchema struct {
|
||||
}
|
||||
|
||||
func (c ConfigSchema) SchemaByModuleId(ctx context.Context, moduleId string) (*domain.ConfigSchema, error) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/helpers"
|
||||
)
|
||||
|
||||
type ModuleRepo interface {
|
||||
All(ctx context.Context) ([]entity.Module, error)
|
||||
Delete(ctx context.Context, idList []string) error
|
||||
}
|
||||
|
||||
type BackendsRepo interface {
|
||||
All(ctx context.Context) ([]entity.Backend, error)
|
||||
}
|
||||
|
||||
type SchemaRepo interface {
|
||||
All(ctx context.Context) ([]entity.ConfigSchema, error)
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
moduleRepo ModuleRepo
|
||||
backendsRepo BackendsRepo
|
||||
schemaRepo SchemaRepo
|
||||
}
|
||||
|
||||
func NewModule(
|
||||
moduleRepo ModuleRepo,
|
||||
backendsRepo BackendsRepo,
|
||||
schemaRepo SchemaRepo,
|
||||
) Module {
|
||||
return Module{
|
||||
moduleRepo: moduleRepo,
|
||||
backendsRepo: backendsRepo,
|
||||
schemaRepo: schemaRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Module) Status(ctx context.Context) ([]domain.ModuleInfo, error) {
|
||||
var (
|
||||
modules []entity.Module
|
||||
backends []entity.Backend
|
||||
schemas []entity.ConfigSchema
|
||||
err error
|
||||
)
|
||||
group, ctx := errgroup.WithContext(ctx)
|
||||
group.Go(func() error {
|
||||
modules, err = s.moduleRepo.All(ctx)
|
||||
return errors.WithMessage(err, "get all modules")
|
||||
})
|
||||
group.Go(func() error {
|
||||
backends, err = s.backendsRepo.All(ctx)
|
||||
return errors.WithMessage(err, "get all backends")
|
||||
})
|
||||
group.Go(func() error {
|
||||
schemas, err = s.schemaRepo.All(ctx)
|
||||
return errors.WithMessage(err, "get all schemas")
|
||||
})
|
||||
err = group.Wait()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "wait")
|
||||
}
|
||||
|
||||
connections := make(map[string][]domain.Connection, len(modules))
|
||||
for _, backend := range backends {
|
||||
addr, err := helpers.SplitAddress(backend)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "split address")
|
||||
}
|
||||
conn := domain.Connection{
|
||||
LibVersion: backend.LibVersion,
|
||||
Version: backend.Version,
|
||||
Address: addr,
|
||||
Endpoints: backend.Endpoints.Value,
|
||||
EstablishedAt: time.Time(backend.UpdatedAt),
|
||||
}
|
||||
connections[backend.ModuleId] = append(connections[backend.ModuleId], conn)
|
||||
}
|
||||
|
||||
schemasMap := make(map[string]json.RawMessage)
|
||||
for _, schema := range schemas {
|
||||
schemasMap[schema.ModuleId] = schema.Data
|
||||
}
|
||||
|
||||
moduleInfos := make([]domain.ModuleInfo, 0, len(modules))
|
||||
for _, module := range modules {
|
||||
info := domain.ModuleInfo{
|
||||
Id: module.Id,
|
||||
Name: module.Name,
|
||||
Active: len(connections[module.Id]) > 0,
|
||||
LastConnectedAt: (*time.Time)(module.LastConnectedAt),
|
||||
LastDisconnectedAt: (*time.Time)(module.LastDisconnectedAt),
|
||||
ConfigSchema: schemasMap[module.Id],
|
||||
Status: connections[module.Id],
|
||||
CreatedAt: time.Time(module.CreatedAt),
|
||||
}
|
||||
moduleInfos = append(moduleInfos, info)
|
||||
}
|
||||
|
||||
return moduleInfos, nil
|
||||
}
|
||||
|
||||
func (s Module) Delete(ctx context.Context, idList []string) error {
|
||||
err := s.moduleRepo.Delete(ctx, idList)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "delete modules")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -26,7 +26,7 @@ func NewCleaner(repo Repo, eventTtl time.Duration, logger log.Logger) Cleaner {
|
||||
|
||||
func (c Cleaner) Do(ctx context.Context) {
|
||||
deleteBefore := time.Now().Add(-c.eventTtl)
|
||||
deleted, err := c.repo.DeleteByCreatedAt(ctx, xtypes.Time{Value: deleteBefore})
|
||||
deleted, err := c.repo.DeleteByCreatedAt(ctx, xtypes.Time(deleteBefore))
|
||||
if err != nil {
|
||||
c.logger.Error(ctx, errors.WithMessage(err, "delete old events"))
|
||||
return
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"isp-config-service/entity"
|
||||
)
|
||||
|
||||
@@ -23,5 +25,6 @@ func (c Compactor) Compact(events []entity.Event) []entity.Event {
|
||||
uniqueEvents = append(uniqueEvents, event)
|
||||
uniqueEventKeys[key] = true
|
||||
}
|
||||
slices.Reverse(uniqueEvents)
|
||||
return uniqueEvents
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (w *Worker) do(ctx context.Context) error {
|
||||
|
||||
events = w.compactor.Compact(events)
|
||||
w.handler.Handle(ctx, events)
|
||||
w.lastEventId = events[len(events)-1].RowId
|
||||
w.lastEventId = events[len(events)-1].Id
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
@@ -25,10 +24,9 @@ const (
|
||||
|
||||
type Repo interface {
|
||||
Upsert(ctx context.Context, module entity.Module) (string, error)
|
||||
SetDisconnectedAt(
|
||||
SetDisconnectedAtNow(
|
||||
ctx context.Context,
|
||||
moduleId string,
|
||||
disconnected xtypes.Time,
|
||||
) error
|
||||
}
|
||||
|
||||
@@ -87,11 +85,9 @@ func NewService(
|
||||
}
|
||||
|
||||
func (s Service) OnConnect(ctx context.Context, conn *etp.Conn, moduleName string) error {
|
||||
now := now()
|
||||
module := entity.Module{
|
||||
Id: uuid.NewString(),
|
||||
Name: moduleName,
|
||||
LastConnectedAt: &xtypes.Time{Value: now},
|
||||
Id: uuid.NewString(),
|
||||
Name: moduleName,
|
||||
}
|
||||
moduleId, err := s.moduleRepo.Upsert(ctx, module)
|
||||
if err != nil {
|
||||
@@ -121,10 +117,9 @@ func (s Service) OnDisconnect(
|
||||
s.logger.Error(ctx, message)
|
||||
}
|
||||
|
||||
now := now()
|
||||
moduleId, _ := store.Get[string](conn.Data(), moduleIdKey)
|
||||
if moduleId != "" {
|
||||
err = s.moduleRepo.SetDisconnectedAt(ctx, moduleName, xtypes.Time{Value: now})
|
||||
err = s.moduleRepo.SetDisconnectedAtNow(ctx, moduleName)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update disconnected time in store")
|
||||
}
|
||||
@@ -241,7 +236,7 @@ func (s Service) OnModuleConfigSchema(
|
||||
ModuleId: moduleId,
|
||||
Data: data.Config,
|
||||
Version: 1,
|
||||
Active: xtypes.Bool{Value: true},
|
||||
Active: xtypes.Bool(true),
|
||||
}
|
||||
err := s.configRepo.Insert(ctx, initialConfig)
|
||||
if err != nil {
|
||||
@@ -273,7 +268,3 @@ func (s Service) OnModuleConfigSchema(
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func now() time.Time {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (d Adapter) Select(ctx context.Context, ptr any, query string, args ...any)
|
||||
consistencyFromContext(ctx).appendParams(params)
|
||||
err := d.cli.Post("/db/query").
|
||||
QueryParams(params).
|
||||
JsonRequestBody(request(query, args...)).
|
||||
JsonRequestBody(requests(request(query, args...))).
|
||||
JsonResponseBody(&resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
|
||||
@@ -2,7 +2,6 @@ package subscription
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -126,7 +125,7 @@ func (s Service) notifyBackendsChanged(
|
||||
|
||||
addresses := make([]cluster.AddressConfiguration, 0)
|
||||
for _, backend := range backends {
|
||||
addr, err := splitAddress(backend)
|
||||
addr, err := helpers.SplitAddress(backend)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "split address")
|
||||
}
|
||||
@@ -173,7 +172,7 @@ func (s Service) notifyRoutingChanged(ctx context.Context, event string, conns [
|
||||
|
||||
routingConfig := cluster.RoutingConfig{}
|
||||
for _, backend := range backends {
|
||||
addr, err := splitAddress(backend)
|
||||
addr, err := helpers.SplitAddress(backend)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "split address")
|
||||
}
|
||||
@@ -217,20 +216,9 @@ func (s Service) emitEvent(
|
||||
if err != nil {
|
||||
err := errors.WithMessagef(
|
||||
err,
|
||||
"emit event %s, to %s module, connId: %d",
|
||||
"emit event '%s', to %s module, connId: %d",
|
||||
event, helpers.ModuleName(conn), conn.Id(),
|
||||
)
|
||||
s.logger.Error(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
func splitAddress(backend entity.Backend) (cluster.AddressConfiguration, error) {
|
||||
host, port, err := net.SplitHostPort(backend.Address)
|
||||
if err != nil {
|
||||
return cluster.AddressConfiguration{}, errors.WithMessagef(err, "split backend %s address: %s", backend.ModuleId, backend.Address)
|
||||
}
|
||||
return cluster.AddressConfiguration{
|
||||
IP: host,
|
||||
Port: port,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user