api.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "gogs.io/gogs/internal/setting"
  13. )
  14. type APIContext struct {
  15. *Context // TODO: Reduce to only needed fields instead of full shadow
  16. // Base URL for the version of API endpoints, e.g. https://try.gogs.io/api/v1
  17. BaseURL string
  18. Org *APIOrganization
  19. }
  20. // FIXME: move this constant to github.com/gogs/go-gogs-client
  21. const DocURL = "https://github.com/gogs/docs-api"
  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": DocURL,
  37. })
  38. }
  39. // NoContent renders the 204 response.
  40. func (c *APIContext) NoContent() {
  41. c.Status(http.StatusNoContent)
  42. }
  43. // NotFound renders the 404 response.
  44. func (c *APIContext) NotFound() {
  45. c.Status(http.StatusNotFound)
  46. }
  47. // ServerError renders the 500 response.
  48. func (c *APIContext) ServerError(title string, err error) {
  49. c.Error(http.StatusInternalServerError, title, err)
  50. }
  51. // NotFoundOrServerError use error check function to determine if the error
  52. // is about not found. It responses with 404 status code for not found error,
  53. // or error context description for logging purpose of 500 server error.
  54. func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) {
  55. if errck(err) {
  56. c.NotFound()
  57. return
  58. }
  59. c.ServerError(title, err)
  60. }
  61. // SetLinkHeader sets pagination link header by given total number and page size.
  62. func (c *APIContext) SetLinkHeader(total, pageSize int) {
  63. page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
  64. links := make([]string, 0, 4)
  65. if page.HasNext() {
  66. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
  67. }
  68. if !page.IsLast() {
  69. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
  70. }
  71. if !page.IsFirst() {
  72. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
  73. }
  74. if page.HasPrevious() {
  75. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, c.Req.URL.Path[1:], page.Previous()))
  76. }
  77. if len(links) > 0 {
  78. c.Header().Set("Link", strings.Join(links, ","))
  79. }
  80. }
  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. }