Skip to content

Commit dc9aa60

Browse files
author
Hasan Demir
committed
Refactoring and add container
1 parent b6b01d9 commit dc9aa60

27 files changed

+420
-929
lines changed

Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
schema:
2-
go generate ./ent
3-
migrate:
4-
go run ./cobra/cobra.go migrate
2+
go generate ./ent && go run ./cobra/cobra.go migrate

app/app.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ package app
22

33
import (
44
"context"
5-
"log"
65

76
"github.com/hsndmr/go-sanctum/pkg/config"
87
"github.com/hsndmr/go-sanctum/pkg/connection"
98
)
109

11-
func Init(ctx context.Context) {
12-
config.App.Init()
13-
connection.Connect()
1410

15-
if config.App.Env == "local" {
16-
CreateSchema()
17-
}
11+
type Container struct {
12+
Config *config.Config
13+
DBClient *connection.DBClient
1814
}
1915

20-
func CreateSchema() {
21-
if err := connection.Client.Schema.Create(context.Background()); err != nil {
22-
log.Fatalf("Database Migration Error: %v", err)
23-
}
16+
var C *Container
17+
18+
// Init initializes the configuration and database connection
19+
func Init() {
20+
C = CreateContainer(context.Background())
21+
}
22+
23+
func CreateContainer(ctx context.Context) *Container {
24+
container := &Container{}
25+
container.Config, _ = config.Init()
26+
container.DBClient, _ = connection.CreateClient(container.Config)
27+
return container
2428
}

cobra/cmd/migrate.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
55
package cmd
66

77
import (
8-
"context"
9-
108
"github.com/hsndmr/go-sanctum/app"
11-
"github.com/hsndmr/go-sanctum/pkg/connection"
129
"github.com/spf13/cobra"
1310
)
1411

@@ -23,8 +20,8 @@ var migrateCmd = &cobra.Command{
2320
func init() {
2421
rootCmd.AddCommand(migrateCmd)
2522

26-
app.Init(context.Background())
27-
defer connection.Client.Close()
23+
app.Init()
24+
defer app.C.DBClient.Close()
2825

2926
// Here you will define your flags and configuration settings.
3027

ent/migrate/schema.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ent/mutation.go

+63-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ent/personalaccesstoken.go

+24-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)