rest

package module
v0.0.0-...-241eec8 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2016 License: BSD-3-Clause, MIT Imports: 9 Imported by: 0

README

Repository information

This repository contains the restful types context's rendering for Iris web framework and fasthttp. The idea of this came from unrolled/render, which I had the time to convert it for fasthttp & iris & improve even more its performance and usability.

Quick look

// small example for json rendering, same for all other

package main

import (
	"github.com/kataras/iris"
)

func main() {
	iris.Get("/hi_json", func(ctx *iris.Context) {
		ctx.JSON(iris.StatusOK, iris.Map{
			"Name": "Iris",
			"Age":  2,
		}) // ctx.XML, ctx.Data, ctx.Text, ctx.JSONP...
	})

	iris.Listen(":8080")
}

How to use

License

This project is licensed under the MIT License.

License can be found here.

Documentation

Index

Constants

View Source
const (
	// ContentBinary header value for binary data.
	ContentBinary = "application/octet-stream"
	// ContentJSON header value for JSON data.
	ContentJSON = "application/json"
	// ContentJSONP header value for JSONP data.
	ContentJSONP = "application/javascript"
	// ContentLength header constant.
	ContentLength = "Content-Length"
	// ContentText header value for Text data.
	ContentText = "text/plain"
	// ContentType header constant.
	ContentType = "Content-Type"
	// ContentXML header value for XML data.
	ContentXML = "text/xml"
)

Variables

View Source
var (
	// DefaultCharset character encoding for rest rendering
	DefaultCharset = "UTF-8"
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Appends the given character set to the Content-Type header. Default is "UTF-8".
	Charset string
	// Gzip enable it if you want to render with gzip compression. Default is false
	Gzip bool
	// Outputs human readable JSON.
	IndentJSON bool
	// Outputs human readable XML. Default is false.
	IndentXML bool
	// Prefixes the JSON output with the given bytes. Default is false.
	PrefixJSON []byte
	// Prefixes the XML output with the given bytes.
	PrefixXML []byte
	// Unescape HTML characters "&<>" to their original values. Default is false.
	UnEscapeHTML bool
	// Streams JSON responses instead of marshalling prior to sending. Default is false.
	StreamingJSON bool
	// Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false.
	DisableHTTPErrorRendering bool
	// MarkdownSanitize sanitizes the markdown. Default is false.
	MarkdownSanitize bool
}

Config is a struct for specifying configuration options for the rest.Render object.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default config for rest

func (Config) Merge

func (c Config) Merge(cfg []Config) (config Config)

Merge merges the default with the given config and returns the result

func (Config) MergeSingle

func (c Config) MergeSingle(cfg Config) (config Config)

MergeSingle merges the default with the given config and returns the result

type Data

type Data struct {
	Head
}

Data built-in renderer.

func (Data) Render

func (d Data) Render(ctx *fasthttp.RequestCtx, v interface{}) error

Render a data response.

func (Data) RenderGzip

func (d Data) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error

RenderGzip a data response using gzip compression.

type Engine

type Engine interface {
	Render(*fasthttp.RequestCtx, interface{}) error
	//used only if config gzip is enabled
	RenderGzip(*fasthttp.RequestCtx, interface{}) error
}

Engine is the generic interface for all responses.

type Head struct {
	ContentType string
	Status      int
}

Head defines the basic ContentType and Status fields.

func (Head) Write

func (h Head) Write(ctx *fasthttp.RequestCtx)

Write outputs the header content.

type JSON

type JSON struct {
	Head
	Indent        bool
	UnEscapeHTML  bool
	Prefix        []byte
	StreamingJSON bool
}

JSON built-in renderer.

func (JSON) Render

func (j JSON) Render(ctx *fasthttp.RequestCtx, v interface{}) error

Render a JSON response.

func (JSON) RenderGzip

func (j JSON) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error

RenderGzip a JSON response using gzip compression.

type JSONP

type JSONP struct {
	Head
	Indent   bool
	Callback string
}

JSONP built-in renderer.

func (JSONP) Render

func (j JSONP) Render(ctx *fasthttp.RequestCtx, v interface{}) error

Render a JSONP response.

func (JSONP) RenderGzip

func (j JSONP) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error

RenderGzip a JSONP response using gzip compression.

type Render

type Render struct {
	// Customize Secure with an Options struct.
	Config          Config
	CompiledCharset string
}

Render is a service that provides functions for easily writing JSON, XML, binary data, and HTML templates out to a HTTP Response.

func New

func New(cfg ...Config) *Render

New constructs a new Render instance with the supplied configs.

func (*Render) Data

func (r *Render) Data(ctx *fasthttp.RequestCtx, status int, v []byte) error

Data writes out the raw bytes as binary data.

func (*Render) JSON

func (r *Render) JSON(ctx *fasthttp.RequestCtx, status int, v interface{}) error

JSON marshals the given interface object and writes the JSON response.

func (*Render) JSONP

func (r *Render) JSONP(ctx *fasthttp.RequestCtx, status int, callback string, v interface{}) error

JSONP marshals the given interface object and writes the JSON response.

func (*Render) Markdown

func (r *Render) Markdown(markdownBytes []byte) string

Markdown parses and returns the converted html from a markdown []byte accepts two parameters first is the http status code second is the markdown string

Note that: Works different than the other rest's functions.

func (*Render) Render

func (r *Render) Render(ctx *fasthttp.RequestCtx, e Engine, data interface{}) error

Render is the generic function called by XML, JSON, Data, HTML, and can be called by custom implementations.

func (*Render) Text

func (r *Render) Text(ctx *fasthttp.RequestCtx, status int, v string) error

Text writes out a string as plain text.

func (*Render) XML

func (r *Render) XML(ctx *fasthttp.RequestCtx, status int, v interface{}) error

XML marshals the given interface object and writes the XML response.

type Text

type Text struct {
	Head
}

Text built-in renderer.

func (Text) Render

func (t Text) Render(ctx *fasthttp.RequestCtx, v interface{}) error

Render a text response.

func (Text) RenderGzip

func (t Text) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error

RenderGzip a Text response using gzip compression.

type XML

type XML struct {
	Head
	Indent bool
	Prefix []byte
}

XML built-in renderer.

func (XML) Render

func (x XML) Render(ctx *fasthttp.RequestCtx, v interface{}) error

Render an XML response.

func (XML) RenderGzip

func (x XML) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error

RenderGzip an XML response using gzip compression.

Jump to

Keyboard shortcuts

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