[Preview] 当前持续完善中,不可生产中使用 [Preview]

GoFast Micro-Service Framework

GoFast是一个用Go语言实现的微服务开发框架。他的产生源于目前流行的gin、go-zero、fastify等众多开源框架;同时结合了作者多年的开发实践经验,很多模块的实现方式都是作者首创;当然也免不了不断借鉴社区中优秀的设计理念。我们的目标是简洁高效易上手,在封装大量特性的同时又不失灵活性。希望你能喜欢GoFast。

更多了解:GoFast的实现细节

GoFast的微服务:虽然也提供现成的微服务治理能力,但我们想强调的是,GoFast更专注于帮助框架使用者清晰的开发业务逻辑。大型项目上框架应该弱化微服务治理,将这一部分特性交给istio处理。

Installation

To install GoFast package, you need to install Go and set your Go workspace first.

The first need Go installed (version 1.15+ is required), then you can use the below Go command to install GoFast.

1
$ go get -u github.com/qinchende/gofast

Quick start

1
2
# assume the following codes in example.go file
$ cat example.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"fmt"
	"github.com/qinchende/gofast/fst"
	"github.com/qinchende/gofast/fstx"
	"log"
	"net/http"
	"time"
)

var handler = func(str string) func(c *fst.Context) {
	return func(c *fst.Context) {
		log.Println(str)
	}
}

var handlerRender = func(str string) func(c *fst.Context) {
	return func(c *fst.Context) {
		log.Println(str)
		c.JSON(200, fst.KV{"data": str})
	}
}

func main() {
	app := fst.CreateServer(&fst.AppConfig{
		PrintRouteTrees:        true,
		HandleMethodNotAllowed: true,
		RunMode:                "debug",
		FitMaxReqCount:         1,
		FitMaxReqContentLen:    10 * 1024,
	})

	// 拦截器,微服务治理 ++++++++++++++++++++++++++++++++++++++
	app.RegFits(fstx.AddDefaultFits)
	app.Fit(func(w *fst.GFResponse, r *http.Request) {
		log.Println("app fit before 1")
		w.NextFit(r)
		log.Println("app fit after 1")
	})

	// 根路由
	app.NoRoute(func(ctx *fst.Context) {
		ctx.JSON(http.StatusNotFound, "404-Can't find the path.")
	})
	app.NoMethod(func(ctx *fst.Context) {
		ctx.JSON(http.StatusMethodNotAllowed, "405-Method not allowed.")
	})

	// ++ (用这种方法可以模拟中间件需要上下文变量的场景)
	app.Before(func(ctx *fst.Context) {
		ctx.Set("nowTime", time.Now())
		time.Sleep(3 * time.Second)
	})
	app.After(func(ctx *fst.Context) {
		// 处理后获取消耗时间
		val, exist := ctx.Get("nowTime")
		if exist {
			costTime := time.Since(val.(time.Time)) / time.Millisecond
			fmt.Printf("The request cost %dms", costTime)
		}
	})
	// ++ end

	app.Post("/root", handler("root"))
	app.Before(handler("before root")).After(handler("after root"))

	// 分组路由1
	adm := app.AddGroup("/admin")
	adm.After(handler("after group admin")).Before(handler("before group admin"))

	tst := adm.Get("/chende", handlerRender("handle chende"))
	// 添加路由处理事件
	tst.Before(handler("before tst_url"))
	tst.After(handler("after tst_url"))
	tst.PreSend(handler("preSend tst_url"))
	tst.AfterSend(handler("afterSend tst_url"))

	// 分组路由2
	adm2 := app.AddGroup("/admin2").Before(handler("before admin2"))
	adm2.Get("/zht", handler("zht")).After(handler("after zht"))

	adm22 := adm2.AddGroup("/group2").Before(handler("before group2"))
	adm22.Get("/lmx", handler("lmx")).Before(handler("before lmx"))

	// 应用级事件
	app.OnReady(func(fast *fst.GoFast) {
		log.Println("App OnReady Call.")
		log.Printf("Listening and serving HTTP on %s\n", "127.0.0.1:8099")
	})
	app.OnClose(func(fast *fst.GoFast) {
		log.Println("App OnClose Call.")
	})
	// 开始监听接收请求
	_ = app.Listen("127.0.0.1:8099")
}
1
2
# run example.go and visit website 127.0.0.1:8099 on browser
$ go run example.go

在控制台启动后台Web服务器之后,你会看到底层的路由树构造结果:

[GoFast-debug] POST   /root                     --> main.glob..func1.1 (1 handlers)
[GoFast-debug] GET    /admin/chende             --> main.glob..func2.1 (1 handlers)
[GoFast-debug] GET    /admin2/zht               --> main.glob..func1.1 (1 handlers)
[GoFast-debug] GET    /admin2/group2/lmx        --> main.glob..func1.1 (1 handlers)
++++++++++The route tree:

(GET)
└── /admin                                                       [false-/2]
    ├── /chende                                                  [true-]
    └── 2/                                                       [false-zg]
        ├── zht                                                  [true-]
        └── group2/lmx                                           [true-]
(POST)
└── /root                                                        [true-]

++++++++++THE END.
2021/02/20 02:56:00 App OnReady Call.
2021/02/20 02:56:00 Listening and serving HTTP on 127.0.0.1:8099

浏览器输入网址访问地址:127.0.0.1:8099/admin/chende,日志会输出:

2021/02/20 02:57:17 app fit before 1
2021/02/20 02:57:20 before root
2021/02/20 02:57:20 before group admin
2021/02/20 02:57:20 before tst_url
2021/02/20 02:57:20 handle chende
2021/02/20 02:57:20 preSend tst_url
2021/02/20 02:57:20 afterSend tst_url
2021/02/20 02:57:20 after tst_url
2021/02/20 02:57:20 after group admin
2021/02/20 02:57:20 after root
2021/02/20 02:57:20 app fit after 1
The request cost 3000ms
[GET] /admin/chende (127.0.0.1/02-20 02:57:20) 200/24 [3000]
  B:  C: 
  P: 
  R: 
  E: 

Core feature

Like gin feature

GoFast目前复用了Gin的很多特性,除特别说明之外,使用方式一样。

Server Handlers

应用启动之后,在开始监听端口之后调用OnReady事件,应用关闭退出之前调用OnClose事件

1
2
3
4
5
6
7
8
9
app.OnReady(func(fast *fst.GoFast) {
	log.Println("App OnReady Call.")
	log.Printf("Listening and serving HTTP on %s\n", "127.0.0.1:8099")
})

app.OnClose(func(fast *fst.GoFast) {
	log.Println("App OnClose Call.")
})

Router Handlers

分组或路由项事件是一样的,现在支持下面四个,以后慢慢扩展和调整

1
2
3
4
tst.Before(handler("before tst_url"))
tst.After(handler("after tst_url"))
tst.PreSend(handler("preSend tst_url"))
tst.AfterSend(handler("afterSend tst_url"))

benchmark

License

This project is licensed under the terms of the MIT license.

(其它介绍陆续补充…)