You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "strings"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/Unknwon/paginater"
  14. macaron "gopkg.in/macaron.v1"
  15. )
  16. // APIContext is a specific macaron context for API service
  17. type APIContext struct {
  18. *Context
  19. Org *APIOrganization
  20. }
  21. // APIError is error format response
  22. // swagger:response error
  23. type APIError struct {
  24. Message string `json:"message"`
  25. URL string `json:"url"`
  26. }
  27. // APIValidationError is error format response related to input validation
  28. // swagger:response validationError
  29. type APIValidationError struct {
  30. Message string `json:"message"`
  31. URL string `json:"url"`
  32. }
  33. //APIEmpty is an empty response
  34. // swagger:response empty
  35. type APIEmpty struct{}
  36. //APIForbiddenError is a forbidden error response
  37. // swagger:response forbidden
  38. type APIForbiddenError struct {
  39. APIError
  40. }
  41. //APINotFound is a not found empty response
  42. // swagger:response notFound
  43. type APINotFound struct{}
  44. //APIRedirect is a redirect response
  45. // swagger:response redirect
  46. type APIRedirect struct{}
  47. // Error responses error message to client with given message.
  48. // If status is 500, also it prints error to log.
  49. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  50. var message string
  51. if err, ok := obj.(error); ok {
  52. message = err.Error()
  53. } else {
  54. message = obj.(string)
  55. }
  56. if status == 500 {
  57. log.Error(4, "%s: %s", title, message)
  58. }
  59. ctx.JSON(status, APIError{
  60. Message: message,
  61. URL: base.DocURL,
  62. })
  63. }
  64. // SetLinkHeader sets pagination link header by given total number and page size.
  65. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  66. page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
  67. links := make([]string, 0, 4)
  68. if page.HasNext() {
  69. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
  70. }
  71. if !page.IsLast() {
  72. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
  73. }
  74. if !page.IsFirst() {
  75. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
  76. }
  77. if page.HasPrevious() {
  78. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
  79. }
  80. if len(links) > 0 {
  81. ctx.Header().Set("Link", strings.Join(links, ","))
  82. }
  83. }
  84. // APIContexter returns apicontext as macaron middleware
  85. func APIContexter() macaron.Handler {
  86. return func(c *Context) {
  87. ctx := &APIContext{
  88. Context: c,
  89. }
  90. c.Map(ctx)
  91. }
  92. }
  93. // ReferencesGitRepo injects the GitRepo into the Context
  94. func ReferencesGitRepo() macaron.Handler {
  95. return func(ctx *APIContext) {
  96. // Empty repository does not have reference information.
  97. if ctx.Repo.Repository.IsBare {
  98. return
  99. }
  100. // For API calls.
  101. if ctx.Repo.GitRepo == nil {
  102. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  103. gitRepo, err := git.OpenRepository(repoPath)
  104. if err != nil {
  105. ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
  106. return
  107. }
  108. ctx.Repo.GitRepo = gitRepo
  109. }
  110. }
  111. }