render

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2020 License: Apache-2.0 Imports: 4 Imported by: 3

README

Render

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

Usage

package main

import (
    "net/http"
    "github.com/lancer-kit/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{})

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:"-"`
}

type Page

type Page struct {
	Page     uint64      `json:"page"`
	PageSize uint64      `json:"pageSize"`
	Order    string      `json:"order"`
	Total    int64       `json:"total"`
	Records  interface{} `json:"records"`
}

func (*Page) Render

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

func (*Page) SetTotal

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

type R

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

Example:

``` go

func MyHandler(w http.ResponseWriter, r *http.Request) {
	// some code ...
	// ...
	res := render.R{
		Code: http.StatusOk,
		Message: "User created",
	}
	res.Render(w)
	return
}

``` Usage of predefined response: ``` go

func MyHandler(w http.ResponseWriter, r *http.Request) {
	// some code ...
	// ...
	render.ResultBadRequest.SetError("Invalid email").Render(w)
	return
}

```

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