Gas aims to be a high performance, full-featured, easy to use, and quick develop backend web application framework in Golang.
- Router (based on fasthttprouter package)
- Easy to use golang template engine. (will include another template engine)
- Context (easy to manage the request, response and session)
- Middleware (Global and specify routing path middleware support)
- Logger package gas-logger
- Read config from a yaml file gas-config
- Database model (developing, based on go-gas/SQLBuilder)
- Support listen HTTP/HTTPS and UNIX addr.
other features are highly active development
and you can see example at gas-example.
- Download and install it:
$ go get gopkg.in/gas.v0
- Import it in your code:
import "gopkg.in/gas.v0"
If you want to create a micro service, you can write all of things in one package, for example:
|-- $GOPATH
| |-- src
| |--Your_Project_Name
| |-- main.go
| |-- config.yaml
main.go
package main
import (
"github.com/go-gas/gas"
"net/http"
)
func main() {
g := gas.Default("config.yaml")
g.Router.Get("/", Index)
g.Router.Get("/user", GetUser)
g.Run()
}
func Index(ctx *gas.Context) error {
return ctx.HTML(http.StatusOK, "Micro service! <br> <a href=\"/user\">json response example</a>")
}
func GetUser(ctx *gas.Context) error {
return ctx.JSON(http.StatusOK, gas.H{
"name": "John",
"age": 32,
})
}
Write all code in one file is so dirty and hard to maintain when you build a large site include many controller, middlerware... and so on.
So, maybe we can seperate them in many packages. (directorys)
|-- $GOPATH
| |-- src
| |--Your_Project_Name
| |-- config
| |-- default.yaml
| |-- controllers
| |-- default.go
| |-- log
| |-- models
| |-- routers
| |-- routers.go
| |-- static
| |-- views
| |-- main.go
import (
"Your_Project_Name/routers"
"github.com/go-gas/gas"
"github.com/go-gas/gas/middleware"
)
Default
returns an Engine instance with the Logger
middleware already attached.
g := gas.Default()
g.LoadConfig("your/config/path")
New
returns a new blank Engine instance without any middleware attached.
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")
routers.RegistRout(g.Router)
Then in your routers/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)
}
gas context provides set cookie with string or []bytes, you don't need to do type assertion.
ctx.SetCookie("key", "value")
ctx.SetCookieBytes([]byte("key"), []byte("value"))
and you can set cookie detail using gas.CookieSettings struct
type CookieSettings struct {
PathByte []byte
PathString string
DomainByte []byte
DomainString string
Expired int
HttpOnly bool
}
for example:
cfg := &CookieSettings{
PathByte: []byte("/somePath"),
DomainByte: []byte("example.com"),
Expired: 3600,
HttpOnly: true,
}
ctx.SetCookieByConfig(cfg, "key", "value")
First, you must import provider package before call SessionStart.
import _ "github.com/go-gas/sessions/memory"
And the provider package will Register to session package automaticly.
Then call SessionStart, and use Get and Set to operate your session.
s := ctx.SessionStart()
s.Set("key", "value")
d := s.Get("value").(string)
The Get function will return interface{} type, you must know the data type and do type assertion your self.
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)
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
}
Run and listen your web application with default 8080
port.
g.Run()
you can give listen address and another port.
g.Run(":8089")
Serving HTTPS (secure) requests.
g.RunTLS(":8080", "CertFile", "CertKey")
HTTP requests from the given UNIX addr.
g.RunUNIX("/tmp/gas.sock", 0644)
but I recommend setting listen address in config files.
Using go-web-framework-benchmark to benchmark with another web fframework.
- Router
- Group Routing
- Models
- Model fields mapping
- ORM
- Relation mapping
- Transaction
- QueryBuilder
- Session
- Filesystem
- Database
- Redis
- Memcache
- In memory
- Cache
- Memory
- File
- Redis
- Memcache
- i18n
- HTTPS
- Command line tools
- Form handler
- Security check features(csrf, xss filter...etc)
- Doker Image