olive

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2016 License: MIT Imports: 4 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.Res.Write([]byte("index"))
			return false
		}).CONNECT("/", func(ctx *olive.Context) bool {
			// olive automatically catch any panic and recover it to the 
			// "stdout" using "log" package .
			panic("connect method !")
			return false
		}).ANY("/page/?(.*?)", func(ctx *olive.Context) bool {
			ctx.Res.Write([]byte("i'm the parent \n"))
			return true
		}).GET("/page", func(ctx *olive.Context) bool {
			ctx.Res.Write([]byte("page"))
			return false
		}).GET("/page/([^/]+)/and/([^/]+)", func(ctx *olive.Context) bool {
			ctx.Res.Write([]byte(ctx.Params[0] + " " + ctx.Params[1]))
			return false
		}).Group("/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")
	}

Documentation

Overview

A tiny http framework perfect for building web services . *

package main

import "net/http"
import "github.com/alash3al/olive-go"

func main() {
	olive.New().GET("/", func(ctx *olive.Context) bool {
		ctx.Res.Write([]byte("index"))
		return false
	}).CONNECT("/", func(ctx *olive.Context) bool {
		// olive automatically catch any panic and recover it to the
		// "stdout" using "log" package .
		panic("connect method !")
		return false
	}).ANY("/page/?(.*?)", func(ctx *olive.Context) bool {
		ctx.Res.Write([]byte("i'm the parent \n"))
		return true
	}).GET("/page", func(ctx *olive.Context) bool {
		ctx.Res.Write([]byte("page"))
		return false
	}).GET("/page/([^/]+)/and/([^/]+)", func(ctx *olive.Context) bool {
		ctx.Res.Write([]byte(ctx.Params[0] + " " + ctx.Params[1]))
		return false
	}).Group("/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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

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

Convert any http.Handler compatible handler to an internal callback

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 callback) *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 callback) *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 callback) *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 callback) *App

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

func (*App) Group

func (self *App) Group(path string, fn gfn) *App

Group some of routes under the specified path/pattern the group function will pass the current instance of App to the grouper .

func (*App) HEAD

func (self *App) HEAD(path string, cb callback) *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 callback) *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 callback) *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 callback) *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 callback) *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 callback) *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 callback) *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

Jump to

Keyboard shortcuts

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