You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.5 KiB
101 lines
2.5 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
|
|
_ "embed"
|
|
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/lib/domain/runner"
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/lib/kernel"
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/runner/internal"
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/runner/internal/data"
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/runner/internal/data/postgres"
|
|
"github.com/jackc/pgx/v4"
|
|
"go.uber.org/dig"
|
|
|
|
_ "git.vdhsn.com/barretthousen/barretthousen/src/runner/internal/domain/liveauctioneers"
|
|
)
|
|
|
|
type (
|
|
RunnerApp struct {
|
|
LogLevel kernel.LogLevel `yaml:"log_level" env:"BH_LOG_LEVEL" env-default:"0" yaml-default:"0"`
|
|
Port int `yaml:"port" env:"RUNNER_PORT"`
|
|
Postgres kernel.PostgresConnection `yaml:"postgres" env:"RUNNER_DB"`
|
|
}
|
|
)
|
|
|
|
var migrate = flag.Bool("migrate", false, "migrates postgres db")
|
|
|
|
//go:embed internal/data/postgres/schema.sql
|
|
var dbMigrateScript string
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
kernel.Run(context.Background(), &RunnerApp{
|
|
LogLevel: kernel.LevelTrace,
|
|
Port: 5001,
|
|
})
|
|
}
|
|
|
|
func (app *RunnerApp) Start(ctx context.Context) error {
|
|
if *migrate {
|
|
kernel.InfoLog.Printf("running db migrations on %v", app.Postgres)
|
|
|
|
kernel.InfoLog.Printf("MIGRATION SCRIPT:\n%q", dbMigrateScript)
|
|
if err := kernel.Migrate(ctx, app.Postgres, dbMigrateScript); err != nil {
|
|
return fmt.Errorf("could not execute db migration: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
ioc := dig.New()
|
|
ioc.Provide(func() kernel.PostgresConnection {
|
|
return app.Postgres
|
|
})
|
|
|
|
ioc.Provide(func(pgCfg kernel.PostgresConnection) (*pgx.Conn, error) {
|
|
return kernel.NewDBConnection(ctx, pgCfg)
|
|
})
|
|
|
|
ioc.Provide(func(pgConn *pgx.Conn) *postgres.Queries {
|
|
return postgres.New(pgConn)
|
|
})
|
|
|
|
ioc.Provide(func(queries *postgres.Queries) runner.Storage {
|
|
return &data.PGRunnerStorage{queries}
|
|
})
|
|
|
|
ioc.Provide(func() runner.CatalogService {
|
|
return &runner.CatalogServiceStub{}
|
|
})
|
|
|
|
ioc.Provide(func(css runner.CatalogService, rs runner.Storage) *runner.Domain {
|
|
return &runner.Domain{
|
|
Storage: rs,
|
|
CatalogService: css,
|
|
}
|
|
})
|
|
|
|
return ioc.Invoke(func(d *runner.Domain) error {
|
|
runnerService := internal.NewRunnerServer(d)
|
|
|
|
if _, err := kernel.StartGRPCServer(ctx, app.Port, runnerService); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (app *RunnerApp) OnStop(ctx context.Context) {
|
|
if err := kernel.StopGRPCServer(); err != nil {
|
|
kernel.ErrorLog.Printf("could not gracefully stop GRPC server: %v", err)
|
|
}
|
|
}
|
|
|
|
func (app *RunnerApp) GetLogLevel() kernel.LogLevel { return app.LogLevel }
|