package main

import (
	"context"
	"fmt"
	"git.nspix.com/golang/micro"
	"git.nspix.com/golang/micro/helper/console"
	"math/rand"
	"time"
)

type (
	Request struct {
		Name string
		Age  int
		Co   float64
	}
)

func main() {

	svr := micro.Init(
		micro.WithName("git.nspix.com/test", "0.0.01"),
		micro.WithoutRegister(),
		micro.WithHttpDebug(),
		micro.WithStats(),
		micro.WithCli(),
		micro.WithPort(6567),
	)

	micro.Handle("getUserList", func(ctx micro.Context) (err error) {
		var req Request
		if err = ctx.Bind(&req); err != nil {
			return err
		}
		return ctx.Success(req)
	}, func(o *micro.HandleOptions) {
		o.DisableCli = false
	})

	micro.Handle("/show/contenxt", func(ctx micro.Context) (err error) {
		table := console.NewTable().WithBordered()
		table.AddRow("Name", "Age")
		for i := 0; i < 10; i++ {
			table.AddRow(fmt.Sprintf("zhansan%d", i), i+12)
		}
		return ctx.Success(table)
	}, func(o *micro.HandleOptions) {
		o.DisableCli = false
	})

	go func() {
		time.AfterFunc(time.Second*5, func() {
			for i := 0; i < 10; i++ {
				r := rand.Int63n(10000)
				micro.DeferTick(time.Duration(r)*time.Millisecond, func(ctx context.Context) {
					fmt.Println(time.Now().Unix())
				})
			}
		})
	}()

	if err := svr.Run(); err != nil {
		fmt.Println(err)
	}
}
