-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
86 lines (73 loc) · 3.09 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/putukrisna6/golang-api/cache"
"github.com/putukrisna6/golang-api/config"
"github.com/putukrisna6/golang-api/controller"
"github.com/putukrisna6/golang-api/middleware"
"github.com/putukrisna6/golang-api/repository"
"github.com/putukrisna6/golang-api/service"
"gorm.io/gorm"
)
var (
db *gorm.DB = config.SetupDatabaseConnection()
userRepository repository.UserRepository = repository.NewUserRepository(db)
bookRepository repository.BookRepository = repository.NewBookRepository(db)
receiptRepository repository.ReceiptRepository = repository.NewReceiptRepository(db)
rd *redis.Client = config.SetupRedisConnection()
receiptCache cache.ReceiptCache = cache.NewReceiptCache(rd, 1000) // random expiration lmao
jwtService service.JWTService = service.NewJWTService()
authService service.AuthService = service.NewAuthService(userRepository)
userService service.UserService = service.NewUserService(userRepository)
bookService service.BookService = service.NewBookService(bookRepository)
receiptService service.ReceiptService = service.NewReceiptService(receiptRepository)
authController controller.AuthController = controller.NewAuthController(authService, jwtService)
userController controller.UserController = controller.NewUserController(userService, jwtService)
bookController controller.BookController = controller.NewBookController(bookService, jwtService)
receiptController controller.ReceiptController = controller.NewReceiptController(receiptService, receiptCache)
)
func main() {
defer config.CloseDatabaseConnection(db)
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowMethods: []string{"POST", "PUT", "PATCH", "GET", "DELETE", "HEAD", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowAllOrigins: true,
AllowCredentials: true,
}))
authRoutes := r.Group("/api/auth")
{
authRoutes.POST("login", authController.Login)
authRoutes.POST("register", authController.Register)
}
userRoutes := r.Group("/api/users", middleware.AuthorizeJWT(jwtService))
{
userRoutes.GET("profile", userController.Get)
userRoutes.PUT("", userController.Update)
}
bookRoutes := r.Group("/api/books", middleware.AuthorizeJWT(jwtService))
{
bookRoutes.GET("", bookController.All)
bookRoutes.POST("", bookController.Insert)
bookRoutes.GET("/:id", bookController.Get)
bookRoutes.PUT("", bookController.Update)
bookRoutes.DELETE("/:id", bookController.Delete)
}
receiptRoutes := r.Group("/api/receipts")
{
receiptRoutes.GET("/all", receiptController.All)
receiptRoutes.POST("", receiptController.Insert)
receiptRoutes.GET("/:id", receiptController.Show)
receiptRoutes.PUT("", receiptController.Update)
receiptRoutes.DELETE("/:id", receiptController.Delete)
}
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, World",
})
})
r.Run()
}