-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
52 lines (46 loc) · 1.86 KB
/
server.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
package main
import (
"github.com/gin-gonic/gin"
"github.com/lexuanquynh/go_api/config"
"github.com/lexuanquynh/go_api/controller"
"github.com/lexuanquynh/go_api/middleware"
"github.com/lexuanquynh/go_api/repository"
"github.com/lexuanquynh/go_api/service"
"gorm.io/gorm"
)
var (
db *gorm.DB = config.SetupDatabaseConnection()
userRepository repository.UserRepository = repository.NewUserRepository(db)
bookRepository repository.BookRepository = repository.NewBookRepository(db)
jwtService service.JWTService = service.NewJWTService()
authService service.AuthService = service.NewAuthService(userRepository)
userService service.UserService = service.NewUserService(userRepository)
bookService service.BookService = service.NewBookService(bookRepository)
authController controller.AuthController = controller.NewAuthController(authService, jwtService)
userController controller.UserController = controller.NewUserController(userService, jwtService)
bookController controller.BookController = controller.NewBookController(bookService, jwtService)
)
func main() {
defer config.CloseDatabaseConnection(db)
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
authRoutes := r.Group("api/auth")
{
authRoutes.POST("/login", authController.Login)
authRoutes.POST("/register", authController.Register)
}
userRoutes := r.Group("api/user", middleware.AuthorizeJWT(jwtService))
{
userRoutes.GET("/profile", userController.Profile)
userRoutes.PUT("/profile", userController.Update)
}
bookRoutes := r.Group("api/books", middleware.AuthorizeJWT(jwtService))
{
bookRoutes.GET("/", bookController.All)
bookRoutes.POST("/", bookController.Insert)
bookRoutes.GET("/:id", bookController.FinByID)
bookRoutes.PUT("/:id", bookController.Update)
bookRoutes.DELETE("/:id", bookController.Delete)
}
r.Run(":8080")
}