-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
85 lines (71 loc) · 2.09 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
package main
import (
"github.com/itsjamie/gin-cors"
"github.com/rorikurniadi/go-task/models"
"github.com/rorikurniadi/go-task/resources"
"github.com/gin-gonic/gin"
)
func main() {
db, err := models.InitDB()
if err != nil {
panic(err)
}
defer db.Close()
statusResource := resources.NewStatusStorage(db)
authResource := resources.AuthDB(db)
taskResource := resources.TaskDB(db)
tagResource := resources.TagDB(db)
noteResource := resources.NoteDB(db)
r := gin.Default()
// handle CORS
r.Use(cors.Middleware(cors.Config{
Origins: "*",
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "",
Credentials: true,
ValidateHeaders: false,
}))
v1 := r.Group("api/v1")
{
v1.POST("/register", authResource.Register)
v1.POST("/login", authResource.Login().LoginHandler)
v1.GET("/logout", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Logout",
})
})
}
auth := r.Group("api/v1")
auth.Use(authResource.Login().MiddlewareFunc())
{
auth.GET("/refresh_token", authResource.Login().RefreshHandler)
//
auth.GET("/users/current", authResource.CurrentUser)
auth.GET("/users", authResource.Get)
// task
auth.GET("/tasks", taskResource.Get)
auth.GET("/tasks/:id", taskResource.Show)
auth.POST("/tasks", taskResource.Store)
auth.PUT("/tasks/:id", taskResource.Update)
auth.DELETE("/tasks/:id", taskResource.Destroy)
auth.GET("/tasks/:id/notes", noteResource.GetByTask)
// tag
auth.GET("/tags", tagResource.Get)
auth.GET("/tags/:id", tagResource.Show)
auth.POST("/tags", tagResource.Store)
auth.PUT("/tags/:id", tagResource.Update)
auth.DELETE("/tags/:id", tagResource.Destroy)
// note
auth.GET("/notes", noteResource.Get)
auth.GET("/notes/:id", noteResource.Show)
auth.POST("/notes", noteResource.Store)
auth.PUT("/notes/:id", noteResource.Update)
auth.DELETE("/notes/:id", noteResource.Destroy)
// statuses
auth.GET("/statuses", statusResource.Get)
auth.GET("/statuses/:id", statusResource.Show)
auth.POST("/statuses", statusResource.Store)
}
r.Run(":8080")
}