From df522ab4235fbea89ad89c8e38572d979ecb8054 Mon Sep 17 00:00:00 2001 From: karn Date: Mon, 23 Dec 2024 10:12:41 +0530 Subject: [PATCH] db is not usable in beego stuff atm --- cmds/api_server/main.go | 9 --------- cmds/ui_api/main.go | 10 +++++++--- config/config.go | 2 +- config/config.toml | 2 +- db/init.go | 22 ++++++++++++++++++---- libs/logger/logger.go | 2 +- makefile | 2 +- 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/cmds/api_server/main.go b/cmds/api_server/main.go index ad06d15..498c5e3 100644 --- a/cmds/api_server/main.go +++ b/cmds/api_server/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "net/http" "runtime" "time" @@ -10,14 +9,11 @@ import ( "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/filter/cors" "github.com/karngyan/maek/conf" - "github.com/karngyan/maek/db" "github.com/karngyan/maek/domains" "github.com/karngyan/maek/routers" ) func main() { - ctx := context.Background() - log := logs.NewLogger(10000) defer log.Flush() @@ -37,11 +33,6 @@ func main() { panic(err) } - if err := db.Init(ctx); err != nil { - panic(err) - } - defer db.Close() - if err := domains.Init(); err != nil { panic(err) } diff --git a/cmds/ui_api/main.go b/cmds/ui_api/main.go index 6974c73..f5062ac 100644 --- a/cmds/ui_api/main.go +++ b/cmds/ui_api/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/karngyan/maek/db" "go.uber.org/fx" "go.uber.org/fx/fxevent" "go.uber.org/zap" @@ -13,13 +14,16 @@ import ( func main() { fx.New( fx.Provide( - config.New, - logger.New, + config.NewFx, + logger.NewFx, ), fx.Decorate(func(l *zap.Logger) *zap.Logger { return l.With(zap.String("service", "ui_api")) }), - fx.Invoke(ui_api.Run), + fx.Invoke( + db.Init, + ui_api.Run, + ), fx.WithLogger(func(l *zap.Logger) fxevent.Logger { return &fxevent.ZapLogger{ Logger: l, diff --git a/config/config.go b/config/config.go index 982481d..1732280 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ type Config struct { *koanf.Koanf } -func New() (*Config, error) { +func NewFx() (*Config, error) { k := koanf.New(".") configFile := os.Getenv("CONFIG_FILE") diff --git a/config/config.toml b/config/config.toml index 36803a2..595b438 100644 --- a/config/config.toml +++ b/config/config.toml @@ -5,5 +5,5 @@ port = 8080 cors_allowed_origins = ["http://localhost:3000"] [database] -dsn = "postgres://maek:passwd@localhost:5432/maek_dev?sslmode=disable" +dsn = "postgres://maek:passwd@localhost:5432/maek_dev?sslmode=disable&search_path=public" dsn_test = "postgres://maek:passwd@localhost:5433/maek_test?sslmode=disable" diff --git a/db/init.go b/db/init.go index a041a35..f7e643e 100644 --- a/db/init.go +++ b/db/init.go @@ -10,8 +10,11 @@ import ( "github.com/beego/beego/v2/core/logs" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "go.uber.org/fx" + "go.uber.org/zap" "github.com/karngyan/maek/conf" + "github.com/karngyan/maek/config" "github.com/karngyan/maek/libs/randstr" ) @@ -23,9 +26,8 @@ var ( //go:embed schema/*.sql var schemaFS embed.FS // only used in tests -func Init(ctx context.Context) error { - connString := fmt.Sprintf(`%s&search_path="%s"`, conf.SQLConn, "public") - dbc, err := pgxpool.ParseConfig(connString) +func Init(lc fx.Lifecycle, c *config.Config, l *zap.Logger) error { + dbc, err := pgxpool.ParseConfig(c.String("database.dsn")) if err != nil { return err } @@ -37,13 +39,25 @@ func Init(ctx context.Context) error { dbc.MaxConnIdleTime = 5 * time.Minute dbc.HealthCheckPeriod = 1 * time.Minute - defaultPgxPool, err = pgxpool.NewWithConfig(ctx, dbc) + defaultPgxPool, err = pgxpool.NewWithConfig(context.Background(), dbc) if err != nil { return err } Q = New(defaultPgxPool) + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + l.Info("closing db pool") + if defaultPgxPool != nil { + defaultPgxPool.Close() + } + return nil + }, + }) + + l.Info("db pool initialized") + return nil } diff --git a/libs/logger/logger.go b/libs/logger/logger.go index 3c6a3e3..f481fcb 100644 --- a/libs/logger/logger.go +++ b/libs/logger/logger.go @@ -12,7 +12,7 @@ import ( "github.com/karngyan/maek/config" ) -func New(lc fx.Lifecycle, c *config.Config) (*zap.Logger, error) { +func NewFx(lc fx.Lifecycle, c *config.Config) (*zap.Logger, error) { cfg := zap.NewProductionConfig() if c.IsDev() { cfg = zap.NewDevelopmentConfig() diff --git a/makefile b/makefile index 487c888..af8399c 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ diff: ## Check for schema changes pg-schema-diff plan --dsn "${SQL_CONN}" --schema-dir ./db/schema dev: ## Start development server - bee run -main=cmds/api_server/main.go + go run cmds/ui_api/main.go test: ## Run go tests go test -v ./... \ No newline at end of file