-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (45 loc) · 1.59 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/yurdigrfnn/api-todo-auth/controllers/todocontroller"
"github.com/yurdigrfnn/api-todo-auth/controllers/usercontroller"
"github.com/yurdigrfnn/api-todo-auth/initializers"
"github.com/yurdigrfnn/api-todo-auth/middleware"
)
func init() {
initializers.LoadEnv()
initializers.ConnectDatabase()
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
if origin == "" {
origin = "*"
}
c.Writer.Header().Set("Access-Control-Allow-Origin", 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, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func main() {
r := gin.Default()
// cors middleware
r.Use(CORSMiddleware())
// USER ROUTER
r.POST("api/register", usercontroller.Signup)
r.POST("api/signin", usercontroller.Signin)
r.GET("api/validate", middleware.RequireAuth, usercontroller.Validate)
r.GET("api/logout", usercontroller.Logout)
//TODO ROUTER
r.GET("api/todo", middleware.RequireAuth, todocontroller.Index)
r.POST("api/todo", middleware.RequireAuth, todocontroller.Create)
r.PUT("api/todo/:id", middleware.RequireAuth, todocontroller.Update)
r.DELETE("api/todo/:id", middleware.RequireAuth, todocontroller.Destroy)
r.Run()
}