From d256a31842ea71fed62805b1d732efa546dec811 Mon Sep 17 00:00:00 2001
From: BkSouX <bsousa@pm.me>
Date: Tue, 14 Apr 2020 20:00:06 +0200
Subject: [PATCH 1/5] api - panic instead of fatal when issue with the database
 migration

---
 api/server.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/api/server.go b/api/server.go
index 0110c6b..79f8ecc 100644
--- a/api/server.go
+++ b/api/server.go
@@ -34,7 +34,7 @@ func main() {
 
 	// Migrate database
 	if err := database.MigrateDatabase(db); err != nil {
-		log.Fatalf("Could not migrate database: %s\n", err)
+		panic(fmt.Sprintf("Could not migrate database: %s\n", err))
 	}
 
 	rootRouter := mux.NewRouter()

From 11ea1c13bd0b74953fac55b79ac5a3ea403322a4 Mon Sep 17 00:00:00 2001
From: BkSouX <bsousa@pm.me>
Date: Tue, 14 Apr 2020 20:04:39 +0200
Subject: [PATCH 2/5] api - miss import

---
 api/server.go | 1 +
 1 file changed, 1 insertion(+)

diff --git a/api/server.go b/api/server.go
index 79f8ecc..0977acc 100644
--- a/api/server.go
+++ b/api/server.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"log"
 	"net/http"
 	"os"

From c64b4d0d391d6e841e8e515fda5f3c5989d27282 Mon Sep 17 00:00:00 2001
From: Bruno Sousa <bruno.sousa@primotexto.com>
Date: Wed, 15 Apr 2020 16:18:51 +0200
Subject: [PATCH 3/5] make SetupDatabase returns an error and use PingContext
 with timeout

---
 .gitignore            |  1 +
 api/database/mysql.go | 31 ++++++++++++-------------------
 api/server.go         |  7 +++++--
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/.gitignore b/.gitignore
index e3307a1..462e530 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,4 @@ yarn-error.log*
 
 # vscode
 __debug_bin
+.idea
diff --git a/api/database/mysql.go b/api/database/mysql.go
index da51a16..5f11cb1 100644
--- a/api/database/mysql.go
+++ b/api/database/mysql.go
@@ -1,12 +1,15 @@
 package database
 
 import (
+	"context"
 	"database/sql"
 	"log"
 	"net/url"
 	"os"
 	"time"
 
+	"github.com/pkg/errors"
+
 	// Load mysql driver
 	_ "github.com/go-sql-driver/mysql"
 
@@ -18,15 +21,15 @@ import (
 )
 
 // SetupDatabase connects to the database using environment variables
-func SetupDatabase() *sql.DB {
+func SetupDatabase() (*sql.DB, error) {
 
 	address, err := url.Parse(os.Getenv("MYSQL_URL"))
 	if err != nil {
-		log.Fatalf("Could not parse mysql url: %s\n", err)
+		return nil, errors.Wrapf(err, "Could not parse mysql url")
 	}
 
 	if address.String() == "" {
-		log.Fatalln("ERROR: Environment variable MYSQL_URL missing, exiting")
+		return nil, errors.New("Environment variable MYSQL_URL missing, exiting")
 	}
 
 	queryValues := address.Query()
@@ -49,33 +52,23 @@ func SetupDatabase() *sql.DB {
 				time.Sleep(time.Second)
 				continue
 			} else {
-				log.Fatalln("ERROR: Could not connect to database, exiting")
+				return nil, errors.New("Could not connect to database, exiting")
 			}
 		}
 
 		break
 	}
 
-	tryCount = 0
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
 
-	for {
-		if err := db.Ping(); err != nil {
-			if tryCount < 4 {
-				tryCount++
-				log.Printf("Could not ping database: %s. Will retry after 1 second", err.Error())
-				time.Sleep(time.Second)
-				continue
-			} else {
-				log.Fatalln("ERROR: Could not ping database, exiting")
-			}
-		}
-
-		break
+	if err := db.PingContext(ctx); err != nil {
+		return nil, errors.Wrap(err, "Could not ping database, exiting")
 	}
 
 	db.SetMaxOpenConns(80)
 
-	return db
+	return db, nil
 }
 
 func MigrateDatabase(db *sql.DB) error {
diff --git a/api/server.go b/api/server.go
index 0977acc..f4f94fb 100644
--- a/api/server.go
+++ b/api/server.go
@@ -30,7 +30,10 @@ func main() {
 
 	devMode := os.Getenv("DEVELOPMENT") == "1"
 
-	db := database.SetupDatabase()
+	db, err := database.SetupDatabase()
+	if err != nil {
+		log.Panicf("Could not connect to database: %s\n", err)
+	}
 	defer db.Close()
 
 	// Migrate database
@@ -96,5 +99,5 @@ func main() {
 
 	}
 
-	log.Fatal(http.ListenAndServe(":"+apiListenUrl.Port(), rootRouter))
+	log.Panic(http.ListenAndServe(":"+apiListenUrl.Port(), rootRouter))
 }

From e39349963ba4c385c47f06006c71499d514dc36f Mon Sep 17 00:00:00 2001
From: Bruno Sousa <bruno.sousa@primotexto.com>
Date: Wed, 15 Apr 2020 16:20:09 +0200
Subject: [PATCH 4/5] use log.Panicf instead of just panic

---
 api/server.go | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/api/server.go b/api/server.go
index f4f94fb..834c89a 100644
--- a/api/server.go
+++ b/api/server.go
@@ -1,7 +1,6 @@
 package main
 
 import (
-	"fmt"
 	"log"
 	"net/http"
 	"os"
@@ -38,7 +37,7 @@ func main() {
 
 	// Migrate database
 	if err := database.MigrateDatabase(db); err != nil {
-		panic(fmt.Sprintf("Could not migrate database: %s\n", err))
+		log.Panicf("Could not migrate database: %s\n", err)
 	}
 
 	rootRouter := mux.NewRouter()

From c5dd3debe5abb5e6e46cec2be63373bdd84c1332 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Wed, 15 Apr 2020 20:52:08 +0200
Subject: [PATCH 5/5] Add missing new dependency + cleanup

---
 api/database/mysql.go | 2 +-
 api/go.mod            | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/api/database/mysql.go b/api/database/mysql.go
index 5f11cb1..68e28d3 100644
--- a/api/database/mysql.go
+++ b/api/database/mysql.go
@@ -25,7 +25,7 @@ func SetupDatabase() (*sql.DB, error) {
 
 	address, err := url.Parse(os.Getenv("MYSQL_URL"))
 	if err != nil {
-		return nil, errors.Wrapf(err, "Could not parse mysql url")
+		return nil, errors.Wrap(err, "Could not parse mysql url")
 	}
 
 	if address.String() == "" {
diff --git a/api/go.mod b/api/go.mod
index 221bc5b..c449601 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -18,6 +18,7 @@ require (
 	github.com/joho/godotenv v1.3.0
 	github.com/nf/cr2 v0.0.0-20180623103828-4699471a17ed
 	github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
+	github.com/pkg/errors v0.8.1
 	github.com/urfave/cli v1.22.3 // indirect
 	github.com/vektah/gqlparser v1.3.1
 	github.com/vektah/gqlparser/v2 v2.0.1
