main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize"
  5. "git.sxidc.com/go-framework/baize/framework/binding"
  6. "git.sxidc.com/go-framework/baize/framework/core/api"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  8. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  9. "git.sxidc.com/go-framework/baize/framework/core/application"
  10. "git.sxidc.com/go-framework/baize/framework/core/domain"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  12. DEATH "github.com/vrecan/death"
  13. "net/http"
  14. "syscall"
  15. )
  16. func main() {
  17. app := baize.NewApplication(application.Config{
  18. ApiConfig: application.ApiConfig{
  19. UrlPrefix: "test",
  20. Port: "10100",
  21. },
  22. })
  23. app.Api().
  24. RootRouter().
  25. AddMiddlewares(func(c *api.Context) {
  26. fmt.Println("Global Before1")
  27. c.Next()
  28. fmt.Println("Global After1")
  29. }, func(c *api.Context) {
  30. fmt.Println("Global Before2")
  31. c.Next()
  32. fmt.Println("Global After2")
  33. })
  34. rootBinder := binding.NewBinder(app.ChooseRouter(api.RouterRoot, ""), app.Infrastructure())
  35. binding.GetBind(rootBinder, &binding.SimpleBindItem[any]{
  36. Path: "/ping",
  37. SendResponseFunc: response.NoResponse,
  38. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  39. c.String(http.StatusOK, "pong")
  40. return nil, nil
  41. },
  42. }, func(c *api.Context, i *infrastructure.Infrastructure) {
  43. fmt.Println("Root Route Before1")
  44. c.Next()
  45. fmt.Println("Root Route After1")
  46. }, func(c *api.Context, i *infrastructure.Infrastructure) {
  47. fmt.Println("Root Route Before2")
  48. c.Next()
  49. fmt.Println("Root Route After2")
  50. })
  51. app.Api().
  52. PrefixRouter().
  53. RegisterVersionedRouter("v1", func(c *api.Context) {
  54. fmt.Println("Global Before1")
  55. c.Next()
  56. fmt.Println("Global After1")
  57. }, func(c *api.Context) {
  58. fmt.Println("Global Before2")
  59. c.Next()
  60. fmt.Println("Global After2")
  61. })
  62. prefixRootBinder := binding.NewBinder(app.ChooseRouter(api.RouterPrefix, "v1"), app.Infrastructure())
  63. binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[any]{
  64. Path: "/ping",
  65. SendResponseFunc: response.NoResponse,
  66. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  67. c.String(http.StatusOK, "pong")
  68. return nil, nil
  69. },
  70. }, func(c *api.Context, i *infrastructure.Infrastructure) {
  71. fmt.Println("Versioned Route Before1")
  72. c.Next()
  73. fmt.Println("Versioned Route After1")
  74. }, func(c *api.Context, i *infrastructure.Infrastructure) {
  75. fmt.Println("Versioned Route Before2")
  76. c.Next()
  77. fmt.Println("Versioned Route After2")
  78. })
  79. go func() {
  80. if err := app.Start(); err != nil {
  81. panic(err)
  82. }
  83. }()
  84. defer func() {
  85. if err := app.Finish(); err != nil {
  86. panic(err)
  87. }
  88. }()
  89. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  90. _ = death.WaitForDeath()
  91. }