-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
59 lines (48 loc) · 1.04 KB
/
context.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
package miru
import (
"io/ioutil"
rdb "github.com/dancannon/gorethink"
)
// Context holds database, configuration and queue data.
type Context struct {
Db *rdb.Session
Config *Config
Queues *Queues
}
// NewContext instantiates a new context and initialises a queue.
func NewContext() *Context {
ctx := new(Context)
ctx.InitQueues()
return ctx
}
// LoadConfig reads a given file from the filesystem, if not found uses the
// default config.
func (c *Context) LoadConfig(f string) error {
file, err := ioutil.ReadFile(f)
data := string(file)
if err != nil {
data = DefaultConfig
}
conf, err := LoadConfig(data)
if err != nil {
return err
}
c.Config = conf
return nil
}
// Connect creates a connection to the database.
func (c *Context) Connect(host string) error {
session, err := rdb.Connect(rdb.ConnectOpts{
Address: host,
Database: c.Config.Database.Name,
})
if err != nil {
return err
}
c.Db = session
return nil
}
// InitQueues initialises a new queue list.
func (c *Context) InitQueues() {
c.Queues = NewQueues()
}