80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
|
package options
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"log/slog"
|
||
|
|
||
|
"bunker/server/internal/application"
|
||
|
"bunker/server/internal/services/core"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
_ = core.Options(&options{})
|
||
|
|
||
|
errOptions = errors.New("options core service")
|
||
|
)
|
||
|
|
||
|
type options struct {
|
||
|
app *application.Application
|
||
|
logger *slog.Logger
|
||
|
db core.Database
|
||
|
}
|
||
|
|
||
|
// Initialize initializes service.
|
||
|
func Initialize(app *application.Application) error {
|
||
|
opts := &options{
|
||
|
app: app,
|
||
|
}
|
||
|
|
||
|
if err := app.RegisterService(opts); err != nil {
|
||
|
return fmt.Errorf("%w: %w", errOptions, err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *options) Configure() error {
|
||
|
if err := o.registerMigrations(); err != nil {
|
||
|
return fmt.Errorf("configure: %w", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *options) ConnectDependencies() error {
|
||
|
databaseRaw := o.app.Service(core.ServiceNameDatabase)
|
||
|
if databaseRaw == nil {
|
||
|
return fmt.Errorf("connect dependencies: get database service: %w", application.ErrServiceNotFound)
|
||
|
}
|
||
|
|
||
|
database, valid := databaseRaw.(core.Database)
|
||
|
if !valid {
|
||
|
return fmt.Errorf("connect dependencies: type assert database service: %w", core.ErrDatabaseIsInvalid)
|
||
|
}
|
||
|
|
||
|
o.db = database
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *options) Initialize() error {
|
||
|
o.logger = o.app.NewLogger("service", core.ServiceNameOptions)
|
||
|
|
||
|
o.logger.Info("Initializing...")
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *options) Name() string {
|
||
|
return core.ServiceNameOptions
|
||
|
}
|
||
|
|
||
|
func (o *options) LaunchStartupTasks() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *options) Shutdown() error {
|
||
|
return nil
|
||
|
}
|