main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "git.nspix.com/golang/micro"
  6. "git.nspix.com/golang/micro/gateway/cli"
  7. "git.nspix.com/golang/micro/helper/console"
  8. "math/rand"
  9. "time"
  10. )
  11. type (
  12. Request struct {
  13. Name string
  14. Age int
  15. Co float64
  16. }
  17. )
  18. func main() {
  19. svr := micro.Init(
  20. micro.WithName("git.nspix.com/test", "0.0.01"),
  21. micro.WithoutRegister(),
  22. micro.WithHttpDebug(),
  23. micro.WithStats(),
  24. micro.WithCli(),
  25. micro.WithReport(),
  26. micro.WithPort(6567),
  27. )
  28. micro.Handle("getUserList", func(ctx micro.Context) (err error) {
  29. var req Request
  30. if err = ctx.Bind(&req); err != nil {
  31. return err
  32. }
  33. return ctx.Success(req)
  34. }, func(o *micro.HandleOptions) {
  35. o.DisableCli = false
  36. })
  37. micro.Handle("/show/contenxt", func(ctx micro.Context) (err error) {
  38. table := console.NewTable().WithBordered()
  39. table.AddRow("Name", "Age")
  40. for i := 0; i < 10; i++ {
  41. table.AddRow(fmt.Sprintf("zhansan%d", i), i+12)
  42. }
  43. return ctx.Success(table)
  44. }, func(o *micro.HandleOptions) {
  45. o.DisableCli = false
  46. })
  47. go func() {
  48. time.AfterFunc(time.Second*5, func() {
  49. for i := 0; i < 10; i++ {
  50. r := rand.Int63n(10000)
  51. micro.DeferTick(time.Duration(r)*time.Millisecond, func(ctx context.Context) {
  52. fmt.Println(time.Now().Unix())
  53. })
  54. }
  55. })
  56. }()
  57. time.AfterFunc(time.Second*10, func() {
  58. svr.CliServe().Handle("/show/user/:uid", func(ctx *cli.Context) (err error) {
  59. return ctx.Success(ctx.ParamValue("uid"))
  60. })
  61. })
  62. if err := svr.Run(); err != nil {
  63. fmt.Println(err)
  64. }
  65. }