api.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. 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://gitote.in/api/v1
  19. BaseURL string
  20. Org *APIOrganization
  21. }
  22. const DOC_URL = "https://api.gitote.in"
  23. // Error responses error message to client with given message.
  24. // If status is 500, also it prints error to log.
  25. func (c *APIContext) Error(status int, title string, obj interface{}) {
  26. var message string
  27. if err, ok := obj.(error); ok {
  28. message = err.Error()
  29. } else {
  30. message = obj.(string)
  31. }
  32. if status == http.StatusInternalServerError {
  33. log.Error(3, "%s: %s", title, message)
  34. }
  35. c.JSON(status, map[string]string{
  36. "message": message,
  37. "url": DOC_URL,
  38. })
  39. }
  40. // NotFound renders the 404 response.
  41. func (c *APIContext) NotFound() {
  42. c.Status(http.StatusNotFound)
  43. }
  44. // ServerError renders the 500 response.
  45. func (c *APIContext) ServerError(title string, err error) {
  46. c.Error(http.StatusInternalServerError, title, err)
  47. }
  48. // NotFoundOrServerError 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) NotFoundOrServerError(title string, errck func(error) bool, err error) {
  52. if errck(err) {
  53. c.NotFound()
  54. return
  55. }
  56. c.ServerError(title, err)
  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\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
  64. }
  65. if !page.IsLast() {
  66. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
  67. }
  68. if !page.IsFirst() {
  69. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
  70. }
  71. if page.HasPrevious() {
  72. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, 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: setting.AppURL + "api/v1",
  83. }
  84. ctx.Map(c)
  85. }
  86. }