项目作者: clavoie

项目描述 :
Http handler utilities for Go
高级语言: Go
项目地址: git://github.com/clavoie/httpu.git
创建时间: 2018-08-22T12:50:33Z
项目社区:https://github.com/clavoie/httpu

开源协议:MIT License

下载


httpu GoDoc Build Status codecov Go Report Card

Http handler utilities for Go.

httpu provides several convenience functions to remove some of the boilerplate from top level http handlers:

  1. func Handler(w http.ResponseWriter, r *http.Request) {
  2. request := new(MyRequest)
  3. if httpu.DecodeJsonOr400(w, r, request, "Could not decode request") {
  4. // http.StatusBadRequest has been written to the response, we can now exit the handler
  5. return
  6. }
  7. result, err := DoWork(request)
  8. if httpu.Write500IfErr(err, w, "Could not process request for: %v", request.Value) {
  9. // http.StatusInternalServerError has been written to the response
  10. return
  11. }
  12. httpu.EncodeJsonOr400(w, result, "Could not encode response json for: %v", result.Value)
  13. }

A full example is available here

Dependency Injection

httpu provides an interface that wraps all top level functions if you would prefer to inject the package into your project. A full example of using httpu with dependency injection is here