Categorygithub.com/frozzare/go-httpapi
modulepackage
1.0.0
Repository: https://github.com/frozzare/go-httpapi.git
Documentation: pkg.go.dev

# README

httpapi Build Status GoDoc Go Report Card

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

# Packages

No description provided by the author

# Functions

NewRouter creates a new router.
ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.

# Structs

Router represents the router.

# Type aliases

Handle is httprouter's function that can be in middlewares.
HandleFunc is a function that can be registered to be a route to handle HTTP requests.
HandleFunc2 is a function that can be registered to be a route to handle HTTP requests.
HandleFunc3 is a function that can be registered to be a route to handle HTTP requests.
HandleFunc4 is a function that can be registered to be a route to handle HTTP requests.
Param is a single URL parameter, consisting of a key and a value.
Params is a Param-slice, as returned by the router.