-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
56 lines (43 loc) · 1.28 KB
/
service.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
package godnsbl
import (
"io/ioutil"
"log"
"os"
"time"
"gopkg.in/yaml.v2"
)
// NewLookupServiceWithConfig creates a new service using a config file
func NewLookupServiceWithConfig(configFile string) *LookupService {
lookupService := NewLookupService()
fileInfo, err := os.Stat(configFile)
// If the config file doesn't exist or it's a directory just return an empty service
if os.IsNotExist(err) || fileInfo.IsDir() {
return lookupService
}
configData := readConfig(configFile)
err = yaml.Unmarshal(configData, lookupService)
if err != nil {
log.Fatal("Could not unmarshal the data:", err)
}
lookupService.timeout, err = time.ParseDuration(lookupService.DnsblTimeout)
if err != nil {
log.Fatal("Failed to parse dnsbl timeout:", lookupService.DnsblTimeout)
}
return lookupService
}
// NewLookupService creates a new service without using a config file
func NewLookupService() *LookupService {
return &LookupService{
DnsblTimeout: "30s",
StartTime: time.Now().Unix(),
timeout: 30000000000, // This is just the value of ParseDuration("30s")
}
}
// readConfig reads the config file
func readConfig(configFile string) []byte {
yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal("Could not read the config file:", err)
}
return yamlFile
}