123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
- // Copyright 2018 - Present, Gitote. All rights reserved.
- //
- // This source code is licensed under the MIT license found in the
- // LICENSE file in the root directory of this source tree.
- package context
- import (
- "fmt"
- "gitote/gitote/pkg/setting"
- "net/http"
- "strings"
- "gitlab.com/yoginth/paginater"
- log "gopkg.in/clog.v1"
- "gopkg.in/macaron.v1"
- )
- // APIContext is a specific macaron context for API service
- type APIContext struct {
- *Context // TODO: Reduce to only needed fields instead of full shadow
- // Base URL for the version of API endpoints, e.g. https://gitote.example.com/api/v1
- BaseURL string
- Org *APIOrganization
- }
- // DocURL api doc url
- const DocURL = "https://gitlab.com/gitote/gitote/tree/master/docs/API"
- // Error responses error message to client with given message.
- // If status is 500, also it prints error to log.
- func (c *APIContext) Error(status int, title string, obj interface{}) {
- var message string
- if err, ok := obj.(error); ok {
- message = err.Error()
- } else {
- message = obj.(string)
- }
- if status == http.StatusInternalServerError {
- log.Error(3, "%s: %s", title, message)
- }
- c.JSON(status, map[string]string{
- "message": message,
- "url": DocURL,
- })
- }
- // NotFound renders the 404 response.
- func (c *APIContext) NotFound() {
- c.Status(http.StatusNotFound)
- }
- // ServerError renders the 500 response.
- func (c *APIContext) ServerError(title string, err error) {
- c.Error(http.StatusInternalServerError, title, err)
- }
- // NotFoundOrServerError use error check function to determine if the error
- // is about not found. It responses with 404 status code for not found error,
- // or error context description for logging purpose of 500 server error.
- func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) {
- if errck(err) {
- c.NotFound()
- return
- }
- c.ServerError(title, err)
- }
- // SetLinkHeader sets pagination link header by given total number and page size.
- func (c *APIContext) SetLinkHeader(total, pageSize int) {
- page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
- links := make([]string, 0, 4)
- if page.HasNext() {
- links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
- }
- if !page.IsLast() {
- links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
- }
- if !page.IsFirst() {
- links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
- }
- if page.HasPrevious() {
- links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, c.Req.URL.Path[1:], page.Previous()))
- }
- if len(links) > 0 {
- c.Header().Set("Link", strings.Join(links, ","))
- }
- }
- // APIContexter returns apicontext as macaron middleware
- func APIContexter() macaron.Handler {
- return func(ctx *Context) {
- c := &APIContext{
- Context: ctx,
- BaseURL: setting.AppURL + "api/v1",
- }
- ctx.Map(c)
- }
- }
|