From 89b31ebf8853d016befc8cc27c0b85d2de4bef27 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Fri, 13 Mar 2020 17:40:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0elk=20DB=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 11 +++++++++++ db/elasticEngine.go | 33 +++++++++++++++++++++++++++++++++ main.go | 7 ++++++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 db/elasticEngine.go diff --git a/config/config.go b/config/config.go index 299e79f..a792bfa 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,7 @@ type ConfAPI struct { Logs LogConfig `yaml:"logs"` // 日志 Redis1 EntityRedis `yaml:"redis1"` Mysql MysqlConfig `yaml:"mysql"` // 认证配置 + ElasticSerach ElasticSearch `yaml:"elasticsearch"` Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置 MongoConf MongoConfig `yaml:"mongo"` CaCert string `yaml:"ca_cert"` @@ -26,6 +27,9 @@ type ConfAPI struct { ClientKey string `yaml:"client_key"'` init bool } +type ElasticSearch struct{ + Address string `yaml:"string"` +} type EntityRedis struct { Addr string `yaml:"addr"` @@ -132,3 +136,10 @@ func GetLogConfig() *LogConfig { } } +func GetElkConfig() *ElasticSearch{ + if gConf.init{ + return &gConf.ElasticSerach + }else { + return nil + } +} \ No newline at end of file diff --git a/db/elasticEngine.go b/db/elasticEngine.go new file mode 100644 index 0000000..87f0046 --- /dev/null +++ b/db/elasticEngine.go @@ -0,0 +1,33 @@ +package db + +import ( + "background/config" + "background/logs" + "gopkg.in/olivere/elastic.v3" +) + +type ElkEngine struct { + cli *elastic.Client +} + +var gElkEngine ElkEngine + +func InitELK() { + var e error + elkconf := config.GetElkConfig() + gElkEngine.cli,e = elastic.NewClient( + elastic.SetURL(elkconf.Address), + // Must turn off sniff in docker + elastic.SetSniff(false),) + if nil != e{ + logs.Error(e.Error()) + gElkEngine.cli = nil + + } +} + +func (*ElkEngine)Search() (error) { + + return nil +} + diff --git a/main.go b/main.go index ebed65a..5a4f995 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,12 @@ func InitLogs() { } func CORSMiddleware(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") - c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080,https://www.testingcloud.club/,https://testingcloud.club/") + if config.ApiConfig().RunMode == "release"{ + c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club") + + }else{ + c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080") + } c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With," +