gas

package module
v0.0.0-...-216fba0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2016 License: MIT Imports: 13 Imported by: 0

README

Gas

go-gas

Build Status codecov Go Report Card

Gas is a Web framework written in Go. And this is not a total complete project.

I just did a minimum workable architechture.

The workable feature is:

  • Router (based on fasthttprouter package)
  • Context (It can easy to render view and print json string)
  • Middleware
  • Logger middleware
  • Log package
  • Read config from a yaml file

And Model is not complete yet. Just finished MySQL sql Builder

all feature you can see in Example Directory.

Install

$ go get github.com/go-gas/gas

Run demo

$ cd $GOPATH/src/github.com/go-gas/gas/Example
$ go run main.go

Your project file structure

|-- $GOPATH
|   |-- src
|       |--Your_Project_Name
|          |-- config
|              |-- default.yaml
|          |-- controllers
|              |-- default.go
|          |-- log
|          |-- models
|          |-- routers
|              |-- routers.go
|          |-- static
|          |-- views
|          |-- main.go

Quick start

Import
import (
    "Your_Project_Name/routers"
    "github.com/go-gas/gas"
    "github.com/go-gas/gas/middleware"
)
New
g := gas.New() // will load "config/default.yaml"

or

g := gas.New("config/path")
Register Routes
routers.RegistRout(g.Router)

Then in your routers.go

package routers

import (
    "Your_Project_Name/controllers"
    "github.com/go-gas/gas"
)

func RegistRout(r *gas.Router)  {

    r.Get("/", controllers.IndexPage)
    r.Post("/post/:param", controllers.PostTest)

    rc := &controllers.RestController{}
    r.REST("/User", rc)

}
Register middleware
g.Router.Use(middleware.LogMiddleware)

And you can write your own middleware function

func LogMiddleware(next gas.CHandler) gas.CHandler {
    return func (c *gas.Context) error  {

       // do something before next handler

       err := next(c)

       // do something after next handler

       return err
    }
}
And done
g.Run()

Benchmark

Using go-web-framework-benchmark to benchmark with another web fframework.

go-gas-benchmark
Benchmark-alloc
go-gas-benchmark-alloc
Benchmark-latency
go-gas-benchmark-latency
Benchmark-pipeline
go-gas-benchmark-pipeline

Concurrency

go-gas-concurrency
Concurrency-alloc
go-gas-concurrency-alloc
Concurrency-latency
go-gas-concurrency-latency
Concurrency-pipeline
go-gas-concurrency-pipeline

Benchmark conclusion

Iris is still the fastest web framework.

But gas is very new, so in the future

I wish this framework might not be a fastest but it will very fast and full featured.

Roadmap
  • Models
  • Model fields mapping
  • ORM
  • Relation mapping
  • Transaction
  • QueryBuilder
  • Session
  • Filesystem
  • Database
  • Redis
  • Memcache
  • Cache
  • Memory
  • File
  • Redis
  • Memcache
  • i18n
  • HTTPS
  • Command line tools
  • Form handler (maybe next version)
  • Security check features(csrf, xss filter...etc)

Documentation

Overview

gas is a web framework.

Example

Your project file structure

|-- $GOPATH
|   |-- src
|       |--Your_Project_Name
|          |-- config
|              |-- default.yaml
|          |-- controllers
|              |-- default.go
|          |-- log
|          |-- models
|          |-- routers
|              |-- routers.go
|          |-- static
|          |-- views
|          |-- main.go

main.go

 import (
 	"Your_Project_Name/routers"
	"github.com/go-gas/gas"
 )

 // Create gas object with config path
 // default is config/default.yaml
 g := gas.New("config/path")

 // register route
 routers.RegistRout(g.Router)

 // run and listen
 g.Run()

routers.go

import (
	"Your_Project_Name/controllers"
	"github.com/go-gas/gas"
)

func RegistRout(r *gas.Router)  {

	r.Get("/", controllers.IndexPage)
	r.Post("/post/:param", controllers.PostTest)

	rc := &controllers.RestController{}
	r.REST("/User", rc)

}

controllers.go

package controllers

import (
	"github.com/go-gas/gas"
)

func IndexPage(ctx *gas.Context) error {
	return ctx.Render("", "views/layout.html", "views/index.html")
}

func PostTest(ctx *gas.Context) error {
	a := map[string]string{
		"Name": ctx.GetParam("param"),
	}

	return ctx.Render(a, "views/layout2.html")
}

rest_controller.go

import (
	"github.com/go-gas/gas"
)

type RestController struct {
	gas.ControllerInterface
}

func (rc *RestController) Get(c *gas.Context) error {

	return c.STRING(200, "Test Get")
}

func (rc *RestController) Post(c *gas.Context) error {

	return c.STRING(200, "Test Post")
}

Index

Constants

View Source
const (
	ApplicationJSON                  = "application/json"
	ApplicationJSONCharsetUTF8       = ApplicationJSON + "; " + CharsetUTF8
	ApplicationJavaScript            = "application/javascript"
	ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8
	ApplicationXML                   = "application/xml"
	ApplicationXMLCharsetUTF8        = ApplicationXML + "; " + CharsetUTF8
	ApplicationForm                  = "application/x-www-form-urlencoded"
	ApplicationProtobuf              = "application/protobuf"
	ApplicationMsgpack               = "application/msgpack"
	TextHTML                         = "text/html"
	TextHTMLCharsetUTF8              = TextHTML + "; " + CharsetUTF8
	TextPlain                        = "text/plain"
	TextPlainCharsetUTF8             = TextPlain + "; " + CharsetUTF8
	MultipartForm                    = "multipart/form-data"

	CharsetUTF8 = "charset=utf-8"

	AcceptEncoding     = "Accept-Encoding"
	Authorization      = "Authorization"
	ContentDisposition = "Content-Disposition"
	ContentEncoding    = "Content-Encoding"
	ContentLength      = "Content-Length"
	ContentType        = "Content-Type"
	Location           = "Location"
	Upgrade            = "Upgrade"
	Vary               = "Vary"
	WWWAuthenticate    = "WWW-Authenticate"
	XForwardedFor      = "X-Forwarded-For"
	XRealIP            = "X-Real-IP"
)

Variables

This section is empty.

Functions

func New

func New(configPath ...string) *gas

New gas Object

Ex:

g := New()
g.Run()

Types

type Context

type Context struct {
	//context.Context
	*fasthttp.RequestCtx
	// contains filtered or unexported fields
}

func (*Context) CloseDB

func (ctx *Context) CloseDB() error

Close db connection

func (*Context) GetModel

func (ctx *Context) GetModel() model.ModelInterface

Get model using context in controller

func (*Context) GetParam

func (ctx *Context) GetParam(name string) string

Get parameter from post or get value

func (*Context) HTML

func (ctx *Context) HTML(code int, html string) error

Set the response data-type to html

func (*Context) JSON

func (ctx *Context) JSON(status int, data interface{}) error

Set response data-type to json and try to json encode

func (*Context) Render

func (ctx *Context) Render(data interface{}, tplPath ...string) error

Render function combined data and template to show

func (*Context) STRING

func (ctx *Context) STRING(status int, data string) error

Set the response data-type to plain text

type Controller

type Controller struct {
	ControllerInterface
}

Controller base class

type ControllerInterface

type ControllerInterface interface {
}

ControllerInterface defind controller interface

type GasHandler

type GasHandler func(*Context) error

CHandler is a function type for rout handler

type MiddlewareFunc

type MiddlewareFunc func(GasHandler) GasHandler

MiddlewareFunc middlewarefunc define

type PanicHandler

type PanicHandler func(*Context, interface{}) error

PanicHandler defined panic handler

type Router

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

Router class include httprouter and gas

func (*Router) Delete

func (r *Router) Delete(path string, ch GasHandler)

Delete REST funcs

func (*Router) Get

func (r *Router) Get(path string, ch GasHandler)

Get REST funcs

func (*Router) Head

func (r *Router) Head(path string, ch GasHandler)

Head REST funcs

func (*Router) Options

func (r *Router) Options(path string, ch GasHandler)

Options REST funcs

func (*Router) Patch

func (r *Router) Patch(path string, ch GasHandler)

Patch REST funcs

func (*Router) Post

func (r *Router) Post(path string, ch GasHandler)

Post REST funcs

func (*Router) Put

func (r *Router) Put(path string, ch GasHandler)

Put REST funcs

func (*Router) REST

func (r *Router) REST(path string, c ControllerInterface)

REST for set all REST route

func (*Router) SetNotFoundHandler

func (r *Router) SetNotFoundHandler(h GasHandler)

SetNotFoundHandler set Notfound and Panic handler

func (*Router) SetPanicHandler

func (r *Router) SetPanicHandler(ph PanicHandler)

func (*Router) StaticPath

func (r *Router) StaticPath(dir string)

func (*Router) Use

func (r *Router) Use(m interface{})

Directories

Path Synopsis
This package has some of middleware that built-in gas framework.
This package has some of middleware that built-in gas framework.
This package defined and implemented databases model.
This package defined and implemented databases model.

Jump to

Keyboard shortcuts

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