add es module
This commit is contained in:
parent
ac8284b7e3
commit
e0323e964f
13
main.go
13
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 {
|
||||
|
@ -3,12 +3,14 @@ package es
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.trj.tw/golang/mtfosbot/module/config"
|
||||
"github.com/olivere/elastic"
|
||||
)
|
||||
|
||||
var client *elastic.Client
|
||||
|
||||
// NewClient -
|
||||
func NewClient() (err error) {
|
||||
conf := config.GetConf()
|
||||
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host))
|
||||
@ -20,11 +22,13 @@ func NewClient () (err error) {
|
||||
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)
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user