respond

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 10 Imported by: 0

Image README

Respond

respond Text, HTML, XML, JSON, JSONP data to http.ResponseWriter

Godoc

Quick start

package main

import (
    "github.com/gookit/respond"
    "net/http"
)

func main() {
    // config and init the default Responder
    respond.Initialize(func(opts *respond.Options) {
        opts.TplLayout = "two-column.tpl"
        opts.TplViewsDir = "templates"
    })
    
    http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
        respond.As.JSON(w, 200, map[string]string{
            "name": "tom",
        })
    })
    
    http.HandleFunc("/xml", func(w http.ResponseWriter, r *http.Request) {
        respond.As.XML(w, 200, map[string]string{
            "name": "tom",
        })
    })
    
    http.HandleFunc("/html", func(w http.ResponseWriter, r *http.Request) {
        respond.As.HTML(w, 200, "home.tpl", map[string]string{
            "name": "tom",
        })
    })
    
    http.ListenAndServe(":8080", nil)
}

Create new

package main

import (
    "github.com/gookit/respond"
    "net/http"
)

func main() {
    render := respond.New(func(opts *respond.Options) {
        opts.TplLayout = "two-column.tpl"
        opts.TplViewsDir = "templates"
    })
    render.Initialize()
    
    // usage
    http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
        render.JSON(w, 200, map[string]string{
            "name": "tom",
        })
    })
    http.HandleFunc("/html", func(w http.ResponseWriter, r *http.Request) {
        render.HTML(w, 200, "home.tpl", map[string]string{
            "name": "tom",
        })
    })
    
}

Reference

License

MIT

Image Documentation

Overview

Package respond is a http data responder

Source code and other details for the project are available at GitHub:

https://github.com/gookit/respond

usage please see README and examples.

Index

Constants

View Source
const (
	// ContentType header key
	ContentType = "Content-Type"

	// ContentText represents content type text/plain
	ContentText = "text/plain"
	// ContentJSON represents content type application/json
	ContentJSON = "application/json"
	// ContentJSONP represents content type application/javascript
	ContentJSONP = "application/javascript"
	// ContentXML represents content type application/xml
	ContentXML = "application/xml"

	// ContentHTML represents content type text/html
	ContentHTML = "text/html"
	// ContentBinary represents content type application/octet-stream
	ContentBinary = "application/octet-stream"

	// ContentDisposition describes contentDisposition
	ContentDisposition = "Content-Disposition"
)

Variables

View Source
var (
	Marshal       = json.Marshal
	MarshalIndent = json.MarshalIndent
	NewDecoder    = json.NewDecoder
)
View Source
var As = New()

create a default instance

Functions

func Auto

func Auto(w http.ResponseWriter, req *http.Request, v interface{}) error

Auto data response

func Binary

func Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error

Binary data response

func Content

func Content(w http.ResponseWriter, status int, v []byte, contentType string) error

Content data response

func Data

func Data(w http.ResponseWriter, status int, v interface{}, contentType string) error

Data data response

func Empty

func Empty(w http.ResponseWriter) error

Empty data response

func HTML

func HTML(w http.ResponseWriter, status int, template string, v interface{}, layout ...string) error

HTML data response

func HTMLString

func HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error

HTMLString data response

func HTMLText

func HTMLText(w http.ResponseWriter, status int, html string) error

HTMLText data response

func Initialize

func Initialize(config func(opts *Options))

Initialize the default instance

func JSON

func JSON(w http.ResponseWriter, status int, v interface{}) error

JSON data response

func JSONP

func JSONP(w http.ResponseWriter, status int, callback string, v interface{}) error

JSONP data response

func LoadTemplateFiles

func LoadTemplateFiles(files ...string)

LoadTemplateFiles data response

func LoadTemplateGlob

func LoadTemplateGlob(pattern string)

LoadTemplateGlob data response

func NoContent

func NoContent(w http.ResponseWriter) error

NoContent data response

func String

func String(w http.ResponseWriter, status int, v string) error

String data response

func Text

func Text(w http.ResponseWriter, status int, v string) error

Text data response

func XML

func XML(w http.ResponseWriter, status int, v interface{}) error

XML data response

Types

type Options

type Options struct {
	Debug bool

	JSONIndent bool
	JSONPrefix string

	XMLIndent bool
	XMLPrefix string

	// template render
	TplLayout   string
	TplDelims   view.TplDelims
	TplFuncMap  template.FuncMap
	TplViewsDir string
	TplSuffixes []string

	// supported content types
	ContentBinary, ContentHTML, ContentXML, ContentText, ContentJSON, ContentJSONP string

	// Charset default content data charset
	Charset string
	// AppendCharset on response content
	AppendCharset bool
}

Options for the Responder

type Responder

type Responder struct {
	// contains filtered or unexported fields
}

Responder definition

func New

func New(config ...func(*Options)) *Responder

New instance

func NewInitialized

func NewInitialized(config func(*Options)) *Responder

NewInitialized create new instance and initialization it.

func (*Responder) Auto

func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data interface{}) error

Auto response data by request accepted header

func (*Responder) Binary

func (r *Responder) Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error

Binary serve data as Binary response. usage:

var reader io.Reader
reader, _ = os.Open("./README.md")
r.Binary(w, http.StatusOK, reader, "readme.md", true)

func (*Responder) Content

func (r *Responder) Content(w http.ResponseWriter, status int, v []byte, contentType string) error

Content serve success but no content response

func (*Responder) Data

func (r *Responder) Data(w http.ResponseWriter, status int, v interface{}, contentType string) error

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

func (*Responder) Empty

func (r *Responder) Empty(w http.ResponseWriter) error

Empty alias method of the NoContent()

func (*Responder) HTML

func (r *Responder) HTML(w http.ResponseWriter, status int, template string, v interface{}, layout ...string) error

HTML render HTML template file to http.ResponseWriter

func (*Responder) HTMLString

func (r *Responder) HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error

HTMLString render HTML template string to http.ResponseWriter

func (*Responder) HTMLText

func (r *Responder) HTMLText(w http.ResponseWriter, status int, html string) error

HTMLText response string as html/text

func (*Responder) Initialize

func (r *Responder) Initialize() *Responder

Initialize the responder

func (*Responder) JSON

func (r *Responder) JSON(w http.ResponseWriter, status int, v interface{}) error

JSON serve string content as json response

func (*Responder) JSONP

func (r *Responder) JSONP(w http.ResponseWriter, status int, callback string, v interface{}) error

JSONP serve data as JSONP response

func (*Responder) LoadTemplateFiles

func (r *Responder) LoadTemplateFiles(files ...string)

LoadTemplateFiles load template files. usage:

LoadTemplateFiles("path/file1.tpl", "path/file2.tpl")

func (*Responder) LoadTemplateGlob

func (r *Responder) LoadTemplateGlob(pattern string)

LoadTemplateGlob load templates by glob usage:

LoadTemplateGlob("views/*")
LoadTemplateGlob("views/**/*")

func (*Responder) NoContent

func (r *Responder) NoContent(w http.ResponseWriter) error

NoContent serve success but no content response

func (*Responder) Options

func (r *Responder) Options() Options

Options get options

func (*Responder) Renderer

func (r *Responder) Renderer() *view.Renderer

Renderer get view renderer

func (*Responder) String

func (r *Responder) String(w http.ResponseWriter, status int, v string) error

String alias method of the Text()

func (*Responder) Text

func (r *Responder) Text(w http.ResponseWriter, status int, v string) error

Text serve string content as text/plain response

func (*Responder) XML

func (r *Responder) XML(w http.ResponseWriter, status int, v interface{}) error

XML serve data as XML response

Jump to

Keyboard shortcuts

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