user.go 678 B

12345678910111213141516171819202122232425
  1. package context
  2. import (
  3. "github.com/flamego/flamego"
  4. "gogs.io/gogs/internal/database"
  5. )
  6. // ParamsUser is the wrapper type of the target user defined by URL parameter, namely '<username>'.
  7. type ParamsUser struct {
  8. *database.User
  9. }
  10. // InjectParamsUser returns a handler that retrieves target user based on URL parameter '<username>',
  11. // and injects it as *ParamsUser.
  12. func InjectParamsUser() flamego.Handler {
  13. return func(c *Context) {
  14. user, err := database.Handle.Users().GetByUsername(c.Request.Context(), c.Param("username"))
  15. if err != nil {
  16. c.NotFoundOrError(err, "get user by name")
  17. return
  18. }
  19. c.Context.MapTo(&ParamsUser{user}, (*ParamsUser)(nil))
  20. }
  21. }