add config to elasiticsearch database type

This commit is contained in:
Martin Geno 2017-04-21 00:45:45 +02:00
parent aefa2a3e33
commit a2dac0365e
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
1 changed files with 19 additions and 18 deletions

View File

@ -6,16 +6,14 @@ package logging
* - example for other developers for new databases * - example for other developers for new databases
*/ */
import ( import (
"context"
"log" "log"
"time" "time"
"context"
elastic "gopkg.in/olivere/elastic.v5"
"github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime" "github.com/FreifunkBremen/yanic/runtime"
"gopkg.in/olivere/elastic.v5"
) )
type Connection struct { type Connection struct {
@ -29,8 +27,20 @@ type Config map[string]interface{}
func (c Config) Enable() bool { func (c Config) Enable() bool {
return c["enable"].(bool) return c["enable"].(bool)
} }
func (c Config) Path() string { func (c Config) Host() string {
return c["path"].(string) return c["host"].(string)
}
func (c Config) Username() string {
return c["username"].(string)
}
func (c Config) Password() string {
return c["password"].(string)
}
func (c Config) IndexPrefix() string {
return c["index_prefix"].(string)
}
func (c Config) UpdateTemplates() bool {
return c["update_templates"].(bool)
} }
func init() { func init() {
@ -44,20 +54,16 @@ func Connect(configuration interface{}) (database.Connection, error) {
return nil, nil return nil, nil
} }
// Create a client // Create a client
client, err := elastic.NewClient( client, err := elastic.NewClient(
elastic.SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201"), elastic.SetURL(config.Host()),
elastic.SetBasicAuth("user", "secret")) elastic.SetBasicAuth(config.Username(), config.Password()))
if err != nil { if err != nil {
// Handle error // Handle error
panic(err) panic(err)
} }
return &Connection{config: config, client: client}, nil return &Connection{config: config, client: client}, nil
} }
@ -71,14 +77,11 @@ func (conn *Connection) InsertNode(node *runtime.Node) {
Refresh("true"). Refresh("true").
Do(context.Background()) Do(context.Background())
if err != nil { if err != nil {
// Handle error // Handle error
panic(err) panic(err)
} }
} }
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) { func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
@ -91,13 +94,11 @@ func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time
Refresh("true"). Refresh("true").
Do(context.Background()) Do(context.Background())
if err != nil { if err != nil {
// Handle error // Handle error
panic(err) panic(err)
} }
} }
func (conn *Connection) PruneNodes(deleteAfter time.Duration) { func (conn *Connection) PruneNodes(deleteAfter time.Duration) {