Olive - just a tiny very simple web app framework for Go

JoniSeidel 8年前發布 | 8K 次閱讀 HTML 中間件 Go語言 Web框架

來自: https://github.com/alash3al/olive-go

olive.go

Just a lightweight golang web application middleware

Author

Mohammed Al Ashaal, a full-stack developer

Install

go get github.com/alash3al/olive-go

Docs

Godoc

Quick overview:

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 { ctx.Res.Write([]byte("api/v1/ " + ctx.Params[0] + " " + ctx.Params[1])) return false }) }).ANY("?.*?", olive.Handler(http.NotFoundHandler(), false)).Listen(":80") }</pre>

Changes

Version 3.0

  • Context.GetQuery now accepts new param called body and its type is bool, so you can get the request body as url-decoded as url.Values
  • Context.GetBody now accepts one paramater, and you don't need to make([]byte, ...) just pass a &v where v is []byte
  • added Context.LimitBody to limit the request body to prevent any memory-leaks attacks while reading it using Context.GetBody .

Version 2.0

  • removed panics handler
  • removed Context.AddHeaders() and Context.SetHeaders()
  • added Context.DelHeader()
  • renamed Context.Query() to Context.GetQuery()
  • renamed Context.Body() to Context.GetBody()
  • renamed Context.Send() to Context.SetBody()
  • added support for html templates in Context.SetBody()
  • renamed App.Group() to App.GroupBy
  • add support for custom vhost routing
  • </ul> </article>

 本文由用戶 JoniSeidel 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!