resp

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentBinaryHeaderValue header value for binary data.
	ContentBinaryHeaderValue = "application/octet-stream"
	// ContentWebassemblyHeaderValue header value for web assembly files.
	ContentWebassemblyHeaderValue = "application/wasm"
	// ContentHTMLHeaderValue is the  string of text/html response header's content type value.
	ContentHTMLHeaderValue = "text/html"
	// ContentJSONHeaderValue header value for JSON data.
	ContentJSONHeaderValue = "application/json"
	// ContentJSONProblemHeaderValue header value for JSON API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentJSONProblemHeaderValue = "application/problem+json"
	// ContentXMLProblemHeaderValue header value for XML API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentXMLProblemHeaderValue = "application/problem+xml"
	// ContentJavascriptHeaderValue header value for JSONP & Javascript data.
	ContentJavascriptHeaderValue = "application/javascript"
	// ContentTextHeaderValue header value for Text data.
	ContentTextHeaderValue = "text/plain"
	// ContentXMLHeaderValue header value for XML data.
	ContentXMLHeaderValue = "text/xml"
	// ContentXMLUnreadableHeaderValue obselete header value for XML.
	ContentXMLUnreadableHeaderValue = "application/xml"
	// ContentMarkdownHeaderValue custom key/content type, the real is the text/html.
	ContentMarkdownHeaderValue = "text/markdown"
	// ContentYAMLHeaderValue header value for YAML data.
	ContentYAMLHeaderValue = "application/x-yaml"
	// ContentFormHeaderValue header value for post form data.
	ContentFormHeaderValue = "application/x-www-form-urlencoded"
	// ContentFormMultipartHeaderValue header value for post multipart form data.
	ContentFormMultipartHeaderValue = "multipart/form-data"

	//need additional implement
	ContentRedirect = "redirect"
)
View Source
const (
	ContentTypeHeaderKey string = "Content-Type"
)

Variables

This section is empty.

Functions

func ContentType

func ContentType(cType string) string

func Redirect

func Redirect(w http.ResponseWriter, location string)

Types

type ErrorResponse added in v1.3.7

type ErrorResponse struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

type IModelAndView added in v1.2.7

type IModelAndView interface {
	SetModel(model interface{}) IModelAndView
	SetAttributes(key string, value interface{}) IModelAndView
	SetView(file string) IModelAndView
	SetFS(fs fs.FS, file string) IModelAndView
	Compile() (raw string, err error)
	Response() Response
	Write(w http.ResponseWriter)
}

Template

func Model added in v1.2.7

func Model() IModelAndView

type Marshaller added in v1.3.7

type Marshaller func(data interface{}) []byte
var JsonMarshaller Marshaller = func(data interface{}) []byte {
	bytes, _ := json.Marshal(data)
	return bytes
}
var RawMarshaller Marshaller = func(data interface{}) []byte {
	files := lang.NewString(data).GetBytes()
	return files
}
var XmlMarshaller Marshaller = func(data interface{}) []byte {
	files, _ := xml.Marshal(data)
	return files
}
var YamlMarshaller Marshaller = func(data interface{}) []byte {
	files, _ := yaml.Marshal(data)
	return files
}

type Response

type Response interface {
	//build response
	Status(httpCode int) Response
	Data(data interface{}) Response
	Marshaller(marshaller Marshaller) Response
	ContentType(cType string) Response

	GetStatus() int
	GetData() interface{}
	GetContentType() string
	GetMarshaller() Marshaller

	// Write the response
	// when content-type is application/json, data will auto marshal to json
	// other time will convert to string
	Write(w http.ResponseWriter)

	//error
	Error() string
}

Response define standard response function

func Created

func Created(data interface{}) Response

func ErrBadRequest added in v1.3.7

func ErrBadRequest(msg string) Response

func ErrInternalError added in v1.3.7

func ErrInternalError(err error) Response

func Error

func Error(httpStatus int, err error) Response

func ModelAndView added in v1.2.7

func ModelAndView(model interface{}, view string) Response

func New

func New(data interface{}) Response

func Ok

func Ok() Response

Jump to

Keyboard shortcuts

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