example_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package permit_test
  2. import (
  3. "fmt"
  4. "time"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/container/pool"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/permit"
  9. "go-common/library/net/metadata"
  10. "go-common/library/net/netutil/breaker"
  11. xtime "go-common/library/time"
  12. )
  13. // This example create a permit middleware instance and attach to several path,
  14. // it will validate request by specified policy and put extra information into context. e.g., `uid`.
  15. // It provides additional handler functions to provide the identification for your business handler.
  16. func Example() {
  17. a := permit.New(&permit.Config{
  18. DsHTTPClient: &bm.ClientConfig{
  19. App: &bm.App{
  20. Key: "manager-go",
  21. Secret: "949bbb2dd3178252638c2407578bc7ad",
  22. },
  23. Dial: xtime.Duration(time.Second),
  24. Timeout: xtime.Duration(time.Second),
  25. KeepAlive: xtime.Duration(time.Second * 10),
  26. Breaker: &breaker.Config{
  27. Window: xtime.Duration(time.Second),
  28. Sleep: xtime.Duration(time.Millisecond * 100),
  29. Bucket: 10,
  30. Ratio: 0.5,
  31. Request: 100,
  32. },
  33. },
  34. MaHTTPClient: &bm.ClientConfig{
  35. App: &bm.App{
  36. Key: "f6433799dbd88751",
  37. Secret: "36f8ddb1806207fe07013ab6a77a3935",
  38. },
  39. Dial: xtime.Duration(time.Second),
  40. Timeout: xtime.Duration(time.Second),
  41. KeepAlive: xtime.Duration(time.Second * 10),
  42. Breaker: &breaker.Config{
  43. Window: xtime.Duration(time.Second),
  44. Sleep: xtime.Duration(time.Millisecond * 100),
  45. Bucket: 10,
  46. Ratio: 0.5,
  47. Request: 100,
  48. },
  49. },
  50. Session: &permit.SessionConfig{
  51. SessionIDLength: 32,
  52. CookieLifeTime: 1800,
  53. CookieName: "mng-go",
  54. Domain: ".bilibili.co",
  55. Memcache: &memcache.Config{
  56. Config: &pool.Config{
  57. Active: 10,
  58. Idle: 5,
  59. IdleTimeout: xtime.Duration(time.Second * 80),
  60. },
  61. Name: "go-business/permit",
  62. Proto: "tcp",
  63. Addr: "172.16.33.54:11211",
  64. DialTimeout: xtime.Duration(time.Millisecond * 1000),
  65. ReadTimeout: xtime.Duration(time.Millisecond * 1000),
  66. WriteTimeout: xtime.Duration(time.Millisecond * 1000),
  67. },
  68. },
  69. ManagerHost: "http://uat-manager.bilibili.co",
  70. DashboardHost: "http://uat-dashboard-mng.bilibili.co",
  71. DashboardCaller: "manager-go",
  72. })
  73. p := permit.New2(nil)
  74. e := bm.NewServer(nil)
  75. //Check whether the user has logged in
  76. e.GET("/login", a.Verify(), func(c *bm.Context) {
  77. c.JSON("pass", nil)
  78. })
  79. //Check whether the user has logged in,and check th user has the access permisson to the specifed path
  80. e.GET("/tag/del", a.Permit("TAG_DEL"), func(c *bm.Context) {
  81. uid := metadata.Int64(c, metadata.Uid)
  82. username := metadata.String(c, metadata.Username)
  83. c.JSON(fmt.Sprintf("pass uid(%d) username(%s)", uid, username), nil)
  84. })
  85. e.GET("/check/login", p.Verify2(), func(c *bm.Context) {
  86. c.JSON("pass", nil)
  87. })
  88. e.POST("/tag/del", p.Permit2("TAG_DEL"), func(c *bm.Context) {
  89. uid := metadata.Int64(c, metadata.Uid)
  90. username := metadata.String(c, metadata.Username)
  91. c.JSON(fmt.Sprintf("pass uid(%d) username(%s)", uid, username), nil)
  92. })
  93. e.Run(":18080")
  94. }