-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
67 lines (54 loc) · 1.35 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
60
61
62
63
64
65
66
67
// Copyright 2014 Simon Zimmermann. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package server
import (
"fmt"
"github.com/simonz05/carry"
"github.com/simonz05/carry/config"
"github.com/simonz05/util/log"
)
type context struct {
sto carry.Storage
allowOrigin []string
}
func newContextFromConfig(conf *config.Config) (*context, error) {
// We just have stathat storage for now.
if conf.Stathat == nil && conf.InfluxDB == nil {
return nil, fmt.Errorf("expected stathat/influxdb storage")
}
var storage carry.Storage
var backends []carry.Storage
if conf.Stathat != nil {
sto, err := carry.CreateStorage("stathat", conf)
if err != nil {
return nil, err
}
backends = append(backends, sto)
log.Println("stathat storage initialized")
}
if conf.InfluxDB != nil {
sto, err := carry.CreateStorage("influxdb", conf)
if err != nil {
return nil, err
}
backends = append(backends, sto)
log.Println("influxdb storage initialized")
}
if len(backends) == 1 {
storage = backends[0]
} else {
storage = carry.MultiStorage(backends)
}
return &context{
sto: storage,
allowOrigin: conf.AllowOrigin,
}, nil
}
func (c *context) Close() error {
closer, ok := c.sto.(carry.ShutdownStorage)
if ok {
return closer.Close()
}
return nil
}