Esempio n. 1
0
func sendStatusResponse(statusCode int, rw http.ResponseWriter, req *http.Request, endpoint, endpointAction string) string {
	message := api.StatusText(statusCode)

	sendMessageResponse(statusCode, message, rw, req, endpoint, endpointAction)

	return message
}
Esempio n. 2
0
func authorize(authChan chan *Authorization, routeAction *config.Action) (*identity.Identity, error) {
	defer close(authChan)

	authorization := <-authChan
	if authorization.Error != nil {
		return nil, authorization.Error
	}

	user := authorization.Identity
	if (!routeAction.AllowAnonymous && user.IsAnonymous()) || (routeAction.RequireAdmin && !user.IsAdmin()) {
		return nil, errors.New(api.StatusText(http.StatusUnauthorized))
	}

	return user, nil
}
Esempio n. 3
0
func respond(resp *api.Response, rw http.ResponseWriter, req *http.Request, endpoint, endpointAction string) {
	if resp.StatusCode == 0 {
		resp.StatusCode = http.StatusInternalServerError
		sendMessageResponse(resp.StatusCode, api.StatusText(resp.StatusCode), rw, req, endpoint, endpointAction)
	} else if len(resp.ErrorMessage) > 0 {
		sendMessageResponse(resp.StatusCode, resp.ErrorMessage, rw, req, endpoint, endpointAction)
	} else {
		if len(resp.ContentType) == 0 {
			resp.ContentType = api.ContentJSON
		}

		sendResponse(resp.StatusCode, resp.Content, rw, req, endpoint, endpointAction, resp.ContentType, resp.File)

		// Try caching the data only if a GET request was made
		go func(resp *api.Response, req *http.Request, endpoint string) {
			if req.Method == api.GET && cache.Status == cache.StatusON {
				cacheResponse(resp, endpoint, endpointAction)
			}
		}(resp, req, endpoint)
	}
}