-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (31 loc) · 1.03 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
package main
import (
//"net/http"
"github.com/gin-gonic/gin"
"github.com/mugraph/anchors-server/controllers"
"github.com/mugraph/anchors-server/models"
)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
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()
}
}
func main() {
router := gin.New()
router.Use(CORSMiddleware())
models.ConnectDatabase()
models.Populate(models.DB)
router.GET("/chapters", controllers.FindChapters)
router.POST("/chapters", controllers.CreateChapter)
router.GET("/tours", controllers.FindTours)
router.GET("/tour/:id", controllers.FindTour)
router.Run()
}