-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (42 loc) · 1.44 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
package main
import (
"github.com/abcd-edu/gentoo-posts/internal/configs"
"github.com/abcd-edu/gentoo-posts/internal/models"
"github.com/abcd-edu/gentoo-posts/internal/services"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
configs.InitializeViper()
models.InitializeDB()
router := gin.Default()
router.Use(CORSMiddleware())
v1 := router.Group("/v1")
v1.Use(CORSMiddleware())
{
v1.GET("/", services.HandleMain)
v1.POST("/register", services.HandleRegistration)
v1.POST("/submit-post", services.HandlePostContent)
v1.GET("/user/profile", services.GetUserPosts)
v1.GET("/user/timeline", services.GetUserTimeline)
v1.POST("/user/mute", services.MuteUser)
v1.POST("/user/ban", services.BanUser)
v1.GET("/post", services.GetPost)
v1.GET("/post/latest", services.GetLatestPosts)
}
port := viper.GetString("port")
router.Run(":" + port)
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}