package core

import (
	"fmt"
	"github.com/etlibgo/et"

	"github.com/gin-gonic/gin"
)

var sApp = struct {
	HttpServer *gin.Engine
	Running    bool
}{
	Running: false,
}

func AppInit(jsonCfg *et.ExJsonVal) {
	CfgInit(jsonCfg)

	sApp.HttpServer = gin.Default()
}

func AppGetHttpServer() *gin.Engine {
	return sApp.HttpServer
}

func AppStart() {

	if sApp.Running {
		return
	}

	sApp.Running = true

	sApp.HttpServer.GET("/ping", func(c *gin.Context) { // /ping是路由
		//输出json结果给调用方
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	go sApp.HttpServer.Run(":8081") // 可以从本地的127.0.0.1:8081访问，不填的话，默认是8080端口

	fmt.Println("foot AppStart started")
}

func AppStop() {
	if !sApp.Running {
		return
	}
	sApp.Running = false
}

func AppIsRunning() bool {
	return sApp.Running
}
