api.go 2.7 KB

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