123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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, i *infrastructure.Infrastructure) {
- fmt.Println("Root Route Before1")
- c.Next()
- fmt.Println("Root Route After1")
- }, func(c *api.Context, i *infrastructure.Infrastructure) {
- 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, i *infrastructure.Infrastructure) {
- fmt.Println("Versioned Route Before1")
- c.Next()
- fmt.Println("Versioned Route After1")
- }, func(c *api.Context, i *infrastructure.Infrastructure) {
- 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()
- }
|