-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
136 lines (118 loc) · 4.94 KB
/
server.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package http
import (
"net/http"
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/unnamedxaer/gymm-api/usecases"
)
type contextKey int
const (
contextKeyUserID contextKey = iota
)
type App struct {
l *zerolog.Logger
authUsecases usecases.IAuthUsecases
userUsecases usecases.IUserUseCases
exerciseUsecases usecases.IExerciseUseCases
trainingUsecases usecases.ITrainingUsecases
Router *mux.Router
Validate *validator.Validate
jwtKey []byte
mailer usecases.Mailer
}
func NewServer(
logger *zerolog.Logger,
authRepo usecases.AuthRepo,
userRepo usecases.UserRepo,
exerciseRepo usecases.ExerciseRepo,
trainingRepo usecases.TrainingRepo,
validate *validator.Validate,
jwtKey []byte,
mailer usecases.Mailer,
) *App {
var authUsecases usecases.IAuthUsecases = usecases.NewAuthUsecases(logger, authRepo)
var userUsecases usecases.IUserUseCases = usecases.NewUserUseCases(userRepo)
var exerciseUsecases usecases.IExerciseUseCases = usecases.NewExerciseUseCases(exerciseRepo)
var trainingUsecases usecases.ITrainingUsecases = usecases.NewTrainingUseCases(trainingRepo)
router := mux.NewRouter()
router.StrictSlash(true)
app := App{
l: logger,
authUsecases: authUsecases,
userUsecases: userUsecases,
exerciseUsecases: exerciseUsecases,
trainingUsecases: trainingUsecases,
Router: router,
Validate: validate,
jwtKey: jwtKey,
mailer: mailer,
}
return &app
}
func (app *App) AddHandlers() {
app.Router.HandleFunc("/login", app.Login).Methods(http.MethodPost)
app.Router.HandleFunc("/logout", app.Logout).Methods(http.MethodGet)
app.Router.HandleFunc("/register", app.Register).Methods(http.MethodPost)
app.Router.HandleFunc("/sessions", chainMiddlewares(app.GetSessions, app.checkAuthenticated)).Methods(http.MethodGet)
app.Router.HandleFunc("/logout-session", chainMiddlewares(app.LogoutSession, app.checkAuthenticated)).Methods(http.MethodPost)
app.Router.HandleFunc("/logout-all", chainMiddlewares(app.LogoutAllSessions, app.checkAuthenticated)).Methods(http.MethodPost)
app.Router.HandleFunc("/health", chainMiddlewares(app.Health, app.checkAuthenticated)).Methods(http.MethodGet)
// password
passwordRouter := app.Router.PathPrefix("/password").Subrouter()
passwordRouter.HandleFunc("/change", chainMiddlewares(app.ChangePassword, app.checkAuthenticated)).Methods(http.MethodPost)
passwordRouter.HandleFunc("/reset/{requestID:[0-9a-zA-Z]+}", app.UpdatePasswordOnResetRequest).Methods(http.MethodPatch)
passwordRouter.HandleFunc("/reset", app.AddResetPasswordRequest).Methods(http.MethodPost)
exercisesRouter := app.Router.PathPrefix("/exercises").Subrouter()
exercisesRouter.HandleFunc(
"/{exerciseID:[0-9a-zA-Z]+}",
chainMiddlewares(app.GetExerciseByID, app.checkAuthenticated)).Methods(http.MethodGet)
exercisesRouter.HandleFunc(
"",
chainMiddlewares(app.GetExercisesByName, app.checkAuthenticated)).Methods(http.MethodGet)
exercisesRouter.HandleFunc(
"/{exerciseID:[0-9a-zA-Z]+}",
chainMiddlewares(app.UpdateExercise, app.checkAuthenticated)).Methods(http.MethodPatch)
exercisesRouter.HandleFunc(
"",
chainMiddlewares(app.CreateExercise, app.checkAuthenticated)).Methods(http.MethodPost)
// training
trainingRouter := app.Router.PathPrefix("/trainings").Subrouter()
trainingRouter.HandleFunc(
"",
chainMiddlewares(app.GetUserTrainings, app.checkAuthenticated)).Methods(http.MethodGet)
trainingRouter.HandleFunc(
"",
chainMiddlewares(app.StartTraining, app.checkAuthenticated)).Methods(http.MethodPost)
trainingRouter.HandleFunc(
"/{trainingID:[0-9a-zA-Z]+}",
chainMiddlewares(app.GetTrainingByID, app.checkAuthenticated)).Methods(http.MethodGet)
trainingRouter.HandleFunc(
"/{trainingID:[0-9a-zA-Z]+}/end",
chainMiddlewares(app.EndTraining, app.checkAuthenticated)).Methods(http.MethodPatch)
// training exercise
trainingExerciseRouter := trainingRouter.PathPrefix("/{trainingID:[0-9a-zA-Z]+}/exercises").Subrouter()
trainingExerciseRouter.HandleFunc(
"",
chainMiddlewares(app.StartTrainingExercise, app.checkAuthenticated)).Methods(http.MethodPost)
trainingExerciseRouter.HandleFunc(
"/{exerciseID:[0-9a-zA-Z]+}/end",
chainMiddlewares(app.EndTrainingExercise, app.checkAuthenticated)).Methods(http.MethodPatch)
// training set
trainingSetRouter := trainingExerciseRouter.PathPrefix("/{exerciseID:[0-9a-zA-Z]+}/sets").Subrouter()
trainingSetRouter.HandleFunc(
"",
chainMiddlewares(app.AddTrainingSetExercise, app.checkAuthenticated)).Methods(http.MethodPost)
app.Router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
logDebug(app.l, r, nil)
rw.WriteHeader(http.StatusMethodNotAllowed)
})
}
func (app *App) Run(addr string) {
srv := &http.Server{
Addr: addr,
Handler: suffixMiddleware(app.Router),
}
app.l.Info().Msg("server is up and running at " + addr)
app.l.Error().Stack().Err(srv.ListenAndServe()).Msg("")
}