clevergo

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2016 License: MIT Imports: 11 Imported by: 1

README

CleverGo

Go Report Card GoDoc Build Status license GitHub release

CleverGo is a simple, high performance and secure web framework for Go (go programing language). It built on top of fasthttp.

Because it is a new project, so it have not do more tests, but this task has been added to TODO LIST.

Some common features and components can be found at https://github.com/clevergo.

Such as: websocket, sessions, captcha, csrf, jwt

  1. Installation
  2. Documentation
  3. Features
  4. Middlewares
  5. Examples
  6. Contribution
  7. Relevant Packages

Installation

go get github.com/headwindfly/clevergo
Documentation

The documentations is not complete now, but we provided completed examples.

Back to top

Features
  • High performance
  1. CleverGo uses fasthttp instead of net/http, so it is more fast than net/http‘s frameworks.
  2. CleverGo's handler router is a high performance router(fork from fasthttprouter).
  3. Simple architecture.
  4. No reflect.

Please refer to Go Web Framework Benchmark for getting more detail.

  • Lightweight

CleverGo's architecture is very simple, such as the Middleware and Handler.

  • Easy to use

We provides some examples below, see also Examples.

  • Multiple Domains

See also Application example

Back to top

Middlewares
Name Description Usage
Session Middlware Session Middleware Session Middlware
CSRF Middleware CSRF attack protection CSRF Middleware
JWT Middleware JSON WEB TOKEN Middleware JWT Middleware

Back to top

Examples
Name Description Usage
Basic Usage Basic Usage Basic Usage
Middleware Middleware Middleware
Websocket Websocket Websocket
Session Session Session
RESTFUL API RESTFUL API Restful API
CSRF Middleware CSRF attack protection CSRF Protection
Captcha Captcha Captcha
JSON WEB TOKEN JSON WEB TOKEN JSON WBE TOKEN

More examples can be found at Examples.

Back to top

TODO LIST
  1. Perfect the documents and examples.
  2. Add tests.

Back to top

Contribution
  1. Fork this repository.
  2. Added your code on your repository.
  3. Send pull request.

I am willing to accept any pull requests and advises.

Back to top

Relevant Packages

Most of packages can be found at https://github.com/clevergo.

Back to top

Actual Applications

How to add my application?

Fork and added your application in README.md and then send pull request.

Back to top

Documentation

Index

Constants

View Source
const (
	// Version of CleverGo.
	Version = "1.1.1"

	Logo = `` /* 256-byte string literal not displayed */

)
View Source
const (

	// ServerTypeDefault means HTTP Application.
	ServerTypeDefault = 1
	// ServerTypeUNIX means UNIX Application.
	ServerTypeUNIX = 2
	// ServerTypeTLS means TLS Application.
	ServerTypeTLS = 3
	// ServerTypeTLSEmbed means TLSEmbed Application.
	ServerTypeTLSEmbed = 4
)

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, handler fasthttp.RequestHandler) error

ListenAndServe is a alias of fasthttp.ListenAndServe.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler fasthttp.RequestHandler) error

ListenAndServeTLS is a alias of fasthttp.ListenAndServeTLS.

func ListenAndServeTLSEmbed

func ListenAndServeTLSEmbed(addr string, certData, keyData []byte, handler fasthttp.RequestHandler) error

ListenAndServeTLSEmbed is a alias of fasthttp.ListenAndServeTLSEmbed.

func ListenAndServeUNIX

func ListenAndServeUNIX(addr string, mode os.FileMode, handler fasthttp.RequestHandler) error

ListenAndServeUNIX is a alias of fasthttp.ListenAndServeUNIX.

Types

type Application added in v1.0.2

type Application struct {
	Config *Config // configuration.
	// contains filtered or unexported fields
}

Application for managing routers.

func NewApplication added in v1.0.2

func NewApplication() *Application

NewApplication returns an application's instance.

func (*Application) AddRouter added in v1.0.2

func (a *Application) AddRouter(domain string, r *Router)

AddRouter for adding router to application.

Set the current router as default router if the domain is an empty string.

func (*Application) Handler added in v1.0.2

func (a *Application) Handler(ctx *fasthttp.RequestCtx)

Handler returns application' Handler.

func (*Application) NewRouter added in v1.1.1

func (a *Application) NewRouter(domain string) *Router

NewRouter returns a new Router's instance.

Set the current router as default router if the domain is an empty string.

func (*Application) Run added in v1.0.2

func (a *Application) Run()

Run fro starting the application.

func (*Application) SetDefaultRouter added in v1.0.2

func (a *Application) SetDefaultRouter(r *Router)

SetDefaultRouter for setting default router.

func (*Application) SetLogger added in v1.0.2

func (a *Application) SetLogger(logger fasthttp.Logger)

SetLogger for setting logger.

func (*Application) SetSessionStore added in v1.0.2

func (a *Application) SetSessionStore(store sessions.Store)

SetSessionStore for setting session store.

type Config added in v1.0.2

type Config struct {
	ServerAddr     string      // Server address.
	ServerType     int         // Server type.
	ServerMode     os.FileMode // Server mode for UNIX application.
	ServerCertFile string      // CertFile for TLS application.
	ServerKeyFile  string      // KeyFile  for TLS application.
	ServerCertData []byte      // CertData  for TLSEmbed application.
	ServerKeyData  []byte      // KeyData  for TLSEmbed application.
}

Config for Application.

func NewConfig added in v1.0.2

func NewConfig() *Config

NewConfig returns default configuration.

func (*Config) IsServeTLS added in v1.0.2

func (c *Config) IsServeTLS() bool

IsServeTLS returns a boolean indicating whether is TLS Application.

func (*Config) IsServeTLSEmbed added in v1.0.2

func (c *Config) IsServeTLSEmbed() bool

IsServeTLSEmbed returns a boolean indicating whether is TLSEmbed Application.

func (*Config) IsServeUNIX added in v1.0.2

func (c *Config) IsServeUNIX() bool

IsServeUNIX returns a boolean indicating whether is UNIX Application.

type Context

type Context struct {
	*fasthttp.RequestCtx
	RouterParams *router.Params
	Session      *sessions.Session
	// contains filtered or unexported fields
}

Context of request.

It contains the router, session and params.

func NewContext

func NewContext(r *Router, ctx *fasthttp.RequestCtx, rps *router.Params) *Context

NewContext returns a Context instance.

Firstly, it will try to get Context instance from contextPool. If failed to get Context from contextPool, returns a new Context instance.

func (*Context) Close added in v1.0.1

func (ctx *Context) Close()

Close Context.

Context should be closed after finishing request, and at this moment, put the context into contextPool.

func (*Context) HTML

func (ctx *Context) HTML(body string)

HTML responses HTML data to client.

func (*Context) HTMLWithCode

func (ctx *Context) HTMLWithCode(code int, body string)

HTMLWithCode responses HTML data and custom status code to client.

func (*Context) JSON

func (ctx *Context) JSON(v interface{})

JSON responses JSON data to client.

func (*Context) JSONP

func (ctx *Context) JSONP(v interface{}, callback []byte)

JSONP responses JSONP data to client.

func (*Context) JSONPWithCode

func (ctx *Context) JSONPWithCode(code int, v interface{}, callback []byte)

JSONPWithCode responses JSONP data and custom status code to client.

func (*Context) JSONWithCode

func (ctx *Context) JSONWithCode(code int, v interface{})

JSONWithCode responses JSON data and custom status code to client.

func (*Context) Logger added in v1.0.2

func (ctx *Context) Logger() fasthttp.Logger

Logger returns logger.

Returns the router's logger if the logger is non-nil. Otherwise, returns the default logger of ctx.

func (*Context) SessionStore added in v1.1.0

func (ctx *Context) SessionStore() sessions.Store

SessionStore returns the session store of router.

func (*Context) SetContentTypeToHTML added in v1.0.3

func (ctx *Context) SetContentTypeToHTML()

SetContentTypeToHTML set Content-Type to HTML.

func (*Context) SetContentTypeToJSON added in v1.0.3

func (ctx *Context) SetContentTypeToJSON()

SetContentTypeToJSON set Content-Type to JSON.

func (*Context) SetContentTypeToJSONP added in v1.0.3

func (ctx *Context) SetContentTypeToJSONP()

SetContentTypeToJSONP set Content-Type to JSONP.

func (*Context) SetContentTypeToXML added in v1.0.3

func (ctx *Context) SetContentTypeToXML()

SetContentTypeToXML set Content-Type to XML.

func (*Context) Text

func (ctx *Context) Text(a ...interface{})

Text responses text data to client using fmt.Fprint().

func (*Context) Textf

func (ctx *Context) Textf(format string, a ...interface{})

Textf responses text data to client using fmt.Fprintf().

func (*Context) XML

func (ctx *Context) XML(v interface{}, headers ...string)

XML responses XML data to client.

func (*Context) XMLWithCode

func (ctx *Context) XMLWithCode(code int, v interface{}, headers ...string)

XMLWithCode responses XML data and custom status code to client.

type Controller

type Controller struct{}

Controller is an empty struct.

func (Controller) DELETE

func (c Controller) DELETE(ctx *Context)

DELETE for handling the DELETE request.

func (Controller) GET

func (c Controller) GET(ctx *Context)

GET for handling the GET request.

func (Controller) HEAD

func (c Controller) HEAD(ctx *Context)

HEAD for handling the HEAD request.

func (Controller) Handle

func (c Controller) Handle(next Handler) Handler

Handle implemented Middleware Interface.

func (Controller) OPTIONS

func (c Controller) OPTIONS(ctx *Context)

OPTIONS for handling the OPTIONS request.

func (Controller) PATCH

func (c Controller) PATCH(ctx *Context)

PATCH for handling the PATCH request.

func (Controller) POST

func (c Controller) POST(ctx *Context)

POST for handling the POST request.

func (Controller) PUT

func (c Controller) PUT(ctx *Context)

PUT for handling the PUT request.

type ControllerInterface

type ControllerInterface interface {
	Handle(next Handler) Handler // Implemented Middleware Interface.

	DELETE(ctx *Context)  // Request handler for DELETE request.
	GET(ctx *Context)     // Request handler for GET request.
	HEAD(ctx *Context)    // Request handler for HEAD request.
	OPTIONS(ctx *Context) // Request handler for OPTIONS request.
	PATCH(ctx *Context)   // Request handler for PATCH request.
	POST(ctx *Context)    // Request handler for POST request.
	PUT(ctx *Context)     // Request handler for PUT request.
}

ControllerInterface contains the methods which the Controller should implements.

In fact, the controller is a middleware.

type Handler

type Handler interface {
	Handle(*Context)
}

A Handler responds to an HTTP request.

type HandlerFunc

type HandlerFunc func(*Context)

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx *Context)

Handle calls f(ctx).

type Middleware

type Middleware interface {
	Handle(next Handler) Handler // handle request.
}

Middleware Interface.

type Router

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

Router for managing request handlers.

func NewRouter

func NewRouter() *Router

NewRouter returns a Router's instance.

func (*Router) AddMiddleware

func (r *Router) AddMiddleware(middleware Middleware)

AddMiddleware for adding middleware.

func (*Router) DELETE

func (r *Router) DELETE(path string, handler Handler)

DELETE for adding DELETE request handler.

func (*Router) GET

func (r *Router) GET(path string, handler Handler)

GET for adding GET request handler.

func (*Router) HEAD

func (r *Router) HEAD(path string, handler Handler)

HEAD for adding HEAD request handler.

func (*Router) Handle

func (r *Router) Handle(method, path string, handler Handler)

Handle for adding custom METHOD request handler.

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler Handler)

OPTIONS for adding OPTIONS request handler.

func (*Router) PATCH

func (r *Router) PATCH(path string, handler Handler)

PATCH for adding PATCH request handler.

func (*Router) POST

func (r *Router) POST(path string, handler Handler)

POST for adding POST request handler.

func (*Router) PUT

func (r *Router) PUT(path string, handler Handler)

PUT for adding PUT request handler.

func (*Router) RegisterController

func (r *Router) RegisterController(route string, c ControllerInterface)

RegisterController for registering controller.

The Controller should implemented the ControllerInterface.

func (*Router) SetLogger added in v1.0.2

func (r *Router) SetLogger(logger fasthttp.Logger)

SetLogger for setting logger.

func (*Router) SetMiddlewares

func (r *Router) SetMiddlewares(middlewares []Middleware)

SetMiddlewares for setting middlewares.

func (*Router) SetSessionStore

func (r *Router) SetSessionStore(store sessions.Store)

SetSessionStore for setting session store.

Directories

Path Synopsis
examples
jwt
middlewares
jwt

Jump to

Keyboard shortcuts

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