-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (94 loc) · 2.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"database/sql"
api "confxsd/arf-case/api"
util "confxsd/arf-case/util"
"github.com/rs/zerolog/log"
db "confxsd/arf-case/db/sqlc"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/golang-migrate/migrate/v4/source/github"
_ "github.com/lib/pq"
)
func runGinServer(config util.Config, store db.Store) {
server, err := api.NewServer(config, store)
if err != nil {
log.Fatal().Err(err).Msg("cannot create server")
}
err = server.Start(config.HTTPServerAddress)
if err != nil {
log.Fatal().Err(err).Msg("cannot start server")
}
}
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
if err != nil {
log.Fatal().Err(err).Msg("cannot create new migrate instance")
}
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal().Err(err).Msg("failed to run migrate up")
}
log.Info().Msg("db migrated successfully")
}
func createSystemUser(ctx context.Context, config util.Config, store db.Store) error {
arg := db.CreateUserParams{
Username: config.SystemUsername,
Password: config.SystemPassword,
}
gotUser, err := store.GetUserByUsername(ctx, config.SystemUsername)
if err != nil {
user, err := store.CreateUser(ctx, arg)
if err != nil {
log.Fatal().Err(err).Msg("cannot create system user")
return err
}
log.Info().Msg("system user created succesfully")
wallets := []db.CreateWalletParams{
{
UserID: user.ID,
Balance: 100000,
Currency: util.USD,
},
{
UserID: user.ID,
Balance: 100000,
Currency: util.EUR,
},
{
UserID: user.ID,
Balance: 100000,
Currency: util.TRY,
},
}
for _, w := range wallets {
_, err := store.CreateWallet(ctx, w)
if err != nil {
log.Fatal().Err(err).Msg("cannot create wallet")
}
}
log.Info().Msg("system user wallets created succesfully")
return nil
}
if gotUser.Username != "" {
log.Info().Msg("system user already created, skipping initialization")
}
return nil
}
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal().Err(err).Msg("cannot load config")
}
dbSource := `postgresql://` + config.DBUser + `:` + config.DBPassword + `@postgres:` + config.DBPort + `/` + config.DBName + `?sslmode=disable`
conn, err := sql.Open("postgres", dbSource)
if err != nil {
log.Fatal().Err(err).Msg("cannot connect db")
}
runDBMigration(config.MigrationURL, dbSource)
store := db.NewStore(conn)
// first time initialization, skips safely if system user available
createSystemUser(context.Background(), config, store)
runGinServer(config, store)
}