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) (interface{}, error)
- type Config
- type ErrHTTP
- type Handle
- type RawHandle
- type Server
- func (s *Server) Delete(path string, handle Handle)
- func (s *Server) Get(path string, handle Handle)
- func (s *Server) GetRaw(path string, handle RawHandle)
- 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)
- func (s *Server) Shutdown(ctx context.Context) error
Examples ¶
Constants ¶
const ( // DefaultAddress is the default address of the server. DefaultAddress = ":5000" // DefaultReadTimeout is the default read timeout. DefaultReadTimeout = 10 * time.Second // DefaultWriteTimeout is the default read timeout. DefaultWriteTimeout = 10 * time.Second // DefaultMaxHeaderBytes is the default max header bytes. DefaultMaxHeaderBytes = 1 << 8 )
Variables ¶
This section is empty.
Functions ¶
func NotFound ¶
func NotFound(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (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 // ReadTimeout is the read timeout. ReadTimeout time.Duration // WriteTimeout is the read timeout. WriteTimeout time.Duration // MaxHeaderBytes is the max header bytes. MaxHeaderBytes int // 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) (interface{}, error)
Handle is the function type for a route handle.
type RawHandle ¶
type RawHandle func(http.ResponseWriter, *http.Request, httprouter.Params)
RawHandle is the function type for a non-JSON 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/sdk/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) (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.