render

package
v0.0.0-...-a807e99 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

GoDoc

Render

render is a simple response render helper and prepared collection of the responses

Usage

package main

import (
    "net/http"
    "git.ooo.ua/pub/armory/api/render"
)

func Get(w http.ResponseWriter, r *http.Request) {
    testMsg := r.Host
    render.WriteJSON(w, http.StatusOK, testMsg)
}

func Crap(w http.ResponseWriter, r *http.Request) {
    render.ResultBadRequest.Render(w)
}

func Ok(w http.ResponseWriter, r *http.Request)  {
    render.ResultSuccess.Render(w)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ResultServerError predefined response for `http.StatusInternalServerError`.
	ResultServerError = &R{
		Code:    http.StatusInternalServerError,
		Message: "Request Failed",
	}

	// ResultBadRequest predefined response for `http.StatusBadRequest`.
	ResultBadRequest = &R{
		Code:    http.StatusBadRequest,
		Message: "Bad Request",
	}

	// ResultSuccess predefined response for `http.StatusOK`.
	ResultSuccess = &R{
		Code:    http.StatusOK,
		Message: "Ok",
	}

	// ResultNotFound predefined response for `http.StatusNotFound`.
	ResultNotFound = &R{
		Code:    http.StatusNotFound,
		Message: "Not Found",
	}

	// ResultUnauthorized predefined response for `http.StatusUnauthorized`.
	ResultUnauthorized = &R{
		Code:    http.StatusUnauthorized,
		Message: "Action Unauthorized",
	}

	// ResultForbidden predefined response for `http.StatusForbidden`.
	ResultForbidden = &R{
		Code:    http.StatusForbidden,
		Message: "Forbidden",
	}

	// ResultConflict predefined response for `http.StatusConflict`.
	ResultConflict = &R{
		Code:    http.StatusConflict,
		Message: "Already created",
	}
)

nolint:gochecknoglobals

View Source
var PrettyMarshal bool // nolint:gochecknoglobals

PrettyMarshal is a flag that enable marshalling with indent.

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, reason interface{})

BadRequest renders `ResultBadRequest` with `reason` as an error.

func Forbidden

func Forbidden(w http.ResponseWriter, reason interface{})

Forbidden renders `ResultForbidden` with `reason` as an error.

func InProgress

func InProgress(w http.ResponseWriter, result interface{})

InProgress renders `ResultAccepted` with `reason` as an message.

func RenderListWithPages

func RenderListWithPages(w http.ResponseWriter, pageQuery db.PageQuery, total int64, list interface{})

RenderListWithPages

func ServerError

func ServerError(w http.ResponseWriter)

ServerError renders default http.StatusInternalServerError.

func Success

func Success(w http.ResponseWriter, result interface{})

Success renders `result` as JSON with `http.StatusOK`.

func Unauthorized

func Unauthorized(w http.ResponseWriter, reason interface{})

Unauthorized renders `ResultUnauthorized` with `reason` as an error.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data interface{})

WriteJSON writes some response as WriteJSON to the `http.ResponseWriter`.

Types

type BaseRow

type BaseRow struct {
	RowCount int64 `db:"row_count" json:"-"`
}

DEPRECATED BaseRow is a helper struct.

type Page

type Page struct {
	// Page number of current page.
	Page uint64 `json:"page"`
	// PageSize is a number of records per page.
	PageSize uint64 `json:"pageSize"`
	// Order is ordering direction: asc or desc.
	Order string `json:"order"`
	// Total is total count of pages.
	Total int64 `json:"total"`
	// Total is total count of rows.
	TotalRows int64 `json:"total_rows"`
	// Records is an array of rows.
	Records interface{} `json:"records"`
}

Page is a standard response structure to render paginated list.

func (*Page) Render

func (page *Page) Render(w http.ResponseWriter)

Render writes page with http.StatusOK.

func (*Page) SetTotal

func (page *Page) SetTotal(rowCount, pageSize uint64)

SetTotal fills total count properly.

type R

type R struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
	Error   interface{} `json:"errmsg,omitempty"`
}

R is a structure for the http responses.

func (*R) Render

func (r *R) Render(w http.ResponseWriter)

Render writes current response as WriteJSON to the `http.ResponseWriter`.

func (*R) SetData

func (r *R) SetData(val interface{}) *R

SetData sets response data.

func (*R) SetError

func (r *R) SetError(val interface{}) *R

SetError adds error details to response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL