api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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
  18. Org *APIOrganization
  19. }
  20. // FIXME: move to gitlab.com/gitote/gitote-client
  21. const DOC_URL = "https://gitlab.com/gitote/gitote-client/wiki"
  22. // Error responses error message to client with given message.
  23. // If status is 500, also it prints error to log.
  24. func (c *APIContext) Error(status int, title string, obj interface{}) {
  25. var message string
  26. if err, ok := obj.(error); ok {
  27. message = err.Error()
  28. } else {
  29. message = obj.(string)
  30. }
  31. if status == http.StatusInternalServerError {
  32. log.Error(3, "%s: %s", title, message)
  33. }
  34. c.JSON(status, map[string]string{
  35. "message": message,
  36. "url": DOC_URL,
  37. })
  38. }
  39. // NotFound renders the 404 response.
  40. func (c *APIContext) NotFound() {
  41. c.Status(http.StatusNotFound)
  42. }
  43. // ServerError renders the 500 response.
  44. func (c *APIContext) ServerError(title string, err error) {
  45. c.Error(http.StatusInternalServerError, title, err)
  46. }
  47. // NotFoundOrServerError use error check function to determine if the error
  48. // is about not found. It responses with 404 status code for not found error,
  49. // or error context description for logging purpose of 500 server error.
  50. func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) {
  51. if errck(err) {
  52. c.NotFound()
  53. return
  54. }
  55. c.ServerError(title, err)
  56. }
  57. // SetLinkHeader sets pagination link header by given total number and page size.
  58. func (c *APIContext) SetLinkHeader(total, pageSize int) {
  59. page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
  60. links := make([]string, 0, 4)
  61. if page.HasNext() {
  62. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
  63. }
  64. if !page.IsLast() {
  65. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
  66. }
  67. if !page.IsFirst() {
  68. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
  69. }
  70. if page.HasPrevious() {
  71. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, c.Req.URL.Path[1:], page.Previous()))
  72. }
  73. if len(links) > 0 {
  74. c.Header().Set("Link", strings.Join(links, ","))
  75. }
  76. }
  77. func APIContexter() macaron.Handler {
  78. return func(ctx *Context) {
  79. c := &APIContext{
  80. Context: ctx,
  81. }
  82. ctx.Map(c)
  83. }
  84. }