1. https://github.com/hoisie/web
比较轻量,仅包含简单的核心路由分发,极简脚手架。
package main
import (
"github.com/hoisie/web"
)
func hello(val string) string { return "hello " + val }
func main() {
web.Get("/(.*)", hello)
web.Run("0.0.0.0:9999")
}
2. https://github.com/gocraft/web
提供路由和中间件功能。
路由注册时写入一个通用的上下文,可以在中间件和后续的业务处理中操作。
func (c *YourContext) UserRequired(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
user := userFromSession(r) // Pretend like this is defined. It reads a session cookie and returns a *User or nil.
if user != nil {
c.User = user
next(rw, r)
} else {
rw.Header().Set("Location", "/")
rw.WriteHeader(http.StatusMovedPermanently)
// do NOT call next()
}
}
gocraft 提供了一个用于构建 Web 应用程序的工具包。目前这些包可用:
https://github.com/gocraft/web - Go Router + Middleware. Your Contexts.
https://github.com/gocraft/dbr - Additions to Go's database/sql for super fast performance and convenience.
https://github.com/gocraft/health - Instrument your web apps with logging and metrics.
https://github.com/gocraft/work - Process background jobs in Go.
3. https://github.com/go-martini/martini
提供了优雅的路由实现,以及服务注入功能,可惜框架已经不再维护。
m.Get("/", func() (int, string) {
return 418, "i'm a teapot" // HTTP 418 : "i'm a teapot"
})
m.Get("/", func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
res.WriteHeader(200) // HTTP 200
})
注入 db
db := &MyDatabase{}
m := martini.Classic()
m.Map(db) // the service will be available to all handlers as *MyDatabase
// ...
m.Run()
包含比较丰富的中间件,比如
https://github.com/martini-contrib/binding
type ContactForm struct {
Name string `form:"name" binding:"required"`
Email string `form:"email"`
Message string `form:"message" binding:"required"`
}
m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s\n",
contact.Name, contact.Email, contact.Message)
})
4. https://github.com/labstack/echo
和 gin 极其相似,对标 Gin 的一款框架。包含路由、中间件、参数解析等主要web功能。在官方文档中描述性能优于Gin。
社区健全,文档完善。
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
// Root level middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Group level middleware
g := e.Group("/admin")
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if username == "joe" && password == "secret" {
return true, nil
}
return false, nil
}))
// Route level middleware
track := func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
println("request to /users")
return next(c)
}
}
e.GET("/users", func(c echo.Context) error {
return c.String(http.StatusOK, "/users")
}, track)
https://github.com/gofiber/fiber
对标Express,构建于Fasthttp 之上,高性能,低内存占用。
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/pug"
)
func main() {
// You can setup Views engine before initiation app:
app := fiber.New(fiber.Config{
Views: pug.New("./views", ".pug"),
})
// And now, you can call template `./views/home.pug` like this:
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("home", fiber.Map{
"title": "Homepage",
"year": 1999,
})
})
log.Fatal(app.Listen(":3000"))
}
同样构建在 fasthttp 上的还有:
https://github.com/qiangxue/fasthttp-routing
https://github.com/fasthttp/router
https://github.com/vincentLiuxiang/lu
https://github.com/gogearbox/gearbox
包含了简单的路由功能。
https://github.com/savsgio/atreugo
路由+响应封装
https://github.com/gobuffalo/buffalo
提供生成项目脚手架,完整、完善的框架。可以通过命令行生成代码,对标 ruby。
├── .yarn/
├── actions/
│ ├── app.go
│ └── render.go
├── assets/
├── cmd/
│ └── app/
│ └── main.go
├── config/
├── fixtures/
├── grifts/
├── locales/
├── models/
├── public/
├── templates/
├── .babelrc
├── .buffalo.dev.yml
├── .codeclimate.yml
├── .docketignore
├── .env
├── .gitignore
├── .pnp.loader.mjs
├── .yarnrc.yml
├── database.yml
├── Dockerfile
├── go.mod
├── go.sum
├── inflections.json
├── package.json
├── postcss.config.js
├── README.md
├── webpack.config.js
└── yarn.lock
版权声明
本文章由作者“衡于墨”创作,转载请注明出处,未经允许禁止用于商业用途
评论区#
还没有评论哦,期待您的评论!
引用发言