-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
68 lines (59 loc) · 2.17 KB
/
config.py
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
from os import path, getenv
import yaml
class Config:
"""
A simple handler for configurations. The methods of this are used to handle
config properties used to configure dotoxy
"""
def __init__(self, config_file="config.yml"):
"""
:type config_file: str
:arg config_file:
Containing the file path to config file
"""
# Default values
upstream = {
"host": "1.1.1.1",
"port": 853,
"cn": "cloudflare-dns.com",
"ca-file": None,
}
server = {
"mode": "tcp",
"bind-address": "0.0.0.0",
"bind-port": 53,
}
# The configuration is set in a yml file.
dirname = path.dirname(path.abspath(__file__))
absolute_path = str(dirname + "/" + config_file)
self.config_file = getenv("CONFIG_ADDRESS", absolute_path)
with open(self.config_file, "r") as ymlfile:
config = yaml.safe_load(ymlfile)
upstream_configfile = config.get("upstream", {})
if upstream_configfile is None:
upstream_configfile = {}
server_configfile = config.get("server", {})
if server_configfile is None:
server_configfile = {}
# Merge config file and default configs
upstream.update(upstream_configfile)
server.update(server_configfile)
# The configuration is set in environment variables
upstream_envs = {
"host": getenv("UPSTREAM__HOST", upstream['host']),
"port": getenv("UPSTREAM__PORT", upstream['port']),
"cn": getenv("UPSTREAM__CN", upstream['cn']),
"ca-file": getenv("UPSTREAM__CA_FILE", upstream['ca-file']),
}
server_envs = {
"mode": getenv("SERVER__MODE", server['mode']),
"bind-address": getenv(
"SERVER__BIND_ADDRESS", server['bind-address']
),
"bind-port": getenv("SERVER__BIND_PORT", server['bind-port']),
}
# Merge all configs
upstream.update(upstream_envs)
server.update(server_envs)
self.upstream = upstream
self.server = server