sneak/integrate-di (#17)
Some checks failed
continuous-integration/drone/push Build is failing

moving this to use uber/fx di framework instead of the ad hoc di setup before

Co-authored-by: sneak <sneak@sneak.berlin>
Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
2023-01-29 03:06:05 +00:00
parent 0c3797ec30
commit dd778174a7
31 changed files with 924 additions and 528 deletions

View File

@@ -0,0 +1,52 @@
package database
import (
"context"
"git.eeqj.de/sneak/gohttpserver/internal/config"
"git.eeqj.de/sneak/gohttpserver/internal/logger"
"github.com/rs/zerolog"
"go.uber.org/fx"
// spooky action at a distance!
// this populates the environment
// from a ./.env file automatically
// for development configuration.
// .env contents should be things like
// `DBURL=postgres://user:pass@.../`
// (without the backticks, of course)
_ "github.com/joho/godotenv/autoload"
)
type DatabaseParams struct {
fx.In
Logger *logger.Logger
Config *config.Config
}
type Database struct {
URL string
log *zerolog.Logger
params *DatabaseParams
}
func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
s := new(Database)
s.params = &params
s.log = params.Logger.Get()
s.log.Info().Msg("Database instantiated")
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.log.Info().Msg("Database OnStart Hook")
// FIXME connect to db
return nil
},
OnStop: func(ctx context.Context) error {
// FIXME disconnect from db
return nil
},
})
return s, nil
}