> 文章列表 > 使用Go语言打造轻量级Web框架

使用Go语言打造轻量级Web框架

使用Go语言打造轻量级Web框架

前言

Web框架是Web开发中不可或缺的组件。它们的主要目标是抽象出HTTP请求和响应的细节,使开发人员可以更专注于业务逻辑的实现。在本篇文章中,我们将使用Go语言实现一个简单的Web框架,类似于Gin框架。

功能

我们的Web框架需要实现以下功能:

  • 路由:处理HTTP请求的路由,并支持路径参数和通配符。
  • 上下文:封装HTTP请求和响应,并提供访问请求参数的方法
  • 中间件:在请求处理之前或之后运行的函数。
  • HTTP请求和响应:支持GET、POST等HTTP方法。

实现

首先,我们需要定义一个HandlerFunc类型,表示处理HTTP请求的函数。这个函数需要接受一个Context类型的参数,用于访问请求和响应。

type HandlerFunc func(Context)

接下来,我们需要定义一个Context类型,封装HTTP请求和响应,并提供访问请求参数的方法。我们可以使用Go语言标准库中的http.ResponseWriterhttp.Request类型分别表示响应和请求。

type Context struct {
Response http.ResponseWriter
Request  *http.Request
Params   map[string]string
}

Params字段用于存储路径参数。例如,如果路由路径为/users/:id,则可以使用c.Params["id"]访问路径参数id的值。

现在,我们可以开始实现路由。我们需要定义一个Route类型,表示一个路由,包含HTTP方法、路径和处理函数。我们还需要一个Router类型,表示整个应用程序的路由器。它应该包含所有的路由,包含需要的中间件,并能够处理HTTP请求。

type Route struct {method  stringpath    stringhandler HandlerFunc
}type Router struct {routes []*Routemiddlewares []MiddlewareFunc}

我们可以使用Handle方法将路由添加到路由器中。

func (r *Router) Handle(method, path string, handler HandlerFunc) {r.routes = append(r.routes, &Route{method, path, handler})
}

当HTTP请求到达时,需要遍历所有的路由,并找到匹配的路由。如果找到了一个匹配的路由,我们就调用它的处理函数,并且如果有中间件,需要遍历所有中间件执行中间件处理逻辑。否则,我们返回HTTP 404错误。

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {var match *Routeparams := make(map[string]string)for _, route := range r.routes {if req.Method == route.method {if ok, p := matchPath(route.path, req.URL.Path); ok {match = routeparams = pbreak}}}if match != nil {handler := match.handlerfor i := len(r.middlewares) - 1; i >= 0; i-- {handler = r.middlewares[i](handler)}handler(Context{w, req, params})} else {http.NotFound(w, req)}
}

在上面的代码中,我们使用了matchPath函数来比较HTTP请求的路径和路由的路径,以确定是否匹配。这个函数还会返回路径参数的值,以便我们可以在Context中访问它们。

现在,我们可以实现中间件。中间件是在请求处理前或处理后运行的函数,它们可以修改请求或响应,或执行其他任务。我们可以定义一个MiddlewareFunc类型,表示中间件函数。

type MiddlewareFunc func(handler HandlerFunc) HandlerFunc

接下来,我们可以在Router中添加一个Use方法,用于注册中间件。这个方法会往路由中添加一个中间件,后面处理函数时候会要遍历使用。

func (r *Router) Use(middleware MiddlewareFunc) {r.middlewares = append(r.middlewares, middleware)
}

最后,我们需要添加HTTP方法的支持。我们可以为每个HTTP方法定义一个快捷方法,它们分别调用Handle方法并传递正确的HTTP方法和路径。

例如,对于GET方法,我们可以定义一个GET方法,如下所示:

func (r *Router) GET(path string, handler HandlerFunc) {r.Handle("GET", path, handler)
}

现在,我们已经完成了一个简单的Web框架的实现。下面是完整的代码:

完整的代码

package mainimport ("fmt""net/http""strings""time"
)type HandlerFunc func(Context)type Context struct {Response http.ResponseWriterRequest  *http.RequestParams   map[string]string
}type Route struct {method  stringpath    stringhandler HandlerFunc
}type Router struct {routes      []*Routemiddlewares []MiddlewareFunc
}type MiddlewareFunc func(handler HandlerFunc) HandlerFuncfunc NewRouter() *Router {return &Router{}
}func (r *Router) Handle(method, path string, handler HandlerFunc) {r.routes = append(r.routes, &Route{method, path, handler})
}func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {var match *Routeparams := make(map[string]string)for _, route := range r.routes {if req.Method == route.method {if ok, p := matchPath(route.path, req.URL.Path); ok {match = routeparams = pbreak}}}if match != nil {handler := match.handlerfor i := len(r.middlewares) - 1; i >= 0; i-- {handler = r.middlewares[i](handler)}handler(Context{w, req, params})} else {http.NotFound(w, req)}
}func (r *Router) Use(middleware MiddlewareFunc) {r.middlewares = append(r.middlewares, middleware)
}func (r *Router) GET(path string, handler HandlerFunc) {r.Handle("GET", path, handler)
}func (r *Router) POST(path string, handler HandlerFunc) {r.Handle("POST", path, handler)
}func (r *Router) PUT(path string, handler HandlerFunc) {r.Handle("PUT", path, handler)
}func (r *Router) DELETE(path string, handler HandlerFunc) {r.Handle("DELETE", path, handler)
}func matchPath(path, pattern string) (bool, map[string]string) {parts1 := strings.Split(path, "/")parts2 := strings.Split(pattern, "/")if len(parts1) != len(parts2) {return false, nil}params := make(map[string]string)for i, part := range parts1 {if part != parts2[i] {if strings.HasPrefix(part, ":") {params[part[1:]] = parts2[i]} else if strings.HasPrefix(part, "*") {params[part[1:]] = strings.Join(parts2[i:], "/")break} else {return false, nil}}}return true, params
}

使用案例

func main() {router := NewRouter()router.Use(func(handler HandlerFunc) HandlerFunc {return func(ctx Context) {start := time.Now()handler(ctx)fmt.Printf("%s cost %s\\n", ctx.Request.RequestURI, time.Now().Sub(start))}})router.GET("/", func(c Context) {fmt.Fprintf(c.Response, "欢迎使用我的web框架!")})router.GET("/users/:id", func(c Context) {fmt.Fprintf(c.Response, "User ID: %s", c.Params["id"])})router.GET("/users/:id/friends", func(c Context) {fmt.Fprintf(c.Response, "User ID: %s, list all friends.", c.Params["id"])})router.GET("/*path", func(c Context) {fmt.Fprintf(c.Response, "User path: %s", c.Params["path"])})http.ListenAndServe(":8080", router)
}

在上面的代码中,我们添加了一个中间件函数记录请求耗时,它用于记录每个HTTP请求的执行。我们还添加了几个路由,以演示路径参数和通配符的用法。

运行结果

$ curl localhost:8080/users
User path: users$ curl localhost:8080/users/1001
User ID: 1001$ curl localhost:8080/users/1001/friends
User ID: 1001, list all friends.$ curl localhost:8080/users/1001/friends/xxx
404 page not found$ curl localhost:8080/xxxx 
User path: xxxx---------------------------------------------------
/users cost 51.917µs
/users/1001 cost 2.75µs
/users/1001/friends cost 5.833µs
/xxxx cost 6.875µs

总结

在本文中,我们使用Go语言实现了一个简单的Web框架。我们实现了路由、上下文、中间件、HTTP请求和响应等功能。还演示了如何使用路径参数和通配符来匹配不同的路径。这个Web框架虽然比不上流行的框架,但它可以作为学习Web框架实现的好起点。


欢迎关注,学习不迷路