api.go 2.4 KB

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