-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.27 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
package main
import (
"database/sql"
"log"
"strings"
api "github.com/SirJager/tables/api"
"github.com/SirJager/tables/config"
repo "github.com/SirJager/tables/service/core/repo"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
)
func main() {
c, err := config.LoadConfig(".")
if err != nil {
log.Fatal("Could not load server conifg :", err)
}
conn, conn_err := sql.Open(strings.Split(c.DBSource, ":")[0], c.DBSource)
if conn_err != nil {
panic(conn_err)
}
runDBMigration(c.MigrationURL, c.DBSource)
store := repo.NewStore(conn)
httpServer, err := api.NewHttpServer(store, conn, c)
if err != nil {
log.Fatal("Could not start http server:", err)
}
err = httpServer.Start(":" + c.Port)
if err != nil {
log.Fatal("Could not start http server:", err)
}
}
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
if err != nil {
log.Fatal("cannot create new migrate instance:", err)
}
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal("Failed to migrate database to latest version:", err)
}
log.Println("Database migration was successful")
}