-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (69 loc) · 3.04 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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/rs/cors"
"github.com/garylow2001/GossipGo-Backend/configs"
"github.com/garylow2001/GossipGo-Backend/controllers"
"github.com/garylow2001/GossipGo-Backend/initializers"
"github.com/garylow2001/GossipGo-Backend/middleware"
)
func init() {
initializers.LoadEnvVariables()
initializers.ConnectToDB()
initializers.SyncDatabase()
}
func main() {
router := gin.Default()
setUpCORS(router)
setUpRouters(router)
router.Run()
}
func setUpCORS(router *gin.Engine) {
c := cors.New(cors.Options{
AllowedOrigins: configs.CORSAllowedOrigins,
AllowedMethods: configs.CORSAllowedMethods,
AllowedHeaders: configs.CORSAllowedHeaders,
AllowCredentials: true, //this is to allow cookies to be included in requests from frontend
})
router.Use(func(context *gin.Context) {
c.HandlerFunc(context.Writer, context.Request)
// CORS preflight check
if context.Request.Method != "OPTIONS" {
context.Next()
} else {
context.AbortWithStatus(http.StatusOK)
}
})
}
func setUpRouters(router *gin.Engine) {
// User endpoints
router.GET("/validate", middleware.JWTAuthMiddleware, controllers.Validate)
router.GET("/users", middleware.JWTAuthMiddleware, controllers.GetUsers)
router.POST("/users/signup", controllers.Signup)
router.POST("/users/login", controllers.Login)
router.POST("/users/logout", controllers.Logout)
router.GET("/users/:id", controllers.GetUser) // Not in use
router.PUT("/users/:id", controllers.UpdateUser) // Not in use
router.DELETE("/users/:id", controllers.DeleteUser) // Not in use
// Thread endpoints
router.GET("/threads", controllers.GetThreads)
router.GET("/threads/category/:category", controllers.GetThreadsByCategory)
router.GET("/threads/recent", controllers.GetThreadsByMostRecent)
router.GET("/threads/popular", controllers.GetThreadsByMostPopular)
router.POST("/threads", middleware.JWTAuthMiddleware, controllers.CreateThread)
router.POST("/threads/:threadID/like", middleware.JWTAuthMiddleware, controllers.LikeThread)
router.DELETE("/threads/:threadID/like", middleware.JWTAuthMiddleware, controllers.UnlikeThread)
router.GET("/threads/:threadID", controllers.GetThread)
router.PUT("/threads/:threadID", middleware.JWTAuthMiddleware, controllers.UpdateThread)
router.DELETE("/threads/:threadID", middleware.JWTAuthMiddleware, controllers.DeleteThread)
// Comment endpoints
threadGroup := router.Group("/threads/:threadID")
threadGroup.POST("/comments", middleware.JWTAuthMiddleware, controllers.CreateComment)
threadGroup.POST("/comments/:commentID/like", middleware.JWTAuthMiddleware, controllers.LikeComment)
threadGroup.DELETE("/comments/:commentID/like", middleware.JWTAuthMiddleware, controllers.UnlikeComment)
threadGroup.GET("/comments", controllers.GetComments)
threadGroup.GET("/comments/:commentID", controllers.GetComment)
threadGroup.PUT("/comments/:commentID", middleware.JWTAuthMiddleware, controllers.UpdateComment)
threadGroup.DELETE("/comments/:commentID", middleware.JWTAuthMiddleware, controllers.DeleteComment)
}