httpapi

package module
v1.0.1-0...-e06492e Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 6 Imported by: 1

README

httpapi Build Status GoDoc Go Report Card

Use Fiber instead. This project will not be maintained any more.

A http router for building http apis in Go based on httprouter and alice.

The router works almost the same way as httprouter does with some changes:

  • Uppercase method functions is capitalized instead.
  • HandleFunc has two arguments instead of three (request and params).
  • All HandleFunc returns the data and/or a error that the response handle will handle.
  • HandleFunc2 has one argument instead of three (request).
  • HandleFunc3 has one argument instead of three (params).
  • HandleFunc4 has zero arguments instead of three.
  • Default response handler that response with JSON. Can be replaced by a custom handler function.
  • Not all methods exists on httpapi.Router struct as httprouter.Router has, e.g HandlerFunc does not exist.
  • Better support for middlewares with alice.
  • Default httprouter handle can also be used.

Installation

go get -u github.com/frozzare/go-httpapi

Usage

Example code:

router := httpapi.NewRouter()

router.Get("/hello/:name", func(r *http.Request, ps httpapi.Params) (interface{}, interface{}) {
    return map[string]string{
        "hello": ps.ByName("name"),
    }, nil
})

http.Handle("/", router)
http.ListenAndServe(":3000", nil)

Example response:

GET /hello/fredrik
{
    "hello": "fredrik"
}

To configure httprouter you just pass it as argument to NewRouter:

router := httpapi.NewRouter(&httprouter.Router{
    RedirectTrailingSlash: true,
})

To modify the response handle that takes in HandleFunc, HandleFunc2 and HandleFunc3 is wrapped with HandleFunc:

router := httpapi.NewRouter()

router.ResponseHandle = func(fn httpapi.HandleFunc) httpapi.Handle {
    return func(w http.ResponseWriter, r *http.Request, ps httpapi.Params) {
        data, out := fn(r, ps)

        // and so on...
    }
}

Both return values are returned as interfaces to support more than just than the error type.

Middlewares

router := httpapi.NewRouter()

// with standard http handler.
router.Use(func(h http.Handler) http.Handler {
    fmt.Println("Hello, world")
    return h
})

// with httprouter's handle.
router.Use(func(h httpapi.Handle) httpapi.Handle {
    fmt.Println("Hello, world")
    return h
})

License

MIT © Fredrik Forsmo

Documentation

Overview

Package httpapi combine the popular httprouter package and alice to bring the best of both worlds when creating http apis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteJSON

func WriteJSON(w http.ResponseWriter, v interface{}) error

WriteJSON writes interface as JSON to response writer. If a error occurred a internal server error status will be written.

Types

type Handle

type Handle = httprouter.Handle

Handle is httprouter's function that can be in middlewares. Handle is a function that can be registered to a route to handle HTTP requests. Just a alias for httprouter.Handle.

func DefaultResponseHandle

func DefaultResponseHandle(fn HandleFunc) Handle

DefaultResponseHandle is the default response handle.

type HandleFunc

type HandleFunc func(r *http.Request, ps httprouter.Params) (interface{}, interface{})

HandleFunc is a function that can be registered to be a route to handle HTTP requests.

type HandleFunc2

type HandleFunc2 func(r *http.Request) (interface{}, interface{})

HandleFunc2 is a function that can be registered to be a route to handle HTTP requests. HandleFunc2 does only have http request as a argument.

type HandleFunc3

type HandleFunc3 func() (interface{}, interface{})

HandleFunc3 is a function that can be registered to be a route to handle HTTP requests. HandleFunc3 does only have params as a argument.

type HandleFunc4

type HandleFunc4 func() (interface{}, interface{})

HandleFunc4 is a function that can be registered to be a route to handle HTTP requests. HandleFunc4 does not have any arguments.

type Param

type Param = httprouter.Param

Param is a single URL parameter, consisting of a key and a value. Just a alias for httprouter.Param.

type Params

type Params = httprouter.Params

Params is a Param-slice, as returned by the router. Just a alias for httprouter.Params.

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present. Just a alias function for httprouter.ParamsFromContext.

type Router

type Router struct {
	ResponseHandle func(HandleFunc) httprouter.Handle
	// contains filtered or unexported fields
}

Router represents the router.

func NewRouter

func NewRouter(args ...*httprouter.Router) *Router

NewRouter creates a new router.

func (*Router) Delete

func (r *Router) Delete(path string, handle interface{})

Delete is a shortcut for router.Handle("DELETE", path, handle).

func (*Router) Get

func (r *Router) Get(path string, handle interface{})

Get is a shortcut for router.Handle("GET", path, handle).

func (*Router) Group

func (r *Router) Group(path string) *Router

Group returns new *Router with given path and middlewares. It should be used for handles which have same path prefix or common middlewares.

func (*Router) Handle

func (r *Router) Handle(method, path string, handle interface{})

Handle adds a new handle to a path and method.

func (*Router) Handler

func (r *Router) Handler(method, path string, handler http.Handler)

Handler is an adapter which allows the usage of an http.Handler as a request handle. Just a alias function for httprouter's Handler.

func (*Router) Head

func (r *Router) Head(path string, handle interface{})

Head is a shortcut for router.Handle("HEAD", path, handle).

func (*Router) Options

func (r *Router) Options(path string, handle interface{})

Options is a shortcut for router.Handle("OPTIONS", path, handle).

func (*Router) Patch

func (r *Router) Patch(path string, handle interface{})

Patch is a shortcut for router.Handle("PATCH", path, handle).

func (*Router) Post

func (r *Router) Post(path string, handle interface{})

Post is a shortcut for router.Handle("POST", path, handle).

func (*Router) Put

func (r *Router) Put(path string, handle interface{})

Put is a shortcut for router.Handle("PUT", path, handle).

func (*Router) ServeFiles

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system root. Just a alias function for httprouter's ServeFiles. Read more: https://godoc.org/github.com/julienschmidt/httprouter#Router.ServeFiles

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the router implement the http.Handler interface. Just a alias function for httprouter's ServeHTTP.

func (*Router) Use

func (r *Router) Use(mwf ...interface{})

Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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