responder

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2020 License: Apache-2.0 Imports: 5 Imported by: 1

README

elton-responder

Build Status

Responder middleware for elton, it can convert Context.Body to json data. Using this middleware, it's more simple for successful response. More response type can be supported through custom marshal function and content type.

package main

import (
	"github.com/vicanso/elton"

	responder "github.com/vicanso/elton-responder"
)

func main() {
	e := elton.New()

	e.Use(responder.NewDefault())

	// {"name":"tree.xie","id":123}
	e.GET("/", func(c *elton.Context) (err error) {
		c.Body = &struct {
			Name string `json:"name"`
			ID   int    `json:"id"`
		}{
			"tree.xie",
			123,
		}
		return
	})

	er := e.ListenAndServe(":3000")
	if err != nil {
		panic(err)
	}
}

API

  • Config.Skipper skipper function to skip middleware
  • Config.Fastest if set true will use the json-iterator fastest config for better performance, deprecated
  • Config.Marshal custom marshal function
  • Config.ContentType the coontent type for response

Documentation

Index

Constants

View Source
const (
	// ErrCategory responder error category
	ErrCategory = "elton-responder"
)

Variables

This section is empty.

Functions

func New

func New(config Config) elton.Handler

New create a responder

func NewDefault

func NewDefault() elton.Handler

NewDefault create a default responder

Types

type Config

type Config struct {
	Skipper elton.Skipper
	// Fastest set to true will use fast json
	Fastest bool
	// Marshal custom marshal function
	Marshal func(v interface{}) ([]byte, error)
	// ContentType response's content type
	ContentType string
}

Config response config

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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