main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize"
  5. "git.sxidc.com/go-framework/baize/api"
  6. "git.sxidc.com/go-framework/baize/application"
  7. DEATH "github.com/vrecan/death"
  8. "net/http"
  9. "syscall"
  10. )
  11. func main() {
  12. app := baize.NewApplication(application.Config{
  13. ApiConfig: application.ApiConfig{
  14. UrlPrefix: "test",
  15. Port: "10000",
  16. },
  17. })
  18. app.Api().
  19. RootRouter().
  20. AddMiddlewares(func(c *api.Context) {
  21. fmt.Println("Global Before1")
  22. c.Next()
  23. fmt.Println("Global After1")
  24. }, func(c *api.Context) {
  25. fmt.Println("Global Before2")
  26. c.Next()
  27. fmt.Println("Global After2")
  28. }).
  29. AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
  30. c.String(http.StatusOK, "pong")
  31. }, func(c *api.Context) {
  32. fmt.Println("Root Route Before1")
  33. c.Next()
  34. fmt.Println("Root Route After1")
  35. }, func(c *api.Context) {
  36. fmt.Println("Root Route Before2")
  37. c.Next()
  38. fmt.Println("Root Route After2")
  39. })
  40. app.Api().
  41. PrefixRouter().
  42. RegisterVersionedRouter("v1", func(c *api.Context) {
  43. fmt.Println("Global Before1")
  44. c.Next()
  45. fmt.Println("Global After1")
  46. }, func(c *api.Context) {
  47. fmt.Println("Global Before2")
  48. c.Next()
  49. fmt.Println("Global After2")
  50. }).
  51. AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
  52. c.String(http.StatusOK, "pong")
  53. }, func(c *api.Context) {
  54. fmt.Println("Versioned Route Before1")
  55. c.Next()
  56. fmt.Println("Versioned Route After1")
  57. }, func(c *api.Context) {
  58. fmt.Println("Versioned Route Before2")
  59. c.Next()
  60. fmt.Println("Versioned Route After2")
  61. })
  62. go func() {
  63. if err := app.Start(); err != nil {
  64. panic(err)
  65. }
  66. }()
  67. defer func() {
  68. if err := app.Finish(); err != nil {
  69. panic(err)
  70. }
  71. }()
  72. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  73. _ = death.WaitForDeath()
  74. }