-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.go
60 lines (48 loc) 路 1.14 KB
/
example.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
package main
// example application that runs along side ProxySQL
import (
. "github.com/kirinrastogi/proxysql-go" // import into current namespace, use proxysql. if you omit this period
"log"
)
// ProxySQLConn is an example interface to accept as a parameter to funcs
// instead of actual sql.DB connections
type ProxySQLConn interface {
AddHost(...HostOpts) error
AddHosts(...*Host) error
PersistChanges() error
}
func main() {
// this is the dsn of the container that ./run.sh creates
conn, err := NewProxySQL("remote-admin:password@tcp(localhost:6032)/")
if err != nil {
log.Fatal(err)
}
err = conn.Clear()
if err != nil {
log.Fatal(err)
}
addHostsAndSave(conn)
entries, err := conn.All()
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
log.Printf("entry: %v\n", entry)
}
log.Println("Success")
}
func addHostsAndSave(conn ProxySQLConn) error {
err := conn.AddHost(Hostname("example"), HostgroupID(1))
if err != nil {
return err
}
err = conn.AddHosts(DefaultHost().SetHostname("example2"))
if err != nil {
return err
}
err = conn.PersistChanges()
if err != nil {
return err
}
return nil
}