Mango - keep simple, keep easy to use.
Installation
To get the package, execute:
go get gopkg.in/mango.v0
To import this package, add the following line to your code:
import "gopkg.in/mango.v0"
Quick Start
func index(ctx contracts.Context) (int, interface{}) {
return 200, map[string]interface{}{
"message": "hello mango",
}
}
func main() {
m := mango.Default()
m.Get("/", index)
m.Start(":8080")
}
Route Definition
Basic Guide
m.Get("/", index)
m.Post("/", index)
m.Put("/", index)
m.Delete("/", index)
m.Any("/any", index) //GET,POST,PUT,DELETE
Routes Group
m.Group("/api", func(api *mango.GroupRouter) {
api.Get("/", getApi)
api.Post("/", postApi)
api.Put("/", putApi)
api.Delete("/", deleteApi)
api.Any("/", anyApi) //GET,POST,PUT,DELETE
})
Nested Routes Group
m.Group("/api", func(api *mango.GroupRouter) {
api.Group("/v1", func(v1 *mango.GroupRouter) {
v1.Get("/", getApiV1)
v1.Post("/", postApiV1)
v1.Put("/", putApiV1)
v1.Delete("/", deleteApiV1)
v1.Any("/", anyApiV1) //GET,POST,PUT,DELETE
})
})
Route Middleware
func myMiddleware() mango.MiddlerFunc {
return func(ctx *mango.Context) {
//do something before route handler executed.
ctx.Next()
//do something before response sent to client.
}
}
m.Use(myMiddleware())
//or
m.Get("/", index, myMiddleware())
//or
m.Group("/api", func(api *mango.GroupRouter){
}, myMiddleware())
Built-in Middlewares
- Record
- Recovery
- Cors
- Static
- Redirect
- Compress
- Throttle
- ...
Serve Mode
HTTP Mode
m.Start(":8080")
HTTPS Mode
m.StartTLS(":8080", "cert file content", "key file content")
HTTPS with Let's encrypt Mode
m.StartAutoTLS(":8080", "example.org")
Handle HTTP and HTTPS
go m.Start(":http")
m.StartAutoTLS(":https", "example.org")
Benchmark
??? what's that???
License
Based on MIT license.