-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (45 loc) · 1.14 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
package main
import (
"bloodbankservice/donation"
"log"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"bloodbankservice/database"
)
//bloodbank service base URL path
const apiBasePath = "/api"
//Get the Port from the environment so we the service can be run on Heroku
func getPort() string {
var port = os.Getenv("PORT")
// Set a default port if there is nothing in the environment
if port == "" {
port = "5001"
log.Println("No PORT environment variable detected, defaulting to " + port)
}
return ":" + port
}
func main() {
//load .env file from given path
//we keep it empty it will load .env from current directory
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
//Create database connection
db, err := database.GetDbConnection()
if err != nil {
log.Fatal(err)
}
//Initialize donation service
donationRepo := donation.NewDonationMySQL(db)
donation.DonationService = donation.NewService(donationRepo)
donation.SetupRoutes(apiBasePath)
//Setup the backend service
port := getPort()
err = http.ListenAndServe(port, nil)
if err != nil {
log.Fatal(err)
}
}