Documentation ¶
Overview ¶
Package jsonhttp defines a simple HTTP server that renders JSON.
Routes can be added by passing a handle that should return JSON serializable data or an error.
Index ¶
- Constants
- func NotFound(w http.ResponseWriter, r *http.Request, _ httprouter.Params, _ *Config) (interface{}, error)
- type Config
- type ErrHTTP
- type Handle
- type Server
- func (s *Server) Delete(path string, handle Handle)
- func (s *Server) Get(path string, handle Handle)
- func (s *Server) ListenAndServe() error
- func (s *Server) Options(path string, handle Handle)
- func (s *Server) Patch(path string, handle Handle)
- func (s *Server) Post(path string, handle Handle)
- func (s *Server) Put(path string, handle Handle)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
Examples ¶
Constants ¶
const (
// DefaultAddress is the default address of the server.
DefaultAddress = ":5000"
)
Variables ¶
This section is empty.
Functions ¶
func NotFound ¶
func NotFound(w http.ResponseWriter, r *http.Request, _ httprouter.Params, _ *Config) (interface{}, error)
NotFound is a handle for a route that is not found.
Types ¶
type Config ¶
type Config struct { // The address of the server. Address string // Optionally, the path to a TLS certificate. CertFile string // Optionally, the path to a TLS private key. KeyFile string }
Config contains configuration options for the server.
type ErrHTTP ¶
type ErrHTTP struct {
// contains filtered or unexported fields
}
ErrHTTP is an error with an HTTP status code.
func NewErrBadRequest ¶
NewErrBadRequest creates an error with a bad request HTTP status code. If the message is empty, the default is "bad request".
func NewErrHTTP ¶
NewErrHTTP creates a, error with a message and HTTP status code.
func NewErrInternalServer ¶
NewErrInternalServer creates an error with an internal server error HTTP status code. If the message is empty, the default is "internal server error".
func NewErrNotFound ¶
NewErrNotFound creates an error with a not found HTTP status code. If the message is empty, the default is "not found".
func NewErrUnauthorized ¶
NewErrUnauthorized creates an error with an unauthorized HTTP status code. If the message is empty, the default is "unauthorized".
func (ErrHTTP) JSONMarshal ¶
JSONMarshal marshals an error to JSON.
type Handle ¶
type Handle func(http.ResponseWriter, *http.Request, httprouter.Params, *Config) (interface{}, error)
Handle is the function type for a route handle.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the type that implements net/http.Handler.
Example ¶
This example shows how to create a server and add a route with a named param. It also tests the route using net/http/httptest.
package main import ( "fmt" "io/ioutil" "log" "net/http" "net/http/httptest" "github.com/julienschmidt/httprouter" "github.com/stratumn/go/jsonhttp" ) func main() { // Create the server. s := jsonhttp.New(&jsonhttp.Config{Address: ":3333"}) // Add a route with a named param. s.Get("/items/:id", func(r http.ResponseWriter, _ *http.Request, p httprouter.Params, _ *jsonhttp.Config) (interface{}, error) { // Return a map containing the ID. result := map[string]string{ "id": p.ByName("id"), } return result, nil }) // Create a test server. ts := httptest.NewServer(s) defer ts.Close() // Test our route. res, err := http.Get(ts.URL + "/items/one") if err != nil { log.Fatal(err) } item, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) } fmt.Printf("%s", item) }
Output: {"id":"one"}
func (*Server) ListenAndServe ¶
ListenAndServe starts the server.