-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
67 lines (55 loc) · 1.36 KB
/
config.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
package database
import (
"encoding/json"
"errors"
)
type ConfigDatabase struct {
Type TypeDatabase `json:"type"`
FileDataSource string `json:"file_data_source"`
Debug bool `json:"debug"`
Migration bool `json:"migration"`
Options map[string]interface{}
}
func convertStruct(src interface{}, des interface{}) error {
dataBase, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(dataBase, des)
}
func (c ConfigDatabase) Sqlite() (source string, err error) {
source = "dipper.db:ent?mode=memory&cache=shared&_fk=1"
raw, ok := c.Options["file_source"]
if ok {
source, ok = raw.(string)
if !ok {
err = errors.New("file_source not string")
}
}
return
}
func (c ConfigDatabase) Postgrest(name string) (pg *PostgrestConfig, err error) {
pg = &PostgrestConfig{
Host: "127.0.0.1",
Port: 5432,
Database: "dipper_engine",
}
raw, ok := c.Options[name]
if ok {
err = convertStruct(raw, pg)
}
return
}
type PostgrestConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string
Password string `json:"password"`
Database string `json:"database"`
}
type TypeDatabase string
const (
SqliteType TypeDatabase = "sqlite"
PostgrestType TypeDatabase = "postgres"
PostgrestClusterType TypeDatabase = "postgres_cluster"
)