main.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.WithPort(6567),
  26. )
  27. micro.Handle("getUserList", func(ctx micro.Context) (err error) {
  28. var req Request
  29. if err = ctx.Bind(&req); err != nil {
  30. return err
  31. }
  32. return ctx.Success(req)
  33. }, func(o *micro.HandleOptions) {
  34. o.DisableCli = false
  35. })
  36. micro.Handle("/show/contenxt", func(ctx micro.Context) (err error) {
  37. table := console.NewTable().WithBordered()
  38. table.AddRow("Name", "Age")
  39. for i := 0; i < 10; i++ {
  40. table.AddRow(fmt.Sprintf("zhansan%d", i), i+12)
  41. }
  42. return ctx.Success(table)
  43. }, func(o *micro.HandleOptions) {
  44. o.DisableCli = false
  45. })
  46. go func() {
  47. time.AfterFunc(time.Second*5, func() {
  48. for i := 0; i < 10; i++ {
  49. r := rand.Int63n(10000)
  50. micro.DeferTick(time.Duration(r)*time.Millisecond, func(ctx context.Context) {
  51. fmt.Println(time.Now().Unix())
  52. })
  53. }
  54. })
  55. }()
  56. time.AfterFunc(time.Second*10, func() {
  57. svr.CliServe().Handle("/show/user/:uid", func(ctx *cli.Context) (err error) {
  58. return ctx.Success(ctx.ParamValue("uid"))
  59. })
  60. })
  61. if err := svr.Run(); err != nil {
  62. fmt.Println(err)
  63. }
  64. }