diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 20:20:36 -0500 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 20:20:36 -0500 |
commit | 831b6f0167b9c1747d128b4a5a648d4de42ff0a9 (patch) | |
tree | 1042411d1027bdf18a4c8884f595c284e8b5570d /api/internal/router/router.go | |
parent | 29f83e05270d0012ad9f273ac3364106fcff5f50 (diff) |
Refactor router and middleware packages
- Move middleware functions from `main.go` to `middleware.go` in the `middleware` package.
- Update import statements in `main.go` and use the `router` package instead of the `controllers` package.
```
Refactor router and middleware packages
Move middleware functions from `main.go` to `middleware.go` in the `middleware` package. Update import statements in `main.go` and use the `router` package instead of the `controllers` package.
```
Diffstat (limited to 'api/internal/router/router.go')
-rw-r--r-- | api/internal/router/router.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/api/internal/router/router.go b/api/internal/router/router.go new file mode 100644 index 0000000..adf96d0 --- /dev/null +++ b/api/internal/router/router.go | |||
@@ -0,0 +1,42 @@ | |||
1 | package router | ||
2 | |||
3 | import ( | ||
4 | "github.com/gin-gonic/gin" | ||
5 | "water/api/internal/controllers" | ||
6 | "water/api/internal/middleware" | ||
7 | ) | ||
8 | |||
9 | func SetupRouter() *gin.Engine { | ||
10 | // Disable Console Color | ||
11 | // gin.DisableConsoleColor() | ||
12 | r := gin.Default() | ||
13 | r.Use(middleware.CORSMiddleware()) | ||
14 | r.Use(gin.Logger()) | ||
15 | r.Use(gin.Recovery()) | ||
16 | |||
17 | api := r.Group("api/v1") | ||
18 | |||
19 | api.POST("/auth", controllers.AuthHandler) | ||
20 | |||
21 | user := api.Group("/user/:uuid") | ||
22 | user.Use(middleware.TokenRequired()) | ||
23 | { | ||
24 | user.GET("", controllers.GetUser) | ||
25 | user.GET("preferences", controllers.GetUserPreferences) | ||
26 | user.PATCH("preferences", controllers.UpdateUserPreferences) | ||
27 | } | ||
28 | |||
29 | stats := api.Group("/stats") | ||
30 | stats.Use(middleware.TokenRequired()) | ||
31 | { | ||
32 | stats.GET("/", controllers.GetAllStatistics) | ||
33 | stats.POST("/", controllers.PostNewStatistic) | ||
34 | stats.GET("weekly/", controllers.GetWeeklyStatistics) | ||
35 | stats.GET("daily/", controllers.GetDailyUserStatistics) | ||
36 | stats.GET("user/:uuid", controllers.GetUserStatistics) | ||
37 | stats.PATCH("user/:uuid", controllers.UpdateUserStatistic) | ||
38 | stats.DELETE("user/:uuid", controllers.DeleteUserStatistic) | ||
39 | } | ||
40 | |||
41 | return r | ||
42 | } \ No newline at end of file | ||