-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
171 lines (142 loc) · 3.82 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
)
const (
configPath string = "./config.json"
)
var (
// Config stores the global server
// configuration parsed from the passed
// config JSON file
Config *Configuration
// config holds the configuration
// path
config string
)
// Configuration holds configuration information
// for the service
//
// Hooks, used for the hooked analysis
// endpoint, are given as a map of id's
// to Hooks. (id's are strings that are
// specified within the passed JSON to the
// POST /task endpoint.) The default hook
// can be passed so you won't have to
// manually pass in a hookId when running
// a task. It defaults to the given hook
// when you only pass one hook in the config.
// When padding multiple, it defaults to
// a random hook.
type Configuration struct {
Port int16 `json:"port,omitempty"`
portString string
Hooks map[string]Hook `json:"hooks,omitempty"`
DefaultHook string `json:"defaultHook,omitempty"`
}
// init grabs the config from the expected
// command line args. Else initializes with
// defaults
func init() {
// holds config path
flag.StringVar(&config, "conf", configPath, "Sets the server configuration filepath")
flag.StringVar(&config, "C", configPath, "(shorthand for -conf)")
}
// ParseConfigFromFile must be run after
// the flag.Parse() function has been run
// to set the global configuration
// variable
func ParseConfigFromFile() error {
path, err := filepath.Abs(config)
if err != nil {
return fmt.Errorf("ERROR: error generating absolute path from given config path. Does the file exist? %v", err)
}
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("ERROR: error opening config file: %v", err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return fmt.Errorf("ERROR: error reading config file from filepath. Path: %v. Error = %v", config, err)
}
// unmarshal file into Config struct
err = json.Unmarshal(bytes, &Config)
if err != nil {
return fmt.Errorf("ERROR: error unmarshalling given config file into a Config struct: %v", err)
}
if Config.Port == 0 {
Config.Port = 8080
}
Config.portString = fmt.Sprintf(":%v", Config.Port)
if Config.DefaultHook == "" {
for id := range Config.Hooks {
Config.DefaultHook = id
}
}
return nil
}
// ParseConfigFromURL pulls the config
// from a URL by making a get request
//
// This expects that the config path
// is, in fact, a URL routing to
// either HTTP or HTTPS
func ParseConfigFromURL() error {
resp, err := http.Get(config)
if err != nil {
return fmt.Errorf("ERROR: error making GET request to given url: %v. Error = %v", config, err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("ERROR: error reading config data from GET url response. URL: %v. Error = %v", config, err)
}
err = json.Unmarshal(data, &Config)
if err != nil {
return fmt.Errorf("ERROR: error unmarshalling config data from response body. URL: %v. Error = %v", config, err)
}
if Config.Port == 0 {
Config.Port = 8080
}
Config.portString = fmt.Sprintf(":%v", Config.Port)
if Config.DefaultHook == "" {
for id := range Config.Hooks {
Config.DefaultHook = id
}
}
return nil
}
// ParseConfig determines whether
// the config is at a URL or a file,
// unmarshals and munges the
// configuration, and returns
// any errors.
func ParseConfig() error {
var err error
// check if file
u, err := url.Parse(config)
if err != nil || !(u.Scheme == "http" || u.Scheme == "https") {
err = ParseConfigFromFile()
} else {
err = ParseConfigFromURL()
}
if err != nil {
return err
}
if Config.Port == 0 {
Config.Port = 8080
}
Config.portString = fmt.Sprintf(":%v", Config.Port)
if Config.DefaultHook == "" {
for id := range Config.Hooks {
Config.DefaultHook = id
}
}
return nil
}