api.go 3.0 KB

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