giraffe

package module
v0.0.0-...-f00d7c4 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2016 License: MIT Imports: 11 Imported by: 1

README

Giraffe

Giraffe is a package for Golang that contains an utility functions and struct for work with HTTP.

alt text

Installation

$ go get github.com/svett/giraffe

Usage

You can encode an object as JSON in simplified way:

encoder := giraffe.NewHTTPEncoder(responseWriter)
encoder.EncodeJSON(map[string]string{"username": "root", "password": "swordfish"})

It can be encoded with padding as well:

encoder := giraffe.NewHTTPEncoder(responseWriter)
encoder.EncodeJSONP("login", map[string]string{"username": "root", "password": "swordfish"})

A similar operation can be performed for a byte array:

encoder := giraffe.NewHTTPEncoder(responseWriter)
encoder.EncodeData([]byte("gopher))

A plain text can be encoded as well:

encoder := giraffe.NewHTTPEncoder(responseWriter)
encoder.EncodeText("Hello World")

You can render HTML templates:

renderer := giraffe.NewHTMLTemplateRenderer(responseWriter)
renderer.Render("my_template", "Jack")

MIT License

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"
	// ContentText header value for Text data.
	ContentText = "text/plain"
	// ContentHTML header value for HTML data.
	ContentHTML = "text/html"

	// ContentType header constant.
	ContentType = "Content-Type"
	// ContentDefaultCharset default character encoding.
	ContentDefaultCharset = "UTF-8"
)

Variables

View Source
var (
	// ColorGreen is a green color
	ColorGreen = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
	// ColorWhite is a white color
	ColorWhite = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
	// ColorYellow is a yellow color
	ColorYellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
	// ColorRed is a red color
	ColorRed = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
	// ColorBlue is a blue color
	ColorBlue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
	// ColorMagenta is a magenta color
	ColorMagenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
	// ColorCyan is a cyan color
	ColorCyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
	// DefaultColor is a default color
	DefaultColor = string([]byte{27, 91, 48, 109})
)

Functions

func SetHTMLTemplateProvider

func SetHTMLTemplateProvider(provider HTMLTemplateProvider)

SetHTMLTemplateProvider sets the default repository

Types

type HTMLTemplateProvider

type HTMLTemplateProvider interface {
	Provide() (*template.Template, error)
}

HTMLTemplateProvider provides a templates

type HTMLTemplateRenderer

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

HTMLTemplateRenderer renders a templates of repository

func NewHTMLTemplateRenderer

func NewHTMLTemplateRenderer(writer http.ResponseWriter) *HTMLTemplateRenderer

NewHTMLTemplateRenderer create a new HTMLTemplateRenderer

func NewHTMLTemplateRendererWithProvider

func NewHTMLTemplateRendererWithProvider(writer http.ResponseWriter, provider HTMLTemplateProvider) *HTMLTemplateRenderer

NewHTMLTemplateRendererWithProvider create a new HTMLTemplateRenderer for specific provider

func (*HTMLTemplateRenderer) Render

func (renderer *HTMLTemplateRenderer) Render(template string, model Model) error

Render renders a template

type HTMLTemplateRepository

type HTMLTemplateRepository struct {

	// Directory to load templates. Default is "templates".
	Directory string
	// FileExtensions to parse template files from. Defaults to [".tmpl"].
	FileExtension string
	// Compilation to compile the templates
	Compilation TemplateCompilation
	// UtilFuncs is a FuncMap to apply to the template upon compilation. This is useful for helper functions. Defaults to [].
	UtilFuncs template.FuncMap
	// contains filtered or unexported fields
}

HTMLTemplateRepository represents a template repository

func (*HTMLTemplateRepository) Provide

func (repository *HTMLTemplateRepository) Provide() (*template.Template, error)

Provide returns the repository compiled templates

type HTTPEncoder

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

HTTPEncoder encodes into a different formats

func NewHTTPEncoder

func NewHTTPEncoder(writer http.ResponseWriter) *HTTPEncoder

NewHTTPEncoder creates a new encoder for concrete writer

func (*HTTPEncoder) EncodeData

func (enc *HTTPEncoder) EncodeData(data []byte) error

EncodeData encodes an array of bytes

func (*HTTPEncoder) EncodeJSON

func (enc *HTTPEncoder) EncodeJSON(model Model) error

EncodeJSON encodes a data as json

func (*HTTPEncoder) EncodeJSONP

func (enc *HTTPEncoder) EncodeJSONP(callback string, model Model) error

EncodeJSONP encodes a data as jsonp

func (*HTTPEncoder) EncodeText

func (enc *HTTPEncoder) EncodeText(text string) error

EncodeText encodes a plain text

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, request *http.Request, next http.HandlerFunc)

HandlerFunc is a func that handle middleware operations

func NewHTTPLogger

func NewHTTPLogger(logger Logger, color bool) HandlerFunc

NewHTTPLogger logs a HTTP requests

func NewHTTPStandardLogger

func NewHTTPStandardLogger() HandlerFunc

NewHTTPStandardLogger prints logs into the standard out

type Logger

type Logger interface {
	// Println writes an info message
	Println(...interface{})
}

Logger that logs information

type Model

type Model interface{}

Model represents a encoder data

type TemplateCompilation

type TemplateCompilation uint8

TemplateCompilation defines the compilcation options

const (
	// CompileAlways enables compilation everytime when the templates are used
	CompileAlways TemplateCompilation = iota
	// CompileOnce  enables compilation only once when the templates are used for first time
	CompileOnce
)

Directories

Path Synopsis
This file was generated by counterfeiter This file was generated by counterfeiter
This file was generated by counterfeiter This file was generated by counterfeiter

Jump to

Keyboard shortcuts

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