-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
103 lines (87 loc) · 3.01 KB
/
router.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package server
import (
"fmt"
rateLimit "github.com/JGLTechnologies/gin-rate-limit"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func (s *Server) defineRoutes(router *gin.Engine) {
store := rateLimit.InMemoryStore(&rateLimit.InMemoryOptions{})
limitRate := limitRateForPasswordReset(store)
apirouter := router.Group("/api/v1")
apirouter.POST("/auth/signup", s.HandleSignup())
apirouter.POST("/auth/login", s.handleLogin())
apirouter.GET("/fb/auth", s.handleFBLogin())
apirouter.GET("fb/callback", s.fbCallbackHandler())
apirouter.GET("/google/login", s.HandleGoogleOauthLogin())
apirouter.GET("/google/callback", s.HandleGoogleCallback())
apirouter.GET("/app/auth", s.HandleAppleOauthLogin())
apirouter.GET("/apple/auth", s.HandleAppleCallback())
apirouter.GET("/verifyEmail/:token", s.HandleVerifyEmail())
apirouter.POST("/password/forgot", limitRate, s.SendEmailForPasswordReset())
apirouter.POST("/password/reset/:token", s.ResetPassword())
authorized := apirouter.Group("/")
authorized.Use(s.Authorize())
authorized.GET("/logout", s.handleLogout())
authorized.GET("/users", s.handleGetUsers())
authorized.DELETE("/users", s.handleDeleteUserByEmail())
authorized.PUT("/me/update", s.handleUpdateUserDetails())
authorized.GET("/me", s.handleShowProfile())
authorized.POST("/user/medications", s.handleCreateMedication())
authorized.GET("/user/medications/:id", s.handleGetMedDetail())
authorized.GET("/user/medications", s.handleGetAllMedications())
authorized.PUT("/user/medications/:medicationID", s.handleUpdateMedication())
authorized.GET("/user/medications/next", s.handleGetNextMedication())
}
func (s *Server) setupRouter() *gin.Engine {
ginMode := os.Getenv("GIN_MODE")
if ginMode == "test" {
r := gin.New()
s.defineRoutes(r)
return r
}
r := gin.New()
staticFiles := "server/templates/static"
htmlFiles := "server/templates/*.html"
if s.Config.Env == "test" {
_, b, _, _ := runtime.Caller(0)
basepath := filepath.Dir(b)
staticFiles = basepath + "/templates/static"
htmlFiles = basepath + "/templates/*.html"
}
r.StaticFS("static", http.Dir(staticFiles))
r.LoadHTMLGlob(htmlFiles)
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// By default gin.DefaultWriter = os.Stdout
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// your custom format
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
r.Use(gin.Recovery())
// setup cors
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST", "GET", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
s.defineRoutes(r)
return r
}