Skip to content
/ gas Public
forked from mingderwang/gas

Gas is a web framework writing in go

License

Notifications You must be signed in to change notification settings

andeya/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, 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.

Start using it

  1. Download and install it:
$ go get gopkg.in/gas.v0
  1. Import it in your code:
import "gopkg.in/gas.v0"

How to use

Micro service

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,
  })
}

see go-gas/example/micro_demo

Large project architecture

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)

file structure

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

main.go

1. import
import (
    "Your_Project_Name/routers"
    "github.com/go-gas/gas"
    "github.com/go-gas/gas/middleware"
)
2. New or Default

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")
3. Register Routes
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)

}
4. Using gas.Context
Cookie

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")
Session

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.

5. 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()

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.

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

  • 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

About

Gas is a web framework writing in go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 98.0%
  • Other 2.0%