feat: user_rework (todo -> GetAll)

This commit is contained in:
2025-06-11 12:30:22 +01:00
parent 6aa3870a5c
commit 189534031b
24 changed files with 647 additions and 142 deletions
+35 -17
View File
@@ -48,9 +48,10 @@ type config struct {
}
type mailConfig struct {
exp time.Duration
fromEmail string
sendGrid sendGridConfig
inviteExp time.Duration
passwordRefreshExp time.Duration
fromEmail string
sendGrid sendGridConfig
}
type authConfig struct {
@@ -64,10 +65,11 @@ type basicConfig struct {
}
type tokenConfig struct {
secret string
exp time.Duration
iss string
aud string
secret string
exp time.Duration
refreshExp time.Duration
iss string
aud string
}
type redisConfig struct {
@@ -89,6 +91,8 @@ type dbConfig struct {
maxIdleTime time.Duration
}
// This `mount` function is responsible for setting up the routing for your API
// using the Chi router. Here's a breakdown of what it does:
func (api *api) mount() http.Handler {
r := chi.NewRouter()
// *CORS
@@ -135,29 +139,37 @@ func (api *api) mount() http.Handler {
r.Route("/users", func(r chi.Router) {
r.Put("/activate/{token}", api.activateUserHandler)
//r.Put("/reset-password/{token}", api.resetPasswordHandler)
r.Route("/{id}", func(r chi.Router) {
r.Route("/me", func(r chi.Router) {
r.Use(api.AuthTokenMiddleware)
r.Get("/", api.getUserHandler)
r.Patch("/", api.checkProfileOwnership(api.updateUserHandler))
r.Patch("/password", api.checkProfileOwnership(api.updatePasswordHandler))
r.Put(("/follow"), api.followUserHandler)
r.Put(("/unfollow"), api.unfollowUserHandler)
})
r.Group(func(r chi.Router) {
r.Use(api.AuthTokenMiddleware)
r.Get("/feed", api.getUserFeedHandler)
})
})
r.Route("/authentication", func(r chi.Router) {
r.Post("/user", api.registerUserHandler)
r.Post("/register", api.registerUserHandler)
r.Post("/token", api.createTokenHandler)
r.Post("/forgot-password", api.forgotPasswordHandler)
r.Post("/refresh", api.refreshTokenHandler)
})
})
r.Route("/admin", func(r chi.Router) {
r.Use(api.AuthTokenMiddleware)
r.Route("/users", func(r chi.Router) {
r.Get("/", api.protectAdminRoutes("moderator", api.getUsersHandler))
r.Route("/{id}", func(r chi.Router) {
r.Get("/", api.protectAdminRoutes("moderator", api.getUserByIdHandler))
})
})
})
return r
}
@@ -204,6 +216,12 @@ func (api *api) run(mux http.Handler) error {
return err
}
if userStore, ok := api.store.Users.(*store.UserStore); ok {
if err := userStore.Close(); err != nil {
api.logger.Errorw("error closing userstore", err)
}
}
api.logger.Infow("server has been shutdown")
return nil