import
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
// Общая инфраструктура: БД, кэш, шина событий.
|
||||
// Этим владеет платформенная команда (или DevOps).
|
||||
// Бизнес-команды используют, но не трогают.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var InfraModule = fx.Module("infra",
|
||||
fx.Provide(newDB, newCache, newEventBus),
|
||||
)
|
||||
|
||||
// --- DB ---
|
||||
|
||||
type dbImpl struct{ dsn string }
|
||||
|
||||
func (db *dbImpl) Query(query string) error { return nil }
|
||||
func (db *dbImpl) QueryRow(query string) error { return nil }
|
||||
func (db *dbImpl) Exec(query string) error { return nil }
|
||||
func (db *dbImpl) BeginTx() error { return nil }
|
||||
func (db *dbImpl) BulkInsert(query string, args ...any) error { return nil }
|
||||
func (db *dbImpl) Close() error {
|
||||
slog.Info("БД: соединение закрыто")
|
||||
return nil
|
||||
}
|
||||
|
||||
func newDB(lc fx.Lifecycle) (DB, error) {
|
||||
db := &dbImpl{dsn: "postgres://localhost:5432/demo"}
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
slog.Info("БД: подключение установлено", "dsn", db.dsn)
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
return db.Close()
|
||||
},
|
||||
})
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// --- Cache ---
|
||||
|
||||
type cacheImpl struct{}
|
||||
|
||||
func (c *cacheImpl) Get(key string) (string, error) { return "", nil }
|
||||
func (c *cacheImpl) Set(key, value string) error { return nil }
|
||||
func (c *cacheImpl) Close() error {
|
||||
slog.Info("Кэш: соединение закрыто")
|
||||
return nil
|
||||
}
|
||||
|
||||
func newCache(lc fx.Lifecycle) Cache {
|
||||
c := &cacheImpl{}
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
slog.Info("Кэш: подключение установлено")
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
return c.Close()
|
||||
},
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
// --- EventBus ---
|
||||
|
||||
type eventBusImpl struct{}
|
||||
|
||||
func (b *eventBusImpl) Publish(event string) { slog.Info("событие", "event", event) }
|
||||
func (b *eventBusImpl) Subscribe(event string, handler func()) { slog.Info("подписка", "event", event) }
|
||||
|
||||
func newEventBus(lc fx.Lifecycle) EventBus {
|
||||
bus := &eventBusImpl{}
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
slog.Info("EventBus: запущен")
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
slog.Info("EventBus: остановлен")
|
||||
return nil
|
||||
},
|
||||
})
|
||||
return bus
|
||||
}
|
||||
Reference in New Issue
Block a user