-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (41 loc) · 1.16 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
package main
import (
"database/sql"
"github.com/Wintersunner/xplor/api"
db "github.com/Wintersunner/xplor/db/sqlc"
"github.com/Wintersunner/xplor/util"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattes/migrate/source/file"
"log"
)
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal("cannot load config file", err)
return
}
migrateDatabase(config)
conn, err := sql.Open(config.DBDriver, config.DBSource())
if err != nil {
log.Fatal("cannot connect to main database: ", err)
}
store := db.NewStore(conn)
server := api.NewServer(store, config)
err = server.Start()
if err != nil {
log.Fatal("cannot start server", err)
}
}
func migrateDatabase(config util.Config) {
migration, err := migrate.New("file://db/migration", config.MigrationSource())
if err != nil {
log.Fatal("cannot create new migration instance: ", err)
}
err = migration.Up()
if err != nil && err != migrate.ErrNoChange {
log.Fatal("failed to run database migrations: ", err)
}
log.Println("database migrated successfully!")
}