Skip to content

Commit

Permalink
db is not usable in beego stuff atm
Browse files Browse the repository at this point in the history
  • Loading branch information
karngyan committed Dec 23, 2024
1 parent 98997d8 commit df522ab
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
9 changes: 0 additions & 9 deletions cmds/api_server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"net/http"
"runtime"
"time"
Expand All @@ -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()

Expand All @@ -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)
}
Expand Down
10 changes: 7 additions & 3 deletions cmds/ui_api/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 18 additions & 4 deletions db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion libs/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...

0 comments on commit df522ab

Please sign in to comment.