-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (41 loc) · 1.54 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/youssef-kishk/go-bookstore/pkg/config"
"github.com/youssef-kishk/go-bookstore/pkg/controllers"
controllerPort "github.com/youssef-kishk/go-bookstore/pkg/controllers"
domainModels "github.com/youssef-kishk/go-bookstore/pkg/domain/model"
domainService "github.com/youssef-kishk/go-bookstore/pkg/domain/service"
repository "github.com/youssef-kishk/go-bookstore/pkg/repository/adapters"
)
// DB configuration params
const USERNAME = ""
const PASS = ""
const DATABASE = ""
var client *gorm.DB
func main() {
conf, err := config.NewConfig(USERNAME, PASS, DATABASE)
if err != nil {
panic(err)
}
repo := repository.NewMysqlRepository(conf)
controller := controllers.NewBookController(domainService.NewBookSerivce(repository.NewBookRepository(repo)))
repo.Db.AutoMigrate(&domainModels.Book{})
router := mux.NewRouter()
configureRoutes(router, controller)
http.Handle("/", router)
fmt.Printf("Start server on port 8080\n")
log.Fatal(http.ListenAndServe(":8080", router))
}
func configureRoutes(router *mux.Router, controller controllerPort.IBookController) {
router.HandleFunc("/book", controller.CreateBook).Methods("POST")
router.HandleFunc("/book", controller.GetBooks).Methods("GET")
router.HandleFunc("/book/{id}", controller.GetBookById).Methods("GET")
router.HandleFunc("/book/{id}", controller.UpdateBook).Methods("PUT")
router.HandleFunc("/book/{id}", controller.DeleteBook).Methods("DELETE")
}