Skip to content

Commit

Permalink
refactor other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
agalitsyn committed Jan 23, 2023
1 parent 620265a commit 9f7c730
Show file tree
Hide file tree
Showing 57 changed files with 1,340 additions and 1,413 deletions.
19 changes: 9 additions & 10 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import (
"golang.org/x/sync/errgroup"

"soldr/pkg/app/api/server"
"soldr/pkg/app/api/storage/mem"
useraction "soldr/pkg/app/api/user_action"
"soldr/pkg/app/api/storage"
"soldr/pkg/app/api/useraction"
"soldr/pkg/app/api/worker"
"soldr/pkg/app/api/worker/events"
"soldr/pkg/log"
"soldr/pkg/logtooling"
"soldr/pkg/mysql"
"soldr/pkg/observability"
"soldr/pkg/secret"
"soldr/pkg/storage"
"soldr/pkg/storage/mysql"
"soldr/pkg/system"
"soldr/pkg/version"
)
Expand Down Expand Up @@ -162,8 +161,8 @@ func main() {
MaxAge: 14,
Compress: true,
}
logrus.SetLevel(log.ParseLevel(cfg.Log.Level))
logrus.SetFormatter(log.ParseFormat(cfg.Log.Format))
logrus.SetLevel(logtooling.ParseLevel(cfg.Log.Level))
logrus.SetFormatter(logtooling.ParseFormat(cfg.Log.Format))
logrus.SetOutput(io.MultiWriter(os.Stdout, logFile))

dsn := fmt.Sprintf("%s:%s@%s/%s?parseTime=true",
Expand Down Expand Up @@ -195,8 +194,8 @@ func main() {
}

// storages
dbConnectionStorage := mem.NewDBConnectionStorage()
s3ConnectionStorage := mem.NewS3ConnectionStorage()
dbConnectionStorage := storage.NewDBConnectionStorage()
s3ConnectionStorage := storage.NewS3ConnectionStorage()

tracerClient := observability.NewProxyTracerClient(
observability.NewOtlpTracerAndLoggerClient(cfg.Tracing.Addr),
Expand Down Expand Up @@ -254,7 +253,7 @@ func main() {
)

gormMeter := meterProvider.Meter("vxapi-meter")
if err = storage.InitGormMetrics(gormMeter); err != nil {
if err = mysql.InitGormMetrics(gormMeter); err != nil {
logrus.WithError(err).Error("could not initialize vxapi-meter")
return
}
Expand Down
33 changes: 18 additions & 15 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ import (
"soldr/pkg/app/server/config"
"soldr/pkg/app/server/mmodule"
"soldr/pkg/controller"
"soldr/pkg/db"
"soldr/pkg/filestorage"
"soldr/pkg/filestorage/fs"
"soldr/pkg/filestorage/s3"
"soldr/pkg/observability"
"soldr/pkg/storage"
"soldr/pkg/system"
"soldr/pkg/utils"
"soldr/pkg/vxproto"
Expand Down Expand Up @@ -78,7 +81,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
if err != nil {
return fmt.Errorf("failed to compose a DSN from config: %w", err)
}
gdb, err := storage.New(dsn)
gdb, err := db.New(dsn)
if err != nil {
return fmt.Errorf("failed to initialize a connection to DB: %w", err)
}
Expand All @@ -103,15 +106,15 @@ func (s *Server) Init(env svc.Environment) (err error) {
case loaderTypeFS:
cl, err = controller.NewConfigFromFS(s.config.Base)
case loaderTypeS3:
var s3ConnParams *storage.S3ConnParams
var s3ConnParams *s3.Config
s3ConnParams, err = s3ConnParamsFromConfig(&s.config.S3)
if err != nil {
err = fmt.Errorf("failed to compose s3 connection params from config: %w", err)
break
}
cl, err = controller.NewConfigFromS3(s3ConnParams)
case loaderTypeDB:
var dsn *storage.DSN
var dsn *db.DSN
dsn, err = dsnFromConfig(&s.config.DB)
if err != nil {
err = fmt.Errorf("failed to compose a DSN from config: %w", err)
Expand All @@ -132,7 +135,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
case loaderTypeFS:
fl, err = controller.NewFilesFromFS(s.config.Base)
case loaderTypeS3:
var s3ConnParams *storage.S3ConnParams
var s3ConnParams *s3.Config
s3ConnParams, err = s3ConnParamsFromConfig(&s.config.S3)
if err != nil {
err = fmt.Errorf("failed to compose s3 connection params from config: %w", err)
Expand All @@ -149,7 +152,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
logger.Info("modules files loader was created")

utils.RemoveUnusedTempDir()
store, err := storage.NewS3(&storage.S3ConnParams{
store, err := s3.New(&s3.Config{
Endpoint: s.config.S3.Endpoint,
AccessKey: s.config.S3.AccessKey,
SecretKey: s.config.S3.SecretKey,
Expand Down Expand Up @@ -185,7 +188,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
return
}

func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
func dsnFromConfig(c *config.DB) (*db.DSN, error) {
if c == nil {
return nil, fmt.Errorf("passed config is nil")
}
Expand All @@ -204,7 +207,7 @@ func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
if len(c.Name) == 0 {
return nil, fmt.Errorf("db name is empty")
}
return &storage.DSN{
return &db.DSN{
Host: c.Host,
Port: c.Port,
User: c.User,
Expand All @@ -213,7 +216,7 @@ func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
}, nil
}

func s3ConnParamsFromConfig(c *config.S3) (*storage.S3ConnParams, error) {
func s3ConnParamsFromConfig(c *config.S3) (*s3.Config, error) {
if c == nil {
return nil, fmt.Errorf("passed config is nil")
}
Expand All @@ -229,19 +232,19 @@ func s3ConnParamsFromConfig(c *config.S3) (*storage.S3ConnParams, error) {
if len(c.BucketName) == 0 {
return nil, fmt.Errorf("bucket name is empty")
}
return &storage.S3ConnParams{
return &s3.Config{
Endpoint: c.Endpoint,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
BucketName: c.BucketName,
}, nil
}

func initCertProvider(c *config.CertsConfig, s3 storage.IFileReader) (certs.Provider, error) {
func initCertProvider(c *config.CertsConfig, s3FileReader filestorage.Reader) (certs.Provider, error) {
if c == nil {
return nil, fmt.Errorf("passed config object is nil")
}
createFileProvider := func(store storage.IFileReader, base string) (certs.Provider, error) {
createFileProvider := func(store filestorage.Reader, base string) (certs.Provider, error) {
conf := &certsConfig.Config{
StaticProvider: &certsConfig.StaticProvider{
Reader: store,
Expand All @@ -256,19 +259,19 @@ func initCertProvider(c *config.CertsConfig, s3 storage.IFileReader) (certs.Prov
}
switch c.Type {
case loaderTypeFS:
store, err := storage.NewFS()
store, err := fs.New()
if err != nil {
return nil, fmt.Errorf("failed to initialize a file store: %w", err)
}
return createFileProvider(store, c.Base)
case loaderTypeS3:
return createFileProvider(s3, c.Base)
return createFileProvider(s3FileReader, c.Base)
default:
return nil, fmt.Errorf("store type %s is not available for certificate providers", c.Type)
}
}

func initGorm(dsn *storage.DSN, logDir string) (*gorm.DB, error) {
func initGorm(dsn *db.DSN, logDir string) (*gorm.DB, error) {
addr := fmt.Sprintf(
"%s:%s@%s/%s?parseTime=true",
dsn.User,
Expand Down
23 changes: 12 additions & 11 deletions pkg/app/api/client/agent_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import (
"github.com/jinzhu/gorm"

"soldr/pkg/app/api/models"
"soldr/pkg/app/api/storage/mem"
"soldr/pkg/app/api/storage"
"soldr/pkg/filestorage"
"soldr/pkg/filestorage/s3"
"soldr/pkg/mysql"
"soldr/pkg/secret"
"soldr/pkg/storage"
"soldr/pkg/storage/mysql"
)

type AgentServerClient struct {
db *gorm.DB
dbConns *mem.DBConnectionStorage
s3Conns *mem.S3ConnectionStorage
dbConns *storage.DBConnectionStorage
s3Conns *storage.S3ConnectionStorage
}

func NewAgentServerClient(
db *gorm.DB,
serviceDBConns *mem.DBConnectionStorage,
serviceS3Conns *mem.S3ConnectionStorage,
serviceDBConns *storage.DBConnectionStorage,
serviceS3Conns *storage.S3ConnectionStorage,
) *AgentServerClient {
return &AgentServerClient{
db: db,
Expand Down Expand Up @@ -70,18 +71,18 @@ func (c *AgentServerClient) GetDB(ctx context.Context, hash string) (*gorm.DB, e
return dbWithORM, nil
}

func (c *AgentServerClient) GetS3(hash string) (storage.IStorage, error) {
s3, err := c.s3Conns.Get(hash)
func (c *AgentServerClient) GetS3(hash string) (filestorage.Storage, error) {
s3Conn, err := c.s3Conns.Get(hash)
if err == nil {
return s3, nil
return s3Conn, nil
}

var service models.Service
if err = c.db.Take(&service, "hash = ?", hash).Error; err != nil {
return nil, fmt.Errorf("could not get service by hash '%s': %w", hash, err)
}

s3Conn, err := storage.NewS3(service.Info.S3.ToS3ConnParams())
s3Conn, err = s3.New(service.Info.S3.ToS3ConnParams())
if err != nil {
return nil, fmt.Errorf("could not create S3 client: %w", err)
}
Expand Down
Loading

0 comments on commit 9f7c730

Please sign in to comment.