本篇是Mygin的第三篇
目的
- 将路由独立出来,方便后续扩展修改
- 上下文Context,对http.ResponseWriter和http.Request进行封装,实现对JSON、HTML等的支持
路由
新建一个router文件,将 Mygin实现简单的路由 中将路由部分复制出来
- 新建Mygin/router.go
package mygin
import (
"log"
"net/http"
)
// 路由节点
type methodTree struct {
method string
paths map[string]func(c *Context)
}
type methodTrees map[string]methodTree
type Router struct {
trees methodTrees
}
// 获取路由root根
func (r *Router) getRoot(method string) methodTree {
if root, ok := r.trees[method]; ok {
return root
}
r.trees[method] = methodTree{method: method, paths: make(map[string]func(c *Context))}
return r.trees[method]
}
// 添加路由方法
func (r *Router) addRoute(method, path string, ctx func(c *Context)) {
root := r.getRoot(method)
if _, ok := root.paths[path]; ok {
log.Default().Println(path, "is exist")
return
}
root.method = method
root.paths[path] = ctx
}
// Get Get方法
func (r *Router) Get(path string, handler func(c *Context)) {
r.addRoute(http.MethodGet, path, handler)
}
// Post Post方法
func (e *Engine) Post(path string, handler func(c *Context)) {
e.addRoute(http.MethodPost, path, handler)
}
- 修改Mygin/engine.go文件
package mygin
import (
"net/http"
)
type Engine struct {
Router
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if tree, ok := e.trees[r.Method]; ok {
if handler, ok := tree.paths[r.URL.Path]; ok {
handler(w, r)
return
}
}
w.Write([]byte("404 Not found!\n"))
}
// Default returns an Engine
func Default() *Engine {
return &Engine{
Router{
trees: make(methodTrees, 2),
}}
}
// Run 启动方法start a http server
func (e *Engine) Run(addr string) {
err := http.ListenAndServe(addr, e)
if err != nil {
return
}
}
- 相当于engine继承router,engine可以调用router中的方法。
上下文
对于1个请求来说,无法就是接收http.Request的数据处理,然后将处理后的数据http.ResponseWriter返回。虽然响应的消息体(Body)不一样,但对于同一种类型比如JSON类型,消息头(Header)和消息体(Body),而 Header 包含了状态码(StatusCode),消息类型(ContentType)等几乎每次请求都需要重新写,所以进行封装,对于后续扩展和修改都非常必要。
- 新建mygin/context.go文件
package mygin
import (
"encoding/json"
"net/http"
)
type Context struct {
Request *http.Request
Writer http.ResponseWriter
}
func writeContentType(w http.ResponseWriter, value []string) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = value
}
}
func (c *Context) Status(code int) {
c.Writer.WriteHeader(code)
}
func (c *Context) JSON(v interface{}) error {
writeContentType(c.Writer, []string{"application/json; charset=utf-8"})
encoder := json.NewEncoder(c.Writer)
err := encoder.Encode(v)
if err != nil {
c.Status(http.StatusInternalServerError)
}
c.Status(http.StatusOK)
return err
}
func (c *Context) Html(v string) error {
writeContentType(c.Writer, []string{"text/html; charset=utf-8"})
c.Status(http.StatusOK)
_, err := c.Writer.Write([]byte(v))
return err
}
func (c *Context) String(v string) error {
writeContentType(c.Writer, []string{"text/plain; charset=utf-8"})
c.Status(http.StatusOK)
_, err := c.Writer.Write([]byte(v))
return err
}
- 在context中实现了JSON、Html、String方法
main.go文件修改
package main
import (
"gophp/mygin"
)
func main() {
engine := mygin.Default()
engine.Get("/json", func(c *mygin.Context) {
c.JSON(map[string]interface{}{
"name": "zs",
"password": "1234",
})
})
engine.Get("/html", func(c *mygin.Context) {
c.Html("<h1>hello html!</h1>")
})
engine.Get("/string", func(c *mygin.Context) {
c.String("string...")
})
engine.Run(":9501")
}
接下来看实际效果
curl -i http://localhost:9501/json
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Wed, 17 Jan 2024 05:52:28 GMT
Content-Length: 32
{"name":"zs","password":"1234"}
~ curl -i http://localhost:9501/html
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Wed, 17 Jan 2024 05:52:36 GMT
Content-Length: 20
<h1>hello html!</h1>
~ curl -i http://localhost:9501/string
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 17 Jan 2024 05:52:42 GMT
Content-Length: 9
string...
标签:http,string,实现,Mygin,func,Context,return,上下文,method
From: https://www.cnblogs.com/pengb/p/17969328