package utils

import (
	"github.com/olivere/elastic/v7"
	"time"
)

var Client *elastic.Client

const (
	ES_URL      = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
	ES_USERNAME = "elastic"                                                               //<2>
	ES_PASSWORD = "hongze@2021"                                                           //<3>
)

func init() {
	//errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
	//file := ""
	//if RunMode == "release" {
	//	//file = `/data/rdlucklog/hongze_cygx/eslog.log`
	//	file = `./rdlucklog/eslog.log`
	//} else {
	//	file = `./rdlucklog/eslog.log`
	//}
	//logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	client, err := elastic.NewClient(
		elastic.SetURL(ES_URL),
		elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
		//elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
		//elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
		elastic.SetSniff(false))
	Client = client
	if err != nil {
		go SendAlarmMsg("ElasticSearch连接失败", 2)
		go SendEmail("ElasticSearch连接失败"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), EmailSendToUsers)
	}
	return
}
