responder

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: MIT Imports: 4 Imported by: 9 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Responder

Responder provides a means to respond differently according to a request's accepted mime type.

GoDoc

Usage

Register mime type
import "github.com/conku/responder"

responder.Register("text/html", "html")
responder.Register("application/json", "json")
responder.Register("application/xml", "xml")

Responder has the above 3 mime types registered by default. You can register more types with the Register function, which accepts 2 parameters:

  1. The mime type, like text/html
  2. The format of the mime type, like html
Respond to registered mime types
func handler(writer http.ResponseWriter, request *http.Request) {
  responder.With("html", func() {
    writer.Write([]byte("this is a html request"))
  }).With([]string{"json", "xml"}, func() {
    writer.Write([]byte("this is a json or xml request"))
  }).Respond(request)
})

The first html in the example will be the default response type if Responder cannot find a corresponding mime type.

License

Released under the MIT License.

Documentation

Overview

Package responder respond differently according to request's accepted mime type

Github: http://github.com/conku/responder

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(mimeType string, format string)

Register mime type and format

responder.Register("application/json", "json")

Types

type Responder

type Responder struct {
	DefaultResponder func()
	// contains filtered or unexported fields
}

Responder is holder of registed response handlers, response `Request` based on its accepted mime type

func With

func With(formats interface{}, fc func()) *Responder

With could be used to register response handler for mime type formats, the formats could be string or []string

responder.With("html", func() {
  writer.Write([]byte("this is a html request"))
}).With([]string{"json", "xml"}, func() {
  writer.Write([]byte("this is a json or xml request"))
})

func (*Responder) Respond

func (rep *Responder) Respond(request *http.Request)

Respond differently according to request's accepted mime type

func (*Responder) With

func (rep *Responder) With(formats interface{}, fc func()) *Responder

With could be used to register response handler for mime type formats, the formats could be string or []string

Jump to

Keyboard shortcuts

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