Skip to content

CarterTsai/gas

 
 

Repository files navigation

Gas

go-gas

Build Status codecov Go Report Card Join the chat at https://gitter.im/go-gas/gas

Gas aims to be a high performance, full-featured, easy to use, and quick develop backend web application framework in Golang.

Features

  • Router (based on fasthttprouter package)
  • Easy to use golang template engine. (will include another template engine)
  • Context (easy to manage the request and response)
  • Middleware (Global and specify routing path middleware support)
  • Log package
  • Read config from a yaml file gas-config
  • Database model (developing, based on go-gas/SQLBuilder)

other features are highly active development

and you can see example at gas-example.

Install

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

Run demo

$ git clone https://github.com/go-gas/example.git && cd 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()
g.LoadConfig("your/config/path")

If you don't want to load any config, you might be know that gas have a default config with

var defaultConfig = map[interface{}]interface{}{
	"Mode":       "DEV",
	"ListenAddr": "localhost",
	"ListenPort": "8080",
	"PubDir":     "public",
	"Db": map[interface{}]interface{}{
		"SqlDriver": "MySQL",
		"Hostname":  "localhost",
		"Port":      "3306",
		"Username":  "root",
		"Password":  "",
		"Charset":   "utf8",
	},
}

or you can give config path when new gas app

g := gas.New("config/path1", "config/path2")

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

Global middleware

If you want a middleware to be run during every request to your application, you can use Router.Use function to register your middleware.

g.Router.Use(middleware.LogMiddleware)
Assigning middleware to Route

If you want to assign middleware to specific routes, you can set your middlewares after set route function like:

r.Get("/", controllers.IndexPage, myMiddleware1, myMiddleware2)
And you can write your own middleware function
func LogMiddleware(next gas.GasHandler) gas.GasHandler {
    return func (c *gas.Context) error  {

       // do something before next handler

       err := next(c)

       // do something after next handler

       return err
    }
}

or

func MyMiddleware2 (ctx *gas.Context) error {
  // do something
}

The final step

Run and listen your web application with default 8080 port.

g.Run()

or you can give listen address and another port.

g.Run(":8089")

or serving HTTPS (secure) requests.

g.RunTLS(":8080", "CertFile", "CertKey")

but I recommend setting listen address in config files.

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

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)

About

Gas is a web framework writing in go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 98.0%
  • Shell 1.1%
  • Other 0.9%