-
Notifications
You must be signed in to change notification settings - Fork 0
/
nurdpaste.py
executable file
·90 lines (69 loc) · 2.62 KB
/
nurdpaste.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import argparse
import os
import pathlib
import yaml
import yaml.parser
from lib.server import NURDpasteAPI # pyright: ignore
from lib.backend import RedisBackend # pyright: ignore
DEFAULT_CONFIG = 'config.yml'
def load_and_validate_config(config_file: pathlib.Path) -> dict:
assert config_file != ""
assert os.path.exists(config_file)
config = None
with open(config_file, 'r') as fd:
try:
config = yaml.load(fd, yaml.SafeLoader)
except yaml.parser.ParserError as errmsg:
print(f'Failed to parse {config_file}: {errmsg}')
return {}
assert type(config) == dict
assert 'api' in config
assert 'redis' in config
assert 'logging' in config
assert 'bind_address' in config['api']
assert type(config['api']['bind_address']) == str
assert 'bind_port' in config['api']
assert type(config['api']['bind_port']) == int
assert 'max_size' in config['api']
assert type(config['api']['max_size']) == int
assert 'frontend' in config['api']
assert type(config['api']['frontend']) == str
assert 'base_url' in config['api']
assert type(config['api']['base_url']) == str
assert 'host' in config['redis']
assert type(config['redis']['host']) == str
assert 'port' in config['redis']
assert type(config['redis']['port']) == int
assert 'logger' in config['logging']
assert type(config['logging']['logger']) == str
assert config['logging']['logger'] != ""
assert 'config' in config['logging']
assert type(config['logging']['config']) == str
return config
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', dest='config_file', type=pathlib.Path, default=DEFAULT_CONFIG, help=f'Config file to use ({DEFAULT_CONFIG})')
args = parser.parse_args()
config = load_and_validate_config(args.config_file)
redis_backend = RedisBackend(
host=config['redis']['host'],
port=config['redis']['port'],
logconfig_use=config['logging']['logger'],
logconfig_file=config['logging']['config'],
)
nurdpaste_api = NURDpasteAPI(
bind_ip=config['api']['bind_address'],
bind_port=config['api']['bind_port'],
max_size=config['api']['max_size'],
default_ttl=config['api']['default_ttl'],
logconfig_use=config['logging']['logger'],
logconfig_file=config['logging']['config'],
frontend=config['api']['frontend'],
base_url=config['api']['base_url'],
backend=redis_backend,
)
try:
nurdpaste_api.run()
except KeyboardInterrupt:
pass