diff --git a/main.go b/main.go index 0a8078c..1cb4fbb 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,6 @@ import ( "encoding/gob" "errors" "flag" - "fmt" - "git.trj.tw/golang/mtfosbot/module/es" "log" "os" "path" @@ -45,17 +43,6 @@ func main() { log.Fatal(err) } - err = es.NewClient() - if err != nil { - log.Fatal(err) - } - err = es.PutLog() - if err != nil { - log.Fatal(err) - } - fmt.Println("run done") - return - // connect to database db, err := model.NewDB() if err != nil { diff --git a/module/es/es.go b/module/es/es.go index 68fd6bc..25cc4e9 100644 --- a/module/es/es.go +++ b/module/es/es.go @@ -1,30 +1,34 @@ package es import ( - "context" - "fmt" - "git.trj.tw/golang/mtfosbot/module/config" - "github.com/olivere/elastic" + "context" + "fmt" + + "git.trj.tw/golang/mtfosbot/module/config" + "github.com/olivere/elastic" ) var client *elastic.Client -func NewClient () (err error) { - conf := config.GetConf() - client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host)) - fmt.Println("host ", conf.Elasticsearch.Host) - if err != nil { - fmt.Println(err) - return - } - return +// NewClient - +func NewClient() (err error) { + conf := config.GetConf() + client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host)) + fmt.Println("host ", conf.Elasticsearch.Host) + if err != nil { + fmt.Println(err) + return + } + return } -func PutLog () (err error) { - conf := config.GetConf() - ctx := context.Background() - _, err = client.Index().Index(conf.Elasticsearch.Index).Type("type").BodyJson(map[string]interface{}{ - "key1": "value1", - }).Do(ctx) - return -} \ No newline at end of file +// PutLog - +func PutLog(t string, body map[string]interface{}) (err error) { + if len(t) == 0 || body == nil { + return + } + conf := config.GetConf() + ctx := context.Background() + _, err = client.Index().Index(conf.Elasticsearch.Index).Type(t).BodyJson(body).Do(ctx) + return +}