# Web框架——白泽 ## 1. 概述 白泽,中国古代神话中的瑞兽。能言语,通万物之情,知鬼神之事,“王者有德”才出现,能辟除人间一切邪气。 该Web框架取名为白泽,希望框架能够通开发者之情,辟除一些Bug。 白泽基于六边形架构并结合公司的技术栈进行设计。主要包含framework包和convenient包,framework包是白泽框架的基础包,大家可以基于该包的接口进行开发, convenient包则是在framework包基础上的封装,提供了更加便捷的接口用于构造Web后端应用。 ## 2. Quick Start 下面的代码给出了白泽框架的的简单使用 ```go package main import ( "fmt" "git.sxidc.com/go-framework/baize" "git.sxidc.com/go-framework/baize/convenient/binding" "git.sxidc.com/go-framework/baize/convenient/binding/request" "git.sxidc.com/go-framework/baize/convenient/binding/response" "git.sxidc.com/go-framework/baize/framwork/api" "git.sxidc.com/go-framework/baize/framwork/application" "git.sxidc.com/go-framework/baize/framwork/domain" "git.sxidc.com/go-framework/baize/framwork/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 := app.Binder(application.RouterRoot, "") binding.GetBind(rootBinder, &binding.SimpleBindItem[any]{ Path: "/ping", ResponseFunc: 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 := app.Binder(application.RouterPrefix, "v1") binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[any]{ Path: "/ping", ResponseFunc: 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() } ```