-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (72 loc) · 2.58 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
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
87
88
89
90
91
92
package main
import (
"net/http"
"api/models"
"github.com/gin-gonic/gin"
middleware "api/middlewares"
"github.com/gin-contrib/cors"
controllers "api/controllers"
adminControllers "api/controllers/admin"
"github.com/go-playground/validator/v10"
)
// Register custom validation messages
var validate *validator.Validate
var Server *gin.Engine
func init(){
// Initialzie DB Connection
models.ConnectDB()
// Initialize Redis Connection
models.InitRedisClient()
// Initialize Cache Connection
models.InitCache()
validate = validator.New()
Server = gin.Default() //router
}
func main(){
// CORS Setup
corsConfig := cors.DefaultConfig()
corsConfig.AllowOrigins = []string{"http://localhost:3000"}
corsConfig.AllowCredentials = true
// Use CORS Middleware
Server.Use(cors.New(corsConfig))
// Use Logging middleware
Server.Use(middleware.RequestLogger())
// Use Rate Limiting middleware
Server.Use(middleware.RateLimiter())
// Group endpoints
public := Server.Group("/api/auth")
protected := Server.Group("/api/users")
admins := Server.Group("/api/admin")
admins_protected := Server.Group("/api/admin")
// Test API works
Server.GET("/api/ping", func(c *gin.Context){
c.IndentedJSON(http.StatusOK, gin.H{
"message": "pong",
})
})
// Routes
// users
protected.Use(middleware.DeserializeUser())
protected.GET("/me", controllers.GetCurrentUser)
protected.POST("/me/redeem", controllers.RedeemPoints)
protected.GET("/me/transaction-history", middleware.CacheMiddleware(), controllers.ViewTransactions)
protected.GET("/products", middleware.CacheMiddleware(), controllers.GetProducts)
protected.PATCH("/me/change-password", controllers.ChangePassword)
// Auth
public.POST("/register", controllers.CreateAccount)
public.GET("/verify-email/:secret_code", controllers.VerifyEmail)
public.POST("/login", controllers.Login)
public.GET("/logout", controllers.Logout)
public.GET("/sessions/oauth/google", controllers.GoogleOAuth)
public.POST("/forgot-password", controllers.ForgotPassword)
public.PATCH("/reset-password/:resetToken", controllers.ResetPassword)
// Admins
admins_protected.Use(middleware.DeserializeAdmin())
admins.POST("/login", adminControllers.AdminLogin)
admins_protected.GET("/logout", adminControllers.LogoutAdmin)
admins_protected.POST("/product", adminControllers.AddProduct)
admins_protected.PUT("/product/:id", adminControllers.UpdateProduct)
admins_protected.DELETE("/product/:id", adminControllers.DeleteProduct)
admins_protected.GET("/product", adminControllers.GetAllProducts)
Server.Run(":" + "8000") // listen and serve on 0.0.0.0:8000
}