-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (42 loc) · 1.23 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
package main
import (
"context"
"log"
"github.com/foyez/simplebank/api"
db "github.com/foyez/simplebank/db/sqlc"
"github.com/foyez/simplebank/util"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal("cannot load config: ", err)
}
connPool, err := pgxpool.New(context.Background(), config.DBSource)
if err != nil {
log.Fatal("cannot connect to db: ", err)
}
runDBMigration(config.MigrationURL, config.DBSource)
store := db.NewStore(connPool)
server, err := api.NewServer(config, store)
if err != nil {
log.Fatal("cannot create server: ", err)
}
err = server.Start(config.ServerAddress)
if err != nil {
log.Fatal("cannot start server: ", err)
}
}
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
if err != nil {
log.Fatal("cannot create new migration instance: ", err)
}
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal("failed to run migrate up: ", err)
}
log.Println("db migrated successfully")
}