Documentation
¶
Overview ¶
Olive (v2.0) A tiny http framework perfect for building web services, created by Mohammed Al Ashaal (http://alash3al.xyz) under MIT License . *
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 yout to run multiple routes with the same properties
return false
}).ANY("/page/?(.*?)", func(ctx *olive.Context) bool {
ctx.SetBody("i'm the parent \n")
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")
}
Index ¶
- func Handler(h http.Handler, rtrn bool) handler
- type App
- func (self *App) ANY(path string, cb handler) *App
- func (self *App) CONNECT(path string, cb handler) *App
- func (self *App) DELETE(path string, cb handler) *App
- func (self *App) GET(path string, cb handler) *App
- func (self *App) GroupBy(by, pattern string, fn grouper) *App
- func (self *App) HEAD(path string, cb handler) *App
- func (self App) Listen(addr string) error
- func (self App) ListenTLS(addr string, certFile string, keyFile string) error
- func (self *App) METHOD(method, path string, cb handler) *App
- func (self *App) OPTIONS(path string, cb handler) *App
- func (self *App) PATCH(path string, cb handler) *App
- func (self *App) POST(path string, cb handler) *App
- func (self *App) PUT(path string, cb handler) *App
- func (self App) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (self *App) TRACE(path string, cb handler) *App
- type Context
- func (self Context) AddHeader(k, v string)
- func (self Context) DelHeader(k string)
- func (self Context) GetBody(v interface{}, max int64) (err error)
- func (self Context) GetHeader(k string) string
- func (self Context) GetQuery() url.Values
- func (self Context) SetBody(d ...interface{})
- func (self Context) SetHeader(k, v string)
- func (self Context) SetStatus(code int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
Our main structure
func (*App) ANY ¶
Handle ANY request method for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) CONNECT ¶
Handle CONNECT request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) DELETE ¶
Handle DELETE request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) GET ¶
Handle GET request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) GroupBy ¶
Group multiple routes by the specified section and its pattern with its grouper, olive currently supports by "host" or "path" only .
func (*App) HEAD ¶
Handle HEAD request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) METHOD ¶
Handle the specified custom method for the specified path; NOTE: method could be a "regexp string"; NOTE: path could be a "regexp string";
func (*App) OPTIONS ¶
Handle OPTIONS request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) PATCH ¶
Handle PATCH request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) POST ¶
Handle POST request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
func (*App) PUT ¶
Handle PUT request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"
type Context ¶
type Context struct {
Req *http.Request
Res http.ResponseWriter
Params []string
Vars map[string]interface{}
}
A request context and properties
func (Context) GetBody ¶
Read the request body into the provided variable, it will read the body as json if the specified "v" isn't ([]byte, io.Writer) .
func (Context) SetBody ¶
func (self Context) SetBody(d ...interface{})
Write to the client, this function will detect the type of the data, it will send the data as json if the specified input isn't (string, []byte, *template.Template and io.Reader), it execute the input as html template if the first argument is *template.Template and second argument is template's data .