Just a lightweight golang web application middleware
go get github.com/alash3al/olive-go
package main
import "net/http"
import "github.com/alash3al/olive-go"
func main() {
olive.New().GET("/", func(ctx *olive.Context) bool {
ctx.SetBody("index")
// return false = "don't run the next matched route with the same method and pattern if any"
// this feature allows you to run multiple routes with the same properties
return false
}).ANY("/page/?(.*?)", func(ctx *olive.Context) bool {
var body []byte
ctx.LimitBody(20)
err := ctx.GetBody(&body)
ctx.SetBody("this is your input \n")
ctx.SetBody(body)
_ = err
return true
}).GET("/page", func(ctx *olive.Context) bool {
ctx.SetBody([]byte("hi !"))
return false
}).POST("/page/([^/]+)/and/([^/]+)", func(ctx *olive.Context) bool {
var input map[string]string
ctx.GetBody(&input, 512) // parse the request body into {input} and returns error if any
ctx.SetBody(ctx.Params)
return false
}).GroupBy("path", "/api/v1", func(ApiV1 *olive.App){
ApiV1.GET("/ok", func(ctx *olive.Context) bool {
ctx.Res.Write([]byte("api/v1/ok"))
return false
}).GET("/page/([^/]+)/and/([^/]+)", func(ctx *olive.Context) bool {
// Params[0] -> current-hostname
// from parent[1] -> the path regex values
ctx.Res.Write([]byte("api/v1/ " + ctx.Params[1] + " " + ctx.Params[2]))
return false
})
}).ANY("?.*?", olive.Handler(http.NotFoundHandler(), false)).Listen(":80")
}
Version 3.0
Context.GetQuery
now accepts new param calledbody
and its type is bool, so you can get the request body as url-decoded as url.ValuesContext.GetBody
now accepts one paramater, and you don't need tomake([]byte, ...)
just pass a&v
wherev
is[]byte
- added
Context.LimitBody
to limit the request body to prevent any memory-leaks attacks while reading it usingContext.GetBody
.
Version 2.0
- removed panics handler
- removed
Context.AddHeaders()
andContext.SetHeaders()
- added
Context.DelHeader()
- renamed
Context.Query()
toContext.GetQuery()
- renamed
Context.Body()
toContext.GetBody()
- renamed
Context.Send()
toContext.SetBody()
- added support for html templates in
Context.SetBody()
- renamed
App.Group()
toApp.GroupBy
- add support for custom vhost routing