api.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package context
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/setting"
  10. "net/http"
  11. "strings"
  12. "gitlab.com/yoginth/paginater"
  13. log "gopkg.in/clog.v1"
  14. "gopkg.in/macaron.v1"
  15. )
  16. // APIContext is a specific macaron context for API service
  17. type APIContext struct {
  18. *Context // TODO: Reduce to only needed fields instead of full shadow
  19. // Base URL for the version of API endpoints, e.g. https://gitote.example.com/api/v1
  20. BaseURL string
  21. Org *APIOrganization
  22. }
  23. // DocURL api doc url
  24. const DocURL = "https://gitlab.com/gitote/gitote/tree/master/docs/API"
  25. // Error responses error message to client with given message.
  26. // If status is 500, also it prints error to log.
  27. func (c *APIContext) Error(status int, title string, obj interface{}) {
  28. var message string
  29. if err, ok := obj.(error); ok {
  30. message = err.Error()
  31. } else {
  32. message = obj.(string)
  33. }
  34. if status == http.StatusInternalServerError {
  35. log.Error(3, "%s: %s", title, message)
  36. }
  37. c.JSON(status, map[string]string{
  38. "message": message,
  39. "url": DocURL,
  40. })
  41. }
  42. // NotFound renders the 404 response.
  43. func (c *APIContext) NotFound() {
  44. c.Status(http.StatusNotFound)
  45. }
  46. // ServerError renders the 500 response.
  47. func (c *APIContext) ServerError(title string, err error) {
  48. c.Error(http.StatusInternalServerError, title, err)
  49. }
  50. // NotFoundOrServerError use error check function to determine if the error
  51. // is about not found. It responses with 404 status code for not found error,
  52. // or error context description for logging purpose of 500 server error.
  53. func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) {
  54. if errck(err) {
  55. c.NotFound()
  56. return
  57. }
  58. c.ServerError(title, err)
  59. }
  60. // SetLinkHeader sets pagination link header by given total number and page size.
  61. func (c *APIContext) SetLinkHeader(total, pageSize int) {
  62. page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
  63. links := make([]string, 0, 4)
  64. if page.HasNext() {
  65. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
  66. }
  67. if !page.IsLast() {
  68. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
  69. }
  70. if !page.IsFirst() {
  71. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
  72. }
  73. if page.HasPrevious() {
  74. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, c.Req.URL.Path[1:], page.Previous()))
  75. }
  76. if len(links) > 0 {
  77. c.Header().Set("Link", strings.Join(links, ","))
  78. }
  79. }
  80. // APIContexter returns apicontext as macaron middleware
  81. func APIContexter() macaron.Handler {
  82. return func(ctx *Context) {
  83. c := &APIContext{
  84. Context: ctx,
  85. BaseURL: setting.AppURL + "api/v1",
  86. }
  87. ctx.Map(c)
  88. }
  89. }