Web框架——白泽
1. 概述
白泽,中国古代神话中的瑞兽。能言语,通万物之情,知鬼神之事,“王者有德”才出现,能辟除人间一切邪气。
该Web框架取名为白泽,希望框架能够通开发者之情,辟除一些Bug。
白泽基于六边形架构并结合方是科技的技术栈进行设计。主要包含framework包和convenient包,framework包是白泽框架的基础包,包含核心框架,网关框架和API Binding机制,大家可以基于该包的接口进行开发, convenient包则是在framework包基础上的封装,提供了更加便捷的接口用于构造Web后端应用。
2. Quick Start
下面的代码给出了白泽框架的的简单使用,使用白泽,一般直接利用binding进行开发即可,封装了最底层的AddRoute方式,一般仅在需要动态引出API时,才使用AddRoute方式。
package main
import (
"fmt"
"git.sxidc.com/go-framework/baize"
"git.sxidc.com/go-framework/baize/framework/binding"
"git.sxidc.com/go-framework/baize/framework/core/api"
"git.sxidc.com/go-framework/baize/framework/core/api/request"
"git.sxidc.com/go-framework/baize/framework/core/api/response"
"git.sxidc.com/go-framework/baize/framework/core/application"
"git.sxidc.com/go-framework/baize/framework/core/domain"
"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
DEATH "github.com/vrecan/death"
"net/http"
"syscall"
)
func main() {
app := baize.NewApplication(application.Config{
ApiConfig: application.ApiConfig{
UrlPrefix: "test",
Port: "10100",
},
})
app.Api().
RootRouter().
AddMiddlewares(func(c *api.Context) {
fmt.Println("Global Before1")
c.Next()
fmt.Println("Global After1")
}, func(c *api.Context) {
fmt.Println("Global Before2")
c.Next()
fmt.Println("Global After2")
})
rootBinder := binding.NewBinder(app.ChooseRouter(api.RouterRoot, ""), app.Infrastructure())
binding.GetBind(rootBinder, &binding.SimpleBindItem[any]{
Path: "/ping",
SendResponseFunc: response.NoResponse,
ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
c.String(http.StatusOK, "pong")
return nil, nil
},
}, func(c *api.Context) {
fmt.Println("Root Route Before1")
c.Next()
fmt.Println("Root Route After1")
}, func(c *api.Context) {
fmt.Println("Root Route Before2")
c.Next()
fmt.Println("Root Route After2")
})
app.Api().
PrefixRouter().
RegisterVersionedRouter("v1", func(c *api.Context) {
fmt.Println("Global Before1")
c.Next()
fmt.Println("Global After1")
}, func(c *api.Context) {
fmt.Println("Global Before2")
c.Next()
fmt.Println("Global After2")
})
prefixRootBinder := binding.NewBinder(app.ChooseRouter(api.RouterPrefix, "v1"), app.Infrastructure())
binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[any]{
Path: "/ping",
SendResponseFunc: response.NoResponse,
ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
c.String(http.StatusOK, "pong")
return nil, nil
},
}, func(c *api.Context) {
fmt.Println("Versioned Route Before1")
c.Next()
fmt.Println("Versioned Route After1")
}, func(c *api.Context) {
fmt.Println("Versioned Route Before2")
c.Next()
fmt.Println("Versioned Route After2")
})
go func() {
if err := app.Start(); err != nil {
panic(err)
}
}()
defer func() {
if err := app.Finish(); err != nil {
panic(err)
}
}()
death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
_ = death.WaitForDeath()
}
3. application
白泽框架整体遵循六边形架构,如下图所示:
整体围绕Application展开,Application以Domain作为核心并协调API和基础设施构建业务逻辑。可以通过application.App的接口获取到响应的基础设施和API对象,可以使用基础设施或构建API。
4. api
api包含了定义API需要用到的相关接口和结构,request包含了请求参数和请求参数绑定的相关函数,response包含了常用的响应结构和响应函数.
Api结构内包含了Router接口,这里Router接口有两种子类型:RootRouter和PrefixRouter。RootRouter是最基础的根路由,指向的路径是http://ip:port,PrefixRouter是添加前缀的路由,指向的路径是http://ip:port/prefix,基于两种路由都可以进一步定义版本限制的路由,如http://ip:port/v1或http://ip:port/prefix/v1。可以通过Api结构对路由进行定义和操作。
5. infrastructure
基础设施是构建应用需要用到的基础设施,基础设施主要包括日志,数据库,缓存等外部第三方被调系统或应用接口,是系统可以使用的基础设施的集合。
6. binding
binding是一种机制,围绕应用将API,基础设施以及领域进行绑定,从而构造领域相关的API的一种手段。