negotiate

package module
v0.0.0-...-6156a88 Latest Latest
Warning

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

Go to latest
Published: May 29, 2016 License: MIT Imports: 4 Imported by: 0

README

negotiate

Content negotiation middleware for Martini.

It's a simple wrapper to the negotiation library and martini-contrib/encoder service.

Status

This project is DEPRECATED and should NOT be used.

If someone magically appears and wants to maintain this project, I'll gladly give access to this repository.

Usage

Here is a ready to use example:

package main

import (
  "github.com/K-Phoen/negotiate"
  "github.com/go-martini/martini"
  "github.com/martini-contrib/encoder"
  "log"
  "net/http"
)

type Some struct {
  Login    string `json:"login"`
  Password string `json:"password" out:"false"`
}

func main() {
  m := martini.New()
  route := martini.NewRouter()

  // create a format -> encoder map
  negotiators := make(map[string]encoder.Encoder)
  negotiators["application/xml"] = encoder.XmlEncoder{}
  negotiators["application/json"] = encoder.JsonEncoder{}

  // use the middleware
  m.Use(negotiate.NegotiateFormat(negotiators))

  // and the right encoder will be automatically injected
  route.Get("/test", func(enc encoder.Encoder) (int, []byte) {
    result := &Some{"awesome", "hidden"}
    return http.StatusOK, encoder.Must(enc.Encode(result))
  })

  m.Action(route.Handle)

  log.Println("Waiting for connections...")

  if err := http.ListenAndServe(":8000", m); err != nil {
    log.Fatal(err)
  }
}

ToDo

  • provide tools to negotiate other things (language for instance)
  • write tests

License

This library is released under the MIT License. See the bundled LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NegotiateFormat

func NegotiateFormat(negotiators map[string](encoder.Encoder)) martini.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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