mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
expose status code (or anything) by wrapping http.ResponseWriter
This commit is contained in:
parent
5eb0e10a78
commit
a6c6ebce8f
16
gin.go
16
gin.go
@ -35,7 +35,7 @@ type (
|
||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||
Context struct {
|
||||
Req *http.Request
|
||||
Writer http.ResponseWriter
|
||||
Writer ResponseWriter
|
||||
Keys map[string]interface{}
|
||||
Errors ErrorMsgs
|
||||
Params httprouter.Params
|
||||
@ -101,7 +101,7 @@ func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
|
||||
handlers := engine.combineHandlers(engine.handlers404)
|
||||
c := engine.createContext(w, req, nil, handlers)
|
||||
if engine.handlers404 == nil {
|
||||
http.NotFound(c.Writer, c.Req)
|
||||
http.NotFound(&c.Writer, c.Req)
|
||||
} else {
|
||||
c.Writer.WriteHeader(404)
|
||||
}
|
||||
@ -125,7 +125,7 @@ func (engine *Engine) ServeFiles(path string, root http.FileSystem) {
|
||||
|
||||
// ServeHTTP makes the router implement the http.Handler interface.
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
engine.router.ServeHTTP(w, req)
|
||||
engine.router.ServeHTTP(&ResponseWriter{w, -1}, req)
|
||||
}
|
||||
|
||||
func (engine *Engine) Run(addr string) {
|
||||
@ -138,7 +138,7 @@ func (engine *Engine) Run(addr string) {
|
||||
|
||||
func (group *RouterGroup) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
||||
return &Context{
|
||||
Writer: w,
|
||||
Writer: ResponseWriter{w, -1},
|
||||
Req: req,
|
||||
index: -1,
|
||||
engine: group.engine,
|
||||
@ -178,7 +178,7 @@ func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
|
||||
p = path.Join(group.prefix, p)
|
||||
handlers = group.combineHandlers(handlers)
|
||||
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
||||
group.createContext(w, req, params, handlers).Next()
|
||||
group.createContext(&ResponseWriter{w, -1}, req, params, handlers).Next()
|
||||
})
|
||||
}
|
||||
|
||||
@ -330,7 +330,7 @@ func (c *Context) JSON(code int, obj interface{}) {
|
||||
encoder := json.NewEncoder(c.Writer)
|
||||
if err := encoder.Encode(obj); err != nil {
|
||||
c.Error(err, obj)
|
||||
http.Error(c.Writer, err.Error(), 500)
|
||||
http.Error(&c.Writer, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
|
||||
@ -344,7 +344,7 @@ func (c *Context) XML(code int, obj interface{}) {
|
||||
encoder := xml.NewEncoder(c.Writer)
|
||||
if err := encoder.Encode(obj); err != nil {
|
||||
c.Error(err, obj)
|
||||
http.Error(c.Writer, err.Error(), 500)
|
||||
http.Error(&c.Writer, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
|
||||
@ -361,7 +361,7 @@ func (c *Context) HTML(code int, name string, data interface{}) {
|
||||
"name": name,
|
||||
"data": data,
|
||||
})
|
||||
http.Error(c.Writer, err.Error(), 500)
|
||||
http.Error(&c.Writer, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
|
||||
|
19
response_writer.go
Normal file
19
response_writer.go
Normal file
@ -0,0 +1,19 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) WriteHeader(code int) {
|
||||
w.status = code
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) Status() int {
|
||||
return w.status
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user