Created
January 8, 2018 09:33
-
-
Save e7/5efe4d5f57d65457293762bccd57cb34 to your computer and use it in GitHub Desktop.
negroni的middleware示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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