2. Context

Kesa...大约 3 分钟golang

1. 为何需要 Context

简化代码

Web 服务的基本流程是根据请求(request)构建响应(response)。

每次构造 response 时,其 Header 中的状态码(StatusCode)和消息类型(ContentType)均需要手动设置。若不进行封装,每次均需要编写繁杂重复的代码,增加了出错的概率。

此时使用 Context 进行封装能够简化代码提升开发效率,以返回 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"),
})

增强系统可扩展性

Context 随着每次请求创建,请求结束而销毁,在整个请求处理周期中可以存储相关的数据,例如动态路由数据,中间件信息等。

此时所有的路由处理函数和中间件函数的参数均可使用 Context 类型作为参数,简化了接口设计并增强了系统的可扩展性。

2. 实现

https://github.com/dreamjz/golang-notes/tree/main/books/7-days-golang/Gee/day2-contextopen in new window

项目结构如下:

\DAY2-CONTEXT
│  go.mod
│  go.work
│  main.go
│
└─gee
     context.go
     gee.go
     go.mod
     router.go

2.1 Context

package gee

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type H map[string]any

type Context struct {
	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,
	}
}

// PostForm return form value of the key
func (c *Context) PostForm(key string) string {
	return c.Req.FormValue(key)
}

// Query return query value of the key
func (c *Context) Query(key string) string {
	return c.Req.URL.Query().Get(key)
}

// Status set HTTP response status code
func (c *Context) Status(code int) {
	c.StatusCode = code
	c.Writer.WriteHeader(code)
}

// SetHeader set HTTP response header
func (c *Context) SetHeader(key string, value string) {
	c.Writer.Header().Set(key, value)
}

// String write string data into HTTP response
func (c *Context) String(code int, format string, values ...any) {
	c.SetHeader("Content-Type", "text/plain")
	c.Status(code)
	c.Writer.Write([]byte(fmt.Sprintf(format, values)))
}

// JSON write JSON data into HTTP response
func (c *Context) JSON(code int, obj any) {
	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)
	}
}

// Data write bytes data into HTTP response
func (c *Context) Data(code int, data []byte) {
	c.Status(code)
	c.Writer.Write(data)
}

// HTML write HTML data into HTTP response
func (c *Context) HTML(code int, html string) {
	c.SetHeader("Content-Type", "text/html")
	c.Status(code)
	c.Writer.Write([]byte(html))
}
  • gee.Hmap[string]any的别名,使得构建 JSON 数据时更加简洁
  • Context:包含http.ResponseWriter*http.Request,提供了常用属性MethodPath的直接访问
  • 提供了访问Query和PostForm参数的方法
  • 提供了快速构造String/Data/JSON/HTML响应的方法

2.2 Router

将路由相关的逻辑提取出来。

package gee

import (
	"log"
	"net/http"
)

type router struct {
	handlers map[string]HandlerFunc
}

func newRouter() *router {
	return &router{handlers: 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)
	}
}

2.3 Gee

package gee

import "net/http"

// HandlerFunc defines the request handler function
type HandlerFunc func(*Context)

// Engine is the instance of framework
type Engine struct {
	router *router
}

// Ensure that *Engine implements the interface
var _ http.Handler = (*Engine)(nil)

func New() *Engine {
	return &Engine{router: newRouter()}
}

func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
	engine.router.addRoute(method, pattern, handler)
}

// GET  add GET request route
func (engine *Engine) GET(pattern string, handler HandlerFunc) {
	engine.addRoute("GET", pattern, handler)
}

// POST  add POST request route
func (engine *Engine) POST(pattern string, handler HandlerFunc) {
	engine.addRoute("POST", pattern, handler)
}

// Run start http server
func (engine *Engine) Run(addr string) error {
	return http.ListenAndServe(addr, engine)
}

// ServeHTTP conforms to http.Handler interface
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	c := newContext(w, req)
	engine.router.handle(c)
}
  • 将处理函数改为func(*Context)

3. Main

package main

import (
	"fmt"
	"gee"
)

func main() {
	r := gee.New()

	r.GET("/", func(c *gee.Context) {
		fmt.Fprintf(c.Writer, "URL.Path = %q\n", c.Path)
	})

	r.GET("/hello", func(c *gee.Context) {
		for k, v := range c.Req.Header {
			fmt.Fprintf(c.Writer, "Header[%q] = %q\n", k, v)
		}
	})

	r.Run(":8000")
}

启动后简单测试:

$ curl http://localhost:8000
URL.Path = "/"

$ curl http://localhost:8000/hello
Header["Accept-Encoding"] = ["gzip"]
Header["User-Agent"] = ["curl/8.0.1"]
Header["Accept"] = ["*/*"]

$ curl http://localhost:8000/hello/2
404 NOT FOUND: [/hello/2]

4. 小结

  1. 路由逻辑独立出来,方便后序的扩展
  2. 实现 Context 结构,封装了 Requst 和 Response,提供了 JSON,HTML 等返回类型的支持

Reference

  1. https://geektutu.com/post/gee-day2.htmlopen in new window
上次编辑于:
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.2