/
config_handler.go
210 lines (172 loc) · 5.76 KB
/
config_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* _ _
*__ _____ __ ___ ___ __ _| |_ ___
*\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
* \ V V / __/ (_| |\ V /| | (_| | || __/
* \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
*
* Copyright © 2016 - 2019 Weaviate. All rights reserved.
* LICENSE: https://github.com/creativesoftwarefdn/weaviate/blob/develop/LICENSE.md
* DESIGN & CONCEPT: Bob van Luijt (@bobvanluijt)
* CONTACT: hello@creativesoftwarefdn.org
*/
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"github.com/go-openapi/swag"
"github.com/creativesoftwarefdn/weaviate/messages"
)
// DefaultConfigFile is the default file when no config file is provided
const DefaultConfigFile string = "./weaviate.conf.json"
// DefaultEnvironment is the default env when no env is provided
const DefaultEnvironment string = "development"
// Flags are input options
type Flags struct {
ConfigSection string `long:"config" description:"the section inside the config file that has to be used"`
ConfigFile string `long:"config-file" description:"path to config file (default: ./weaviate.conf.json)"`
}
// File gives the outline of the config file
type File struct {
Environments []Environment `json:"environments"`
}
// Environment outline of the environment inside the config file
type Environment struct {
Name string `json:"name"`
Database Database `json:"database"`
Schemas Schemas `json:"schemas"`
Broker Broker `json:"broker"`
Network *Network `json:"network"`
Limit int64 `json:"limit"`
Debug bool `json:"debug"`
Development Development `json:"development"`
Contextionary Contextionary `json:"contextionary"`
ConfigStore ConfigStore `json:"configuration_storage"`
}
type Contextionary struct {
KNNFile string `json:"knn_file"`
IDXFile string `json:"idx_file"`
failOnGerund bool `json:"fail_ongerund"` // is false by default.
}
type Network struct {
GenesisURL string `json:"genesis_url"`
PublicURL string `json:"public_url"`
PeerName string `json:"peer_name"`
}
type ConfigStore struct {
Type string `json:"type"`
URL string `json:"url"`
}
// Broker checks if broker details are set
type Broker struct {
Host string `json:"host"`
Port int32 `json:"port"`
}
// Database is the outline of the database
type Database struct {
Name string `json:"name"`
DatabaseConfig interface{} `json:"database_config"`
}
// Schemas contains the schema for 'things' and for 'actions'
type Schemas struct {
Thing string `json:"thing"`
Action string `json:"action"`
}
// Development is the outline of (temporary) config variables
// Note: the purpose is that these variables will be moved somewhere else in time
type Development struct {
ExternalInstances []Instance `json:"external_instances"`
}
// Instance is the outline for an external instance whereto crossreferences can be resolved
type Instance struct {
URL string `json:"url"`
APIKey string `json:"api_key"`
APIToken string `json:"api_token"`
}
// GetConfigOptionGroup creates a option group for swagger
func GetConfigOptionGroup() *swag.CommandLineOptionsGroup {
commandLineOptionsGroup := swag.CommandLineOptionsGroup{
ShortDescription: "Connector config & MQTT config",
LongDescription: "",
Options: &Flags{},
}
return &commandLineOptionsGroup
}
// WeaviateConfig represents the used schema's
type WeaviateConfig struct {
Environment Environment
Hostname string
Scheme string
}
// GetHostAddress from config locations
func (f *WeaviateConfig) GetHostAddress() string {
return fmt.Sprintf("%s://%s", f.Scheme, f.Hostname)
}
// LoadConfig from config locations
func (f *WeaviateConfig) LoadConfig(flags *swag.CommandLineOptionsGroup, m *messages.Messaging) error {
// Get command line flags
configEnvironment := flags.Options.(*Flags).ConfigSection
configFileName := flags.Options.(*Flags).ConfigFile
// Set default if not given
if configFileName == "" {
configFileName = DefaultConfigFile
m.InfoMessage("Using default config file location '" + DefaultConfigFile + "'.")
}
// Read config file
file, err := ioutil.ReadFile(configFileName)
if err != nil {
return errors.New("config file '" + configFileName + "' not found.")
}
// Set default env if not given
if err != nil || configEnvironment == "" {
configEnvironment = DefaultEnvironment
m.InfoMessage("Using default environment '" + DefaultEnvironment + "'.")
}
// Read from the config file and add it to an object
var configFile File
err = json.Unmarshal(file, &configFile)
// Return error if config file is incorrect
if err != nil {
return fmt.Errorf("error unmarshalling the config file: %s", err)
}
// Loop through all values in object to see whether the given connection-name exists
foundEnvironment := false
for _, env := range configFile.Environments {
if env.Name == configEnvironment {
foundEnvironment = true
// Get config interface data
f.Environment = env
}
}
// Return default database because no good config is found
if !foundEnvironment {
return errors.New("no environment found with name '" + configEnvironment + "'")
}
m.InfoMessage("Config file found, loading environment..")
// Check the debug mode
m.Debug = f.Environment.Debug
if f.Environment.Debug {
m.InfoMessage("Running in DEBUG-mode")
}
return nil
}
// GetInstance from config
func (f *WeaviateConfig) GetInstance(hostname string) (instance Instance, err error) {
err = nil
found := false
// For each instance, check if hostname is the same
for _, v := range f.Environment.Development.ExternalInstances {
if hostname == v.URL {
instance = v
found = true
break
}
}
if !found {
// Didn't find item in list
err = errors.New("can't find key for given instance")
return
}
return
}