-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
35 lines (28 loc) · 874 Bytes
/
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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/gujral1997/book-list/controllers"
"github.com/gujral1997/book-list/driver"
"github.com/subosito/gotenv"
)
// Init books var as a slice Book struct
var db *sql.DB
func init() {
gotenv.Load()
}
func main() {
db = driver.ConnectDB()
// init router
router := mux.NewRouter()
controller := controllers.Controller{}
// Route handlers
router.HandleFunc("/api/books", controller.GetBooks(db)).Methods("GET")
router.HandleFunc("/api/books/{id}", controller.GetBook(db)).Methods("GET")
router.HandleFunc("/api/books", controller.CreateBook(db)).Methods("POST")
router.HandleFunc("/api/books", controller.UpdateBook(db)).Methods("PUT")
router.HandleFunc("/api/books/{id}", controller.DeleteBook(db)).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8000", router))
}