-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
51 lines (38 loc) · 1.01 KB
/
Copy pathmain.go
File metadata and controls
51 lines (38 loc) · 1.01 KB
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
package main
import (
"fmt"
"library-api/handler"
"library-api/model"
"library-api/repository"
"library-api/service"
"log"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
router := gin.Default()
dsn := "host=localhost user=postgres password=postgres dbname=book port=5432 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("DB Connection Error")
}
db.AutoMigrate(model.Book{})
bookRepository := repository.NewRepository(db)
bookService := service.NewService(bookRepository)
bookHandler := handler.NewBookHandler(bookService)
books, err := bookService.FindAll()
if err != nil {
log.Fatal(err.Error())
return
}
for _, e := range books {
fmt.Println(e)
}
v1 := router.Group("/v1")
router.GET("/", bookHandler.HelloHandler)
v1.GET("/books", bookHandler.BookHandler)
v1.GET("/books/:id", bookHandler.QueryHandler)
v1.POST("/books", bookHandler.CreateBookHandler)
router.Run(":8081")
}