-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (82 loc) · 2.46 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"example/library_project/handlers"
// "example/library_project/models"
"example/library_project/dao"
"example/library_project/utils"
"example/library_project/dao/inmemorydao"
"example/library_project/dao/mysqldao"
"example/library_project/testdata"
// "net/http"
"github.com/gin-gonic/gin"
// "errors"
// "time"
// "encoding/json"
"fmt"
// "reflect"
// "strconv"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
// DAO selection
var daoFactory dao.DAOFactory
daoSelection := os.Getenv("DAO_SELECTION")
testMode := os.Getenv("TEST_MODE")
if daoSelection == "inmemory" {
daoFactory = inmemorydao.NewInMemoryDAOFactory()
} else if daoSelection == "mysql" {
// Environment variables for database
dbUsername := os.Getenv("LIBRARY_DB_USERNAME")
dbPassword := os.Getenv("LIBRARY_DB_PASSWORD")
dbHost := os.Getenv("LIBRARY_DB_HOST")
dbPort := os.Getenv("LIBRARY_DB_PORT")
dbName := os.Getenv("LIBRARY_DB_NAME")
daoFactory = mysqldao.NewMySQLDAOFactory(dbUsername, dbPassword, dbHost, dbPort, dbName)
} else {
log.Fatal("unexpected dao selection")
}
// Open the database connection
if err := daoFactory.Open(); err != nil {
log.Fatal("failed to open database connection: ", err)
}
// Defer closing the database connection
signalsChannel := make(chan os.Signal, 1)
signal.Notify(signalsChannel, syscall.SIGINT)
go func() {
receivedSignal := <- signalsChannel
fmt.Println("RECEIVED SIGNAL...")
if receivedSignal == syscall.SIGINT {
fmt.Println("SIGNAL IS SIGINT...")
daoFactory.Close()
fmt.Println("ABOUT TO EXIT...")
os.Exit(0)
}
}()
// Instantiate DAO
bookDAO := daoFactory.BookDAO()
// If in integration test mode, instantiate test data and add to database
if testMode == "integration" {
testBooks, err := testdata.InstantiateIntegrationTestData()
if err != nil{
log.Fatal("failed to instantiate test data")
}
for _, currentTestBook := range testBooks {
if err := bookDAO.Create(currentTestBook); err != nil{
log.Fatal("failed to add test data to DAO")
}
}
}
realTimeProvider := &utils.ProductionDateTimeProvider{}
h := handlers.NewBooksHandler(bookDAO, realTimeProvider)
router := gin.Default()
router.GET("/books", h.GetAllBooks)
router.GET("/books/:isbn", h.GetIndividualBook)
router.POST("/books", h.CreateBook)
router.DELETE("/books/:isbn", h.DeleteBook)
router.PATCH("/books/:isbn", h.UpdateBook)
fmt.Println("ABOUT TO CALL ROUTER.RUN...")
router.Run("localhost:8080")
}