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

api.go 3.3 kB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 a 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. // Error responses error message to client with given message.
  45. // If status is 500, also it prints error to log.
  46. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  47. var message string
  48. if err, ok := obj.(error); ok {
  49. message = err.Error()
  50. } else {
  51. message = obj.(string)
  52. }
  53. if status == 500 {
  54. log.Error(4, "%s: %s", title, message)
  55. }
  56. ctx.JSON(status, APIError{
  57. Message: message,
  58. URL: base.DocURL,
  59. })
  60. }
  61. // SetLinkHeader sets pagination link header by given total number and page size.
  62. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  63. page := paginater.New(total, pageSize, ctx.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\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
  67. }
  68. if !page.IsLast() {
  69. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
  70. }
  71. if !page.IsFirst() {
  72. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
  73. }
  74. if page.HasPrevious() {
  75. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
  76. }
  77. if len(links) > 0 {
  78. ctx.Header().Set("Link", strings.Join(links, ","))
  79. }
  80. }
  81. // APIContexter returns apicontext as macaron middleware
  82. func APIContexter() macaron.Handler {
  83. return func(c *Context) {
  84. ctx := &APIContext{
  85. Context: c,
  86. }
  87. c.Map(ctx)
  88. }
  89. }
  90. // ReferencesGitRepo injects the GitRepo into the Context
  91. func ReferencesGitRepo() macaron.Handler {
  92. return func(ctx *APIContext) {
  93. // Empty repository does not have reference information.
  94. if ctx.Repo.Repository.IsBare {
  95. return
  96. }
  97. // For API calls.
  98. if ctx.Repo.GitRepo == nil {
  99. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  100. gitRepo, err := git.OpenRepository(repoPath)
  101. if err != nil {
  102. ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
  103. return
  104. }
  105. ctx.Repo.GitRepo = gitRepo
  106. }
  107. }
  108. }