ace

package module
v1.0.0-...-83b99d2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2015 License: Apache-2.0 Imports: 18 Imported by: 0

README

ACE godoc badge gocover badge Build Status Go Report Card

Blazing fast Go Web Framework

image

####Installation

go get github.com/plimble/ace
Import
import "github.com/plimble/ace"

Performance

Ace is very fast you can see on this

##Usage

Quick Start
a := ace.New()
a.GET("/:name", func(c *ace.C) {
	name := c.Params.ByName("name")
	c.JSON(200, map[string]string{"hello": name})
})
a.Run(":8080")

Default Middleware (Logger, Recovery)

a := ace.Default()
a.GET("/", func(c *ace.C) {
	c.String(200,"Hello ACE")
})
a.Run(":8080")
Router
a.DELETE("/", HandlerFunc)
a.HEAD("/", HandlerFunc)
a.OPTIONS("/", HandlerFunc)
a.PATCH("/", HandlerFunc)
a.PUT("/", HandlerFunc)
a.POST("/", HandlerFunc)
a.GET("/", HandlerFunc)
Example
	a := ace.New()

	a.GET("/", func(c *ace.C){
		c.String(200, "Hello world")
	})

	a.POST("/form/:id/:name", func(c *ace.C){
		id := c.Params.ByName("id")
		name := c.Params.ByName("name")
		age := c.Request.PostFormValue("age")
	})

Response

JSON
	data := struct{
		Name string `json:"name"`
	}{
		Name: "John Doe",
	}
	c.JSON(200, data)
String
	c.String(200, "Hello Ace")
Download
	//application/octet-stream
	c.Download(200, []byte("Hello Ace"))
HTML
	c.HTML("index.html")
Redirect
	c.Redirect("/home")

Group Router

g := a.Group("/people", func(c *ace.C) {
	fmt.Println("before route func")
	c.Next()
})

// /people/:name
g.GET("/:name", func(c *ace.C) {
	c.JSON(200, map[string]string{"TEST": "GET METHOD"})
})

// /people/:name
g.POST("/:name", func(c *ace.C) {
	c.JSON(200, map[string]string{"TEST": "POST METHOD"})
})

Data

Set/Get data in any HandlerFunc

a.Use(func(c *ace.C){
	c.SetData("isLogin", true)
})

a.Get("/", func(c *ace.C){
	isLogin := c.GetData("isLogin").(bool)
	//or get all data
	//c.GetAllData()
})

Middlewares

Ace middleware is implemented by custom handler

type HandlerFunc func(c *C)

#####Example

a := ace.New()
a.Use(ace.Logger())
Built-in Middlewares
Serve Static
a.Static("/assets", "./img")
Session with Gorilla sessions
var store = sessions.NewCookieStore([]byte("something-very-secret"))
a.UseSession("cookie", store, nil)

a.GET("/hello", func(c *ace.C) {
	c.Session.SetString("name", "John Doe")
	fmt.Println(c.Session.GetString("name"))
}
Logger
a.Use(ace.Logger())
Recovery
a.Use(ace.Recovery())

HTML Template Engine

Ace built on renderer interface. So you can use any template engine

type Renderer interface {
	Render(w http.ResponseWriter, name string, data interface{})
}
ACE Middlewares
Name Description
gzip GZIP compress
cors Enable Cross-origin resource sharing (CORS)
sessions Gorilla Sessions
pongo2 Pongo2 Template Engine
csrf Cross Site Request Forgery protection

###Contributing

If you'd like to help out with the project. You can put up a Pull Request.

Documentation

Index

Constants

View Source
const (
	CookieSession = "cookie"
	RedisSession  = "redis"
	MongoSession  = "mongo"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ace

type Ace struct {
	*Router
	// contains filtered or unexported fields
}

func Default

func Default() *Ace

func New

func New() *Ace

func (*Ace) HtmlTemplate

func (a *Ace) HtmlTemplate(render Renderer)

HtmlTemplate use html template middleware

func (*Ace) Run

func (a *Ace) Run(addr string)

func (*Ace) RunTLS

func (a *Ace) RunTLS(addr string, cert string, key string)

func (*Ace) ServeHTTP

func (a *Ace) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Ace) Session

func (a *Ace) Session(name string, store sessions.Store, options *SessionOptions)

Session use session middleware

type C

type C struct {
	Params  httprouter.Params
	Request *http.Request
	Writer  ResponseWriter

	Session *session
	// contains filtered or unexported fields
}

C is context for every goroutine

func (*C) Abort

func (c *C) Abort()

Abort stop maddileware

func (*C) AbortWithStatus

func (c *C) AbortWithStatus(status int)

AbortWithStatus stop maddileware and return http status code

func (*C) ClientIP

func (c *C) ClientIP() string

ClientIP get ip from RemoteAddr

func (*C) Download

func (c *C) Download(status int, v []byte)

Download response with application/octet-stream; charset=UTF-8 Content type

func (*C) GetAllData

func (c *C) GetAllData() map[string]interface{}

GetAllData return all data

func (*C) GetData

func (c *C) GetData(key string) interface{}

Get data

func (*C) HTML

func (c *C) HTML(name string, data interface{})

HTML render template engine

func (*C) HTTPLang

func (c *C) HTTPLang() string

HTTPLang get first language from HTTP Header

func (*C) JSON

func (c *C) JSON(status int, v interface{})

JSON response with application/json; charset=UTF-8 Content type

func (*C) MustPostFloat64

func (c *C) MustPostFloat64(key string, d float64) float64

func (*C) MustPostInt

func (c *C) MustPostInt(key string, d int) int

func (*C) MustPostString

func (c *C) MustPostString(key, d string) string

func (*C) MustPostStrings

func (c *C) MustPostStrings(key string, d []string) []string

func (*C) MustPostTime

func (c *C) MustPostTime(key string, layout string, d time.Time) time.Time

func (*C) MustQueryFloat64

func (c *C) MustQueryFloat64(key string, d float64) float64

func (*C) MustQueryInt

func (c *C) MustQueryInt(key string, d int) int

func (*C) MustQueryString

func (c *C) MustQueryString(key, d string) string

func (*C) MustQueryStrings

func (c *C) MustQueryStrings(key string, d []string) []string

func (*C) MustQueryTime

func (c *C) MustQueryTime(key string, layout string, d time.Time) time.Time

func (*C) Next

func (c *C) Next()

Next next middleware

func (*C) ParseJSON

func (c *C) ParseJSON(v interface{}) error

ParseJSON decode json to interface{}

func (*C) Redirect

func (c *C) Redirect(url string)

Redirect 302 response

func (*C) SetData

func (c *C) SetData(key string, v interface{})

Set data

func (*C) String

func (c *C) String(status int, format string, val ...interface{})

String response with text/html; charset=UTF-8 Content type

type Context

type Context map[string]interface{}

type HandlerFunc

type HandlerFunc func(c *C)

func Logger

func Logger() HandlerFunc

NewLogger returns a new Logger instance

func Recovery

func Recovery() HandlerFunc

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. While Martini is in development mode, Recovery will also output the panic as HTML.

type PanicHandler

type PanicHandler func(c *C, rcv interface{})

type Renderer

type Renderer interface {
	Render(w http.ResponseWriter, name string, data interface{})
}

Renderer html render interface

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has not been written.
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

type Router

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

Router http router

func (*Router) DELETE

func (r *Router) DELETE(path string, handlers ...HandlerFunc)

DELETE handle DELETE method

func (*Router) GET

func (r *Router) GET(path string, handlers ...HandlerFunc)

GET handle GET method

func (*Router) Group

func (r *Router) Group(path string, handlers ...HandlerFunc) *Router

Group group route

func (*Router) HEAD

func (r *Router) HEAD(path string, handlers ...HandlerFunc)

HEAD handle HEAD method

func (*Router) HTTPHandlerFunc

func (r *Router) HTTPHandlerFunc(h http.HandlerFunc) HandlerFunc

HandlerFunc convert http.HandlerFunc to ace.HandlerFunc

func (*Router) Handle

func (r *Router) Handle(method, path string, handlers []HandlerFunc)

Handle handle with specific method

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handlers ...HandlerFunc)

OPTIONS handle OPTIONS method

func (*Router) PATCH

func (r *Router) PATCH(path string, handlers ...HandlerFunc)

PATCH handle PATCH method

func (*Router) POST

func (r *Router) POST(path string, handlers ...HandlerFunc)

POST handle POST method

func (*Router) PUT

func (r *Router) PUT(path string, handlers ...HandlerFunc)

PUT handle PUT method

func (*Router) Panic

func (r *Router) Panic(h PanicHandler)

Panic call when panic was called

func (*Router) RouteNotFound

func (r *Router) RouteNotFound(h HandlerFunc)

RouteNotFound call when route does not match

func (*Router) Static

func (r *Router) Static(path string, root http.Dir, handlers ...HandlerFunc)

Static server static file path is url path root is root directory

func (*Router) Use

func (r *Router) Use(middlewares ...HandlerFunc)

Use register middleware

type SessionOptions

type SessionOptions struct {
	Path   string
	Domain string
	// MaxAge=0 means no 'Max-Age' attribute specified.
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
	// MaxAge>0 means Max-Age attribute present and given in seconds.
	MaxAge   int
	Secure   bool
	HTTPOnly bool
}

SessionOptions session options

Jump to

Keyboard shortcuts

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