forked from vvfesik/vgs-satellite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
161 lines (145 loc) · 4.39 KB
/
app.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
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
import locale
import logging
import os
from multiprocessing import set_start_method
import click
from tblib import pickling_support
from satellite import db
from satellite import logging as satellite_logging
from satellite.aliases.store import AliasStore
from satellite.config import (
InvalidConfigError,
SatelliteConfig,
configure,
init_satellite_dir,
)
from satellite.routes.loaders import LoadError, load_from_yaml
from satellite.web_application import WebApplication
DEFAULT_CONFIG = SatelliteConfig()
# Do not put default values for non-flag options. Otherwise other config
# sources (env and config file) will be ignored.
@click.command()
@click.option(
'--debug',
is_flag=True,
default=None,
envvar='SATELLITE_DEBUG',
help=f'[env:SATELLITE_DEBUG] (default:{DEFAULT_CONFIG.debug}) Debug mode.',
)
@click.option(
'--web-server-port', # TODO: Rename to --api-port
type=int,
envvar='SATELLITE_API_PORT',
help=(
'[env:SATELLITE_API_PORT] '
f'(default:{DEFAULT_CONFIG.web_server_port}) API port.'
),
)
@click.option(
'--reverse-proxy-port',
type=int,
envvar='SATELLITE_REVERSE_PROXY_PORT',
help=(
'[env:SATELLITE_REVERSE_PROXY_PORT] '
f'(default: {DEFAULT_CONFIG.reverse_proxy_port}) Reverse proxy port.'
),
)
@click.option(
'--forward-proxy-port',
type=int,
envvar='SATELLITE_FORWARD_PROXY_PORT',
help=(
'[env:SATELLITE_FORWARD_PROXY_PORT] '
f'(default:{DEFAULT_CONFIG.forward_proxy_port}) Forward proxy port.'
),
)
@click.option(
'--config-path',
type=click.Path(exists=True, dir_okay=False),
envvar='SATELLITE_CONFIG_PATH',
help=(
'[env:SATELLITE_CONFIG_PATH] (default:$HOME/.vgs-satellite/config.yml) '
'Path to the config YAML file.'
),
)
@click.option(
'--db-path',
type=click.Path(dir_okay=False),
envvar='SATELLITE_DB_PATH',
help=(
'[env:SATELLITE_DB_PATH] (default:$HOME/.vgs-satellite/db.sqlite) '
'Path to the DB file.'
),
)
@click.option(
'--log-path',
type=click.Path(dir_okay=False),
envvar='SATELLITE_LOG_PATH',
help=('[env:SATELLITE_LOG_PATH] (default:None) Path to a log file.'),
)
@click.option(
'--silent',
is_flag=True,
default=None,
envvar='SATELLITE_SILENT',
help=(
f'[env:SATELLITE_SILENT] (default:{DEFAULT_CONFIG.silent}) '
'Do not log into stdout.'
),
)
@click.option(
'--volatile-aliases-ttl',
type=int,
default=None,
envvar='VOLATILE_ALIASES_TTL',
help=(
f'[env:VOLATILE_ALIASES_TTL] (default:{DEFAULT_CONFIG.volatile_aliases_ttl}) '
'TTL for volatile aliases in seconds.'
),
)
@click.option(
'--routes-path',
type=click.Path(exists=True, dir_okay=False),
envvar='SATELLITE_ROUTES_PATH',
help=(
'[env:SATELLITE_ROUTES_PATH] (default:None) Path to a routes config '
'YAML file. If provided all the current routes present in Satellite '
'DB will be deleted.'
),
)
def main(**kwargs):
set_start_method('fork') # PyInstaller supports only fork start method
pickling_support.install()
init_satellite_dir()
try:
config = configure(
**{name: value for name, value in kwargs.items() if value is not None}
)
except InvalidConfigError as exc:
raise click.ClickException(f'Invalid config: {exc}') from exc
satellite_logging.configure(log_path=config.log_path, silent=config.silent)
logger = logging.getLogger()
db.configure(config.db_path)
try:
db.init()
except db.DBVersionMismatch as exc:
raise click.ClickException(exc) from exc
if config.routes_path:
with open(config.routes_path, 'r') as stream:
try:
loaded_routes_count = load_from_yaml(stream)
except LoadError as exc:
raise click.ClickException(
f'Unable to load routes from file: {exc}'
) from exc
logger.info(f'Loaded {loaded_routes_count} routes from routes config file.')
deleted_aliases = AliasStore.cleanup()
logger.info(f'Deleted {deleted_aliases} expired aliases.')
app = WebApplication(config)
app.start()
if __name__ == '__main__':
# Locale should be set before runing Click
lang, encoding = locale.getdefaultlocale()
if not lang or not encoding:
os.environ['LC_ALL'] = 'en_US.utf-8'
main()