1
0

api.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package context
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/cockroachdb/errors"
  7. "github.com/unknwon/paginater"
  8. "gopkg.in/macaron.v1"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/errutil"
  12. )
  13. type APIContext struct {
  14. *Context // TODO: Reduce to only needed fields instead of full shadow
  15. // Base URL for the version of API endpoints, e.g. https://try.gogs.io/api/v1
  16. BaseURL string
  17. Org *APIOrganization
  18. }
  19. // FIXME: move this constant to github.com/gogs/go-gogs-client
  20. const DocURL = "https://github.com/gogs/docs-api"
  21. // NoContent renders the 204 response.
  22. func (c *APIContext) NoContent() {
  23. c.Status(http.StatusNoContent)
  24. }
  25. // NotFound renders the 404 response.
  26. func (c *APIContext) NotFound() {
  27. c.Status(http.StatusNotFound)
  28. }
  29. // ErrorStatus renders error with given status code.
  30. func (c *APIContext) ErrorStatus(status int, err error) {
  31. c.JSON(status, map[string]string{
  32. "message": err.Error(),
  33. "url": DocURL,
  34. })
  35. }
  36. // Error renders the 500 response.
  37. func (c *APIContext) Error(err error, msg string) {
  38. log.ErrorDepth(4, "%s: %v", msg, err)
  39. c.ErrorStatus(
  40. http.StatusInternalServerError,
  41. errors.New("Something went wrong, please check the server logs for more information."),
  42. )
  43. }
  44. // Errorf renders the 500 response with formatted message.
  45. func (c *APIContext) Errorf(err error, format string, args ...any) {
  46. c.Error(err, fmt.Sprintf(format, args...))
  47. }
  48. // NotFoundOrError use error check function to determine if the error
  49. // is about not found. It responses with 404 status code for not found error,
  50. // or error context description for logging purpose of 500 server error.
  51. func (c *APIContext) NotFoundOrError(err error, msg string) {
  52. if errutil.IsNotFound(err) {
  53. c.NotFound()
  54. return
  55. }
  56. c.Error(err, msg)
  57. }
  58. // SetLinkHeader sets pagination link header by given total number and page size.
  59. func (c *APIContext) SetLinkHeader(total, pageSize int) {
  60. page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
  61. links := make([]string, 0, 4)
  62. if page.HasNext() {
  63. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.Next()))
  64. }
  65. if !page.IsLast() {
  66. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.TotalPages()))
  67. }
  68. if !page.IsFirst() {
  69. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", conf.Server.ExternalURL, c.Req.URL.Path[1:]))
  70. }
  71. if page.HasPrevious() {
  72. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.Previous()))
  73. }
  74. if len(links) > 0 {
  75. c.Header().Set("Link", strings.Join(links, ","))
  76. }
  77. }
  78. func APIContexter() macaron.Handler {
  79. return func(ctx *Context) {
  80. c := &APIContext{
  81. Context: ctx,
  82. BaseURL: conf.Server.ExternalURL + "api/v1",
  83. }
  84. ctx.Map(c)
  85. }
  86. }