olive

package module
v3.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2016 License: MIT Imports: 8 Imported by: 0

README

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")
}

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

Documentation

Overview

Olive Version 3.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler, rtrn bool) handler

Convert any http.Handler compatible handler to an internal handler, and rtrn is what will be returned for "[don't]run the next matched route with the same method and patttern"

Types

type App

type App struct {
	// contains filtered or unexported fields
}

Our main structure

func New

func New() *App

Create a new instance

func (*App) ANY

func (self *App) ANY(path string, cb handler) *App

Handle ANY request method for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) CONNECT

func (self *App) CONNECT(path string, cb handler) *App

Handle CONNECT request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) DELETE

func (self *App) DELETE(path string, cb handler) *App

Handle DELETE request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) GET

func (self *App) GET(path string, cb handler) *App

Handle GET request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) GroupBy

func (self *App) GroupBy(by, pattern string, fn grouper) *App

Group multiple routes by the specified section and its pattern with its grouper, olive currently supports by "host" or "path" only .

func (*App) HEAD

func (self *App) HEAD(path string, cb handler) *App

Handle HEAD request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (App) Listen

func (self App) Listen(addr string) error

An Alias to http.ListenAndServe

func (App) ListenTLS

func (self App) ListenTLS(addr string, certFile string, keyFile string) error

An Alias to http.ListenAndServeTLS

func (*App) METHOD

func (self *App) METHOD(method, path string, cb handler) *App

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

func (self *App) OPTIONS(path string, cb handler) *App

Handle OPTIONS request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) PATCH

func (self *App) PATCH(path string, cb handler) *App

Handle PATCH request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) POST

func (self *App) POST(path string, cb handler) *App

Handle POST request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (*App) PUT

func (self *App) PUT(path string, cb handler) *App

Handle PUT request for the specified path with the specified callback; NOTE: this is based on "func(self *App) METHOD()"

func (App) ServeHTTP

func (self App) ServeHTTP(res http.ResponseWriter, req *http.Request)

Dispatch all registered routes

func (*App) TRACE

func (self *App) TRACE(path string, cb handler) *App

Handle TRACE 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) AddHeader

func (self Context) AddHeader(k, v string)

Add a header field

func (Context) DelHeader

func (self Context) DelHeader(k string)

Delete the specified header field

func (Context) GetBody

func (self Context) GetBody(v interface{}) (err error)

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) GetHeader

func (self Context) GetHeader(k string) string

Get the specified header field "from the request"

func (Context) GetQuery

func (self Context) GetQuery(body bool) url.Values

Return the url query vars, if body is true, then this function will parse the request body as x-www-form-urlencoded .

func (Context) LimitBody

func (self Context) LimitBody(max int64)

Limit the size of the request body .

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, you MUST LimitBody() to prevent memory-leaks attacks .

func (Context) SetHeader

func (self Context) SetHeader(k, v string)

Set a http header field

func (Context) SetStatus

func (self Context) SetStatus(code int)

Set the status code

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL