对Web服务来说,无非是依据申请*http.Request,结构响应http.ResponseWriter
上下文必要性
在书写web利用的时候,咱们应用原始的库的时候,其中承受申请信息和返回的音讯体是独自的存在,而后这样就会导致咱们书写很多冗余的代码,而后在返回的时候咱们须要返回json这个时候如果应用原生的库这个时候咱们书写的代码是:
obj = map[string]interface{}{ "name": "geektutu", "password": "1234",}w.Header().Set("Content-Type", "application/json")w.WriteHeader(http.StatusOK)encoder := json.NewEncoder(w)if err := encoder.Encode(obj); err != nil { http.Error(w, err.Error(), 500)}
而后如果咱们封装了过后咱们书写的代码是:
c.JSON(http.StatusOK, gee.H{ "username": c.PostForm("username"), "password": c.PostForm("password"),})
这里咱们能够显著的感触到咱们的代码简略了很多,如果没有进行封装,这个时候咱们应用起来是十分好受的。在这里咱们须要晓得的是,其实在web利用中,咱们须要很多的工具,在解决申请的时候,例如,未来解析动静路由/hello/:name,参数:name的值放在哪呢?再比方,框架须要反对中间件,那中间件产生的信息放在哪呢?Context 随着每一个申请的呈现而产生,申请的完结而销毁,和以后申请强相干的信息都应由 Context 承载。
也就是在这次会话的时候context就是一个会话的百宝箱,这个时候咱们能够找到咱们解决这次会话的所有工具。
实现代码
type H map[string]interface{}type Context struct { // origin objects Writer http.ResponseWriter Req *http.Request // request info Path string Method string // response info StatusCode int}func newContext(w http.ResponseWriter, req *http.Request) *Context { return &Context{ Writer: w, Req: req, Path: req.URL.Path, Method: req.Method, }}func (c *Context) PostForm(key string) string { return c.Req.FormValue(key)}func (c *Context) Query(key string) string { return c.Req.URL.Query().Get(key)}func (c *Context) Status(code int) { c.StatusCode = code c.Writer.WriteHeader(code)}func (c *Context) SetHeader(key string, value string) { c.Writer.Header().Set(key, value)}func (c *Context) String(code int, format string, values ...interface{}) { c.SetHeader("Content-Type", "text/plain") c.Status(code) c.Writer.Write([]byte(fmt.Sprintf(format, values...)))}func (c *Context) JSON(code int, obj interface{}) { c.SetHeader("Content-Type", "application/json") c.Status(code) encoder := json.NewEncoder(c.Writer) if err := encoder.Encode(obj); err != nil { http.Error(c.Writer, err.Error(), 500) }}func (c *Context) Data(code int, data []byte) { c.Status(code) c.Writer.Write(data)}func (c *Context) HTML(code int, html string) { c.SetHeader("Content-Type", "text/html") c.Status(code) c.Writer.Write([]byte(html))}
在这外面咱们将很多的工具进行了封装
代码最结尾,给map[string]interface{}起了一个别名gee.H,构建JSON数据时,显得更简洁。
Context目前只蕴含了http.ResponseWriter和*http.Request,另外提供了对 Method 和 Path 这两个罕用属性的间接拜访。
提供了拜访Query和PostForm参数的办法。
提供了疾速结构String/Data/JSON/HTML响应的办法。
对处理函数的改装
昨天咱们进行了路由解析的书写,咱们晓得了继承了handler后咱们就能够进行申请的解决,而后咱们将解决的函数进行一个改装
package geeimport ( "log" "net/http")type router struct { handlers map[string]HandlerFunc}func newRouter() *router { return &router{handlers: make(map[string]HandlerFunc)}}func (r *router) addRoute(method string, pattern string, handler HandlerFunc) { log.Printf("Route %4s - %s", method, pattern) key := method + "-" + pattern r.handlers[key] = handler}func (r *router) handle(c *Context) { key := c.Method + "-" + c.Path if handler, ok := r.handlers[key]; ok { handler(c) } else { c.String(http.StatusNotFound, "404 NOT FOUND: %s\n", c.Path) }}
咱们将减少处理函数的办法放在了这里,而后对昨天的代码进行一个改装
package geeimport ( "net/http")// HandlerFunc defines the request handler used by geetype HandlerFunc func(*Context)// Engine implement the interface of ServeHTTPtype Engine struct { router *router}// New is the constructor of gee.Engine//这里进行了router的创立func New() *Engine { return &Engine{router: newRouter()}}func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) { engine.router.addRoute(method, pattern, handler)}// GET defines the method to add GET requestfunc (engine *Engine) GET(pattern string, handler HandlerFunc) { engine.addRoute("GET", pattern, handler)}// POST defines the method to add POST requestfunc (engine *Engine) POST(pattern string, handler HandlerFunc) { engine.addRoute("POST", pattern, handler)}// Run defines the method to start a http serverfunc (engine *Engine) Run(addr string) (err error) { return http.ListenAndServe(addr, engine)}//这里就是最初的处理函数,这里开始建设的上下文。func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { c := newContext(w, req) engine.router.handle(c)}
在这里咱们联合了明天写的context进行了一个改装。 而后咱们明天的上下文建设就建设好了。