-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
92 lines (71 loc) · 3.17 KB
/
routes.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
package main
import (
"github.com/gin-gonic/contrib/gzip"
handlers "github.com/ondrejholik/springkilometers/handlers"
mid "github.com/ondrejholik/springkilometers/middleware"
)
func initializeRoutes() {
// Use the setUserStatus middleware for every route to set a flag
// indicating whether the request was from an authenticated user or not
router.Use(mid.SetUserStatus())
router.Use(gzip.Gzip(gzip.BestCompression))
router.Static("/static", "./static")
// Use db in context
// Handle the index route
router.GET("/", handlers.ShowIndexPage)
// Group user related routes together
userRoutes := router.Group("/u")
{
// Handle the GET requests at /u/login
// Show the login page
// Ensure that the user is not logged in by using the middleware
userRoutes.GET("/login", mid.EnsureNotLoggedIn(), handlers.ShowLoginPage)
// Handle POST requests at /u/login
// Ensure that the user is not logged in by using the middleware
userRoutes.POST("/login", mid.EnsureNotLoggedIn(), handlers.PerformLogin)
// Handle GET requests at /u/logout
// Ensure that the user is logged in by using the middleware
userRoutes.GET("/logout", mid.EnsureLoggedIn(), handlers.Logout)
// Handle the GET requests at /u/register
// Show the registration page
// Ensure that the user is not logged in by using the middleware
userRoutes.GET("/register", mid.EnsureNotLoggedIn(), handlers.ShowRegistrationPage)
// Handle POST requests at /u/register
// Ensure that the user is not logged in by using the middleware
userRoutes.POST("/register", mid.EnsureNotLoggedIn(), handlers.Register)
userRoutes.GET("/trips", mid.EnsureLoggedIn(), handlers.MyTrips)
userRoutes.GET("/settings", mid.EnsureLoggedIn(), handlers.ShowSettings)
userRoutes.POST("/settings", mid.EnsureLoggedIn(), handlers.SettingsUpdate)
userRoutes.GET("/view/:id", handlers.ShowUser)
}
tripRoutes := router.Group("/trip")
{
tripRoutes.GET("/all", handlers.ShowTripsPage)
tripRoutes.GET("/create", mid.EnsureLoggedIn(), handlers.ShowTripCreationPage)
tripRoutes.GET("/join/:id", mid.EnsureLoggedIn(), handlers.GetTrip)
tripRoutes.GET("/update/:id", mid.EnsureLoggedIn(), handlers.ShowTripUpdatePage)
tripRoutes.GET("/view/:id", handlers.GetTrip)
tripRoutes.GET("/ws/:tripID", handlers.Chat)
tripRoutes.POST("/create", mid.EnsureLoggedIn(), handlers.CreateTrip)
tripRoutes.POST("/delete/:id", mid.EnsureLoggedIn(), handlers.DeleteTrip)
tripRoutes.POST("/join/:id", mid.EnsureLoggedIn(), handlers.JoinTrip)
tripRoutes.POST("/disjoin/:id", mid.EnsureLoggedIn(), handlers.DisjoinTrip)
tripRoutes.POST("/update/:id", mid.EnsureLoggedIn(), handlers.UpdateTrip)
}
achievmentRoutes := router.Group("/achievments")
{
achievmentRoutes.GET("/", mid.EnsureLoggedIn(), handlers.ShowAchievmentsPage)
}
router.NoRoute(handlers.NoRoute)
}
// SetDBMiddleware --
/*
func SetDBMiddleware(next http.Handler) http.Handler {
database := models.Setup()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timeoutContext, _ := context.WithTimeout(context.Background(), time.Second)
ctx := context.WithValue(r.Context(), "DB", database.WithContext(timeoutContext))
next.ServeHTTP(w, r.WithContext(ctx))
})
}
*/