mirror of
https://github.com/nkanaev/yarr.git
synced 2025-12-15 16:40:04 +00:00
router package
This commit is contained in:
33
src/router/context.go
Normal file
33
src/router/context.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Req *http.Request
|
||||
Out http.ResponseWriter
|
||||
|
||||
Vars map[string]string
|
||||
|
||||
chain []Handler
|
||||
index int
|
||||
}
|
||||
|
||||
func (c *Context) Next() {
|
||||
c.index++
|
||||
c.handlers[c.index](c)
|
||||
}
|
||||
|
||||
func (c *Context) JSON(status int, data interface{}) {
|
||||
reply, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
c.Out.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
c.Out.WriteHeader(status)
|
||||
c.Out.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
c.Out.Write(reply)
|
||||
c.Out.Write([]byte("\n"))
|
||||
}
|
||||
Reference in New Issue
Block a user