像使用 Tornado 一样使用 Go,并支持 middleware

介绍

之前一直使用 Python 的 Tornado 框架进行 Web 开发。最近使用 Go 进行 Web 开发时,遇到如下问题:

  • 需要为判断 http method,使用 httprouter 时,需要为每一个方法添加对应的 HandleFunc,无法像 tornado 那样定义一个 router 时,支持所有的 http method。

解决方案

笔者实现了一个简单的 go-web-framework 框架 可以支持像使用 tornado 那样定义路由。

框架介绍

Application

type Application struct {
    router *httprouter.Router
}
NewApplication()
用于声明应用。并且自动添加 http method 到对应的 router 中
ListenAndServe()
用于启动应用。

Router

type Router struct {
    Path    string
    Handler handlers.HanderInterface
}
Router 结构体包含 path 和 对应的 handler

MiddlewareHandle

中间件支持
可以定义 before request 和 after request 处理方法

关注 github

go_web_framework

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。