-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (50 loc) · 1.5 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
package main
import (
"log"
"github.com/YannHulot/petstore/api/models"
"github.com/YannHulot/petstore/api/server"
"github.com/jinzhu/gorm"
)
func main() {
var db *gorm.DB
// Load the .env file containing the variables
err := models.LoadEnvFile()
if err != nil {
log.Fatalf("error while loading the env file: %s", err.Error())
}
// create a new config
config, err := models.NewConfig()
if err != nil {
log.Fatalf("error while getting the config: %s", err.Error())
}
// validate the config
err = config.Validate()
if err != nil {
log.Fatalf("error while validating the config: %s", err.Error())
}
// create the Database if possible
err = models.CreatePgDb(config)
if err != nil {
log.Printf("error while creating the database: %s", err.Error())
log.Printf("db may already exist")
// database may already exist , so we can try to connect to it
db, err = models.OpenAndTestDBConnection(config)
if err != nil {
// connection has failed for some reason
// needs investigation so we shut down the application
log.Fatalf("error while creating a connection with the db: %s", err.Error())
}
}
if db == nil {
// create the tables, run the migrations and open a connection to the DB
db, err = models.OpenAndTestDBConnection(config)
if err != nil {
log.Fatalf("error while creating a connection with the db: %s", err.Error())
}
}
defer db.Close()
// create the router and the routes
router := server.CreateRouter(db)
// start the server
log.Fatal(router.Run(":8080"))
}