-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
60 lines (49 loc) · 1.91 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
53
54
55
56
57
58
59
60
package main
import (
"github.com/gin-gonic/gin"
"github.com/johnkeychishugi/golang-api/config"
"github.com/johnkeychishugi/golang-api/controllers"
"github.com/johnkeychishugi/golang-api/middlewares"
"github.com/johnkeychishugi/golang-api/repository"
"github.com/johnkeychishugi/golang-api/services"
"gorm.io/gorm"
)
var (
//Database
db *gorm.DB = config.SetUpDatabaseConnection()
//Repositories
userRepository repository.UserRepository = repository.NewUserRepository(db)
bookRepository repository.BookRepository = repository.NewBookRepository(db)
//Services
jwtService services.JWTService = services.NewJWTService()
userService services.UserService = services.NewUserService(userRepository)
authService services.AuthService = services.NewAuthService(userRepository)
bookService services.BookService = services.NewBookService(bookRepository)
//Conrollers
authController controllers.AuthController = controllers.NewAuthController(authService, jwtService)
userController controllers.UserController = controllers.NewUserController(userService, jwtService)
bookController controllers.BookController = controllers.NewBookController(bookService, jwtService)
)
func main() {
defer config.CloseDatabaseConnection(db)
server := gin.Default()
authRoutes := server.Group("/api/auth")
{
authRoutes.POST("/login", authController.Login)
authRoutes.POST("/register", authController.Register)
}
userRoutes := server.Group("/api/user", middlewares.AuthorizeJWT(jwtService))
{
userRoutes.GET("/profile", userController.Profile)
userRoutes.PUT("/profile", userController.Update)
}
bookRoutes := server.Group("/api/books", middlewares.AuthorizeJWT(jwtService))
{
bookRoutes.GET("/", bookController.All)
bookRoutes.POST("/", bookController.Insert)
bookRoutes.GET("/:id", bookController.FindByID)
bookRoutes.PUT("/:id", bookController.Update)
bookRoutes.DELETE("/:id", bookController.Delete)
}
server.Run()
}