2
0

api.go 2.9 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. "gopkg.in/macaron.v1"
  11. log "unknwon.dev/clog/v2"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/errutil"
  14. )
  15. type APIContext struct {
  16. *Context // TODO: Reduce to only needed fields instead of full shadow
  17. // Base URL for the version of API endpoints, e.g. https://try.gogs.io/api/v1
  18. BaseURL string
  19. Org *APIOrganization
  20. }
  21. // FIXME: move this constant to github.com/gogs/go-gogs-client
  22. const DocURL = "https://github.com/gogs/docs-api"
  23. // NoContent renders the 204 response.
  24. func (c *APIContext) NoContent() {
  25. c.Status(http.StatusNoContent)
  26. }
  27. // NotFound renders the 404 response.
  28. func (c *APIContext) NotFound() {
  29. c.Status(http.StatusNotFound)
  30. }
  31. // ErrorStatus renders error with given status code.
  32. func (c *APIContext) ErrorStatus(status int, err error) {
  33. c.JSON(status, map[string]string{
  34. "message": err.Error(),
  35. "url": DocURL,
  36. })
  37. }
  38. // Error renders the 500 response.
  39. func (c *APIContext) Error(err error, msg string) {
  40. log.ErrorDepth(5, "%s: %v", msg, err)
  41. c.ErrorStatus(http.StatusInternalServerError, err)
  42. }
  43. // Errorf renders the 500 response with formatted message.
  44. func (c *APIContext) Errorf(err error, format string, args ...interface{}) {
  45. c.Error(err, fmt.Sprintf(format, args...))
  46. }
  47. // NotFoundOrError 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) NotFoundOrError(err error, msg string) {
  51. if errutil.IsNotFound(err) {
  52. c.NotFound()
  53. return
  54. }
  55. c.Error(err, msg)
  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\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.Next()))
  63. }
  64. if !page.IsLast() {
  65. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.TotalPages()))
  66. }
  67. if !page.IsFirst() {
  68. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", conf.Server.ExternalURL, c.Req.URL.Path[1:]))
  69. }
  70. if page.HasPrevious() {
  71. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", conf.Server.ExternalURL, 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. BaseURL: conf.Server.ExternalURL + "api/v1",
  82. }
  83. ctx.Map(c)
  84. }
  85. }