package main

import (
	"bp.jydev.jianyu360.cn/BaseService/gateway/core/node"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	//node.NewNode().Register("usermodule", "8888", "http://127.0.0.1")

	var serverCode string = "gatewayDemo"
	var serverPort int = 8099

	server := g.Server()
	server.SetPort(serverPort)
	server.BindHandler("/gatewayDemo/{page}", func(r *ghttp.Request) {
		r.Response.Write(map[string]interface{}{
			"code": 1,
			"func": r.Get("page"),
			"param": map[string]interface{}{
				"name": r.Get("name").String(),
				"age":  r.Get("age").Int64(),
				"eId":  r.Get("entId").Int64(),
				"uId":  r.Get("userId").String(),
			},
		})
	})

	//向网关注册表注册服务。此步骤应在服务启动后注册。服务有重试机制不受影响。
	//NewNode可传入etcd节点，默认http://127.0.0.1:2379
	closeNotify, err := node.NewNode().Register(serverCode, fmt.Sprintf("%d", serverPort))
	if err != nil {
		panic(err)
	}

	go func() {
		server.Run()
	}()

	//阻塞主进程,接受退出消息。关闭服务，从注册表中删除。
	//kill -9 信号接受不到，会有2～3秒延迟。服务有重试机制不受影响。
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	closeNotify()
}
