example_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package verify_test
  2. import (
  3. "fmt"
  4. "time"
  5. bm "go-common/library/net/http/blademaster"
  6. "go-common/library/net/http/blademaster/middleware/verify"
  7. "go-common/library/net/metadata"
  8. xtime "go-common/library/time"
  9. )
  10. // This example create a identify middleware instance and attach to several path,
  11. // it will validate request by specified policy and put extra information into context. e.g., `mid`.
  12. // It provides additional handler functions to provide the identification for your business handler.
  13. func Example() {
  14. idt := verify.New(&verify.Config{
  15. OpenServiceHost: "http://uat-open.bilibili.co",
  16. HTTPClient: &bm.ClientConfig{
  17. App: &bm.App{
  18. Key: "53e2fa226f5ad348",
  19. Secret: "3cf6bd1b0ff671021da5f424fea4b04a",
  20. },
  21. Dial: xtime.Duration(time.Second),
  22. Timeout: xtime.Duration(time.Second),
  23. KeepAlive: xtime.Duration(time.Second * 10),
  24. },
  25. })
  26. e := bm.Default()
  27. // mark `/verify` path as Verify policy
  28. e.GET("/verify", idt.Verify, func(c *bm.Context) {
  29. c.JSON("pass", nil)
  30. })
  31. // mark `/verify` path as VerifyUser policy
  32. e.GET("/verifyUser", idt.VerifyUser, func(c *bm.Context) {
  33. mid := metadata.Int64(c, metadata.Mid)
  34. c.JSON(fmt.Sprintf("%d", mid), nil)
  35. })
  36. e.Run(":18080")
  37. }