http_utility.go 4.39 KB
package webutility

import (
	"encoding/json"
	"net/http"
)

type webError struct {
	Request string `json:"request"`
	Error   string `json:"error"`
}

// NotFoundHandler writes HTTP error 404 to w.
func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
	SetDefaultHeaders(w)
	if req.Method == "OPTIONS" {
		return
	}
	NotFound(w, req, "Not found")
}

// SetDefaultHeaders set's default headers for an HTTP response.
func SetDefaultHeaders(w http.ResponseWriter) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
	w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type,
		Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
}

func ReqLocale(req *http.Request, dflt string) string {
	loc := req.FormValue("locale")
	if loc == "" {
		return dflt
	}
	return loc
}

// 2xx
func Success(w http.ResponseWriter, payload *Payload, code int) {
	w.WriteHeader(code)
	if payload != nil {
		json.NewEncoder(w).Encode(*payload)
	}
}

// 200
func OK(w http.ResponseWriter, payload *Payload) {
	Success(w, payload, http.StatusOK)
}

// 201
func Created(w http.ResponseWriter, payload *Payload) {
	Success(w, payload, http.StatusCreated)
}

// 4xx; 5xx
func Error(w http.ResponseWriter, r *http.Request, code int, err string) {
	werr := webError{Error: err, Request: r.Method + " " + r.RequestURI}
	w.WriteHeader(code)
	json.NewEncoder(w).Encode(werr)
}

// 400
func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusBadRequest, err)
}

// 404
func NotFound(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusNotFound, err)
}

// 401
func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusUnauthorized, err)
}

// 403
func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusForbidden, err)
}

// 403
func Conflict(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusConflict, err)
}

// 500
func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
	Error(w, r, http.StatusInternalServerError, err)
}

///
/// Old API
///

const (
	templateHttpErr500_EN = "An internal server error has occurred."
	templateHttpErr500_RS = "Došlo je do greške na serveru."
	templateHttpErr400_EN = "Bad request."
	templateHttpErr400_RS = "Neispravan zahtev."
	templateHttpErr404_EN = "Resource not found."
	templateHttpErr404_RS = "Resurs nije pronadjen."
	templateHttpErr401_EN = "Unauthorized request."
	templateHttpErr401_RS = "Neautorizovan zahtev."
)

type httpError struct {
	Error   []HttpErrorDesc `json:"error"`
	Request string          `json:"request"`
}

type HttpErrorDesc struct {
	Lang string `json:"lang"`
	Desc string `json:"description"`
}

// DeliverPayload encodes payload as JSON to w.
func DeliverPayload(w http.ResponseWriter, payload Payload) {
	// Don't write status OK in the headers here. Leave it up for the caller.
	// E.g. Status 201.
	json.NewEncoder(w).Encode(payload)
	payload.Data = nil
}

// ErrorResponse writes HTTP error to w.
func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) {
	err := httpError{desc, r.Method + " " + r.RequestURI}
	w.WriteHeader(code)
	json.NewEncoder(w).Encode(err)
}

// NotFoundResponse writes HTTP error 404 to w.
func NotFoundResponse(w http.ResponseWriter, req *http.Request) {
	ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{
		{"en", templateHttpErr404_EN},
		{"rs", templateHttpErr404_RS},
	})
}

// BadRequestResponse writes HTTP error 400 to w.
func BadRequestResponse(w http.ResponseWriter, req *http.Request) {
	ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{
		{"en", templateHttpErr400_EN},
		{"rs", templateHttpErr400_RS},
	})
}

// InternalSeverErrorResponse writes HTTP error 500 to w.
func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) {
	ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{
		{"en", templateHttpErr500_EN},
		{"rs", templateHttpErr500_RS},
	})
}

// UnauthorizedError writes HTTP error 401 to w.
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) {
	w.Header().Set("WWW-Authenticate", "Bearer")
	ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{
		{"en", templateHttpErr401_EN},
		{"rs", templateHttpErr401_RS},
	})
}