Skip to content

Instantly share code, notes, and snippets.

@e7
Created January 8, 2018 09:33
Show Gist options
  • Save e7/5efe4d5f57d65457293762bccd57cb34 to your computer and use it in GitHub Desktop.
Save e7/5efe4d5f57d65457293762bccd57cb34 to your computer and use it in GitHub Desktop.
negroni的middleware示例
type bodyParser struct{
unused int;
}
func NewBodyParser() (*bodyParser){
return &bodyParser{}
}
func (m *bodyParser) ServeHTTP(rsp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
var err error
rsp.Header().Set("Content-Type", "application/json;charset=utf-8")
data, _ := ioutil.ReadAll(req.Body)
dict := make(map[string]string)
if err = json.Unmarshal(data, &dict); err != nil {
logrus.Errorf("qiniu replied invalid json:%s", err.Error())
rsp.WriteHeader(http.StatusBadRequest)
return
}
next(rsp, req.WithContext(
context.WithValue(req.Context(), "req-map", dict)),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment