response

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: GPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TEMPLATE_MANAGER *templates.Manager

Functions

func Json

func Json(w http.ResponseWriter, jsonResponse *JSONResponse) error

Encode json to a request.

func JsonDecode

func JsonDecode(r *http.Request, data interface{}) error

Decoode json from a request, into any.

func JsonEncode

func JsonEncode(w http.ResponseWriter, data interface{}, status ...ResponseStatus) error

Render json to a request. Response will be in the form of:

{
	"status": "ok",
	"data": {
		"key": "value"
	}
}

func JsonError

func JsonError(w http.ResponseWriter, message string, statusCode int, err error) error

func Render

func Render(w http.ResponseWriter, templateName string, data any) error

Template configuration must be set before calling this function!

See the templates package for more information.

func String

func String(w http.ResponseWriter, templateString string, data any) error

Render a string as a template

Types

type JSONError

type JSONError struct {
	Message    string `json:"message"`
	StatusCode int    `json:"status_code"` // HTTP status code

	// The error that caused this error
	Err error `json:"-"`
}

Render a JSONError to a request.

func NewJsonError

func NewJsonError(message string, statusCode int, err error) *JSONError

Create a new JSONError

func (*JSONError) Error

func (e *JSONError) Error() string

func (*JSONError) Unwrap

func (e *JSONError) Unwrap() error

func (*JSONError) Write

func (e *JSONError) Write(w http.ResponseWriter) error

type JSONResponse

type JSONResponse struct {
	Detail string         `json:"detail,omitempty"`
	Status ResponseStatus `json:"status"`
	Data   interface{}    `json:"data"`
}

type ResponseStatus

type ResponseStatus string
const (
	ResponseStatusOK       ResponseStatus = "ok"
	ResponseStatusError    ResponseStatus = "error"
	ResponseStatusRedirect ResponseStatus = "redirect"
)

Jump to

Keyboard shortcuts

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