This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·75 lines (61 loc) · 2.17 KB
/
main.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
#!/usr/bin/env python3
import os
import click
from consumatio.app import App
from consumatio.constants import DEFAULT_DATABASE_URI
from consumatio.exceptions.undefined_environment_variable import \
UndefinedEnvironmentVariable
@click.command()
@click.option("--tmdb-key",
envvar="TMDB_KEY",
help="API key for the TMDB API",
required=True)
@click.option("--backend-secret",
envvar="BACKEND_SECRET",
help="Secret for the backend",
required=True)
@click.option("--database-uri",
default=DEFAULT_DATABASE_URI,
envvar="DATABASE_URI",
help="URI for the database",
required=True)
@click.option("--port", default=5000, envvar="PORT", help="Port to listen on")
@click.option("--debug",
default=False,
envvar="DEBUG",
help="Enable debug mode")
def run_consumatio_backend_cli(tmdb_key, backend_secret, database_uri, port,
debug):
"""Backend for https://github.com/alphahorizonio/consumat.io."""
# Configure the app
app = App(tmdb_key, backend_secret, database_uri, port, debug)
app.configure()
print(f"Listening on port {port}")
# Start the app
app.run()
if __name__ == '__main__':
# Running as CLI
run_consumatio_backend_cli()
else:
# Running with WSGI, so manually read env variables
tmdb_key = os.getenv("TMDB_KEY")
backend_secret = os.getenv("BACKEND_SECRET")
database_uri = os.getenv("DATABASE_URI")
port = os.getenv("PORT")
debug = os.getenv("DEBUG")
# Check if env variables are set
if tmdb_key == None:
raise UndefinedEnvironmentVariable(
"Please specify the TMDB_KEY env variable")
if backend_secret == None:
raise UndefinedEnvironmentVariable(
"Please specify the BACKEND_SECRET env variable")
if database_uri == None:
database_uri = DEFAULT_DATABASE_URI
if debug == None:
debug = False
# Configure the app
app = App(tmdb_key, backend_secret, database_uri, None, debug)
app.configure()
# Expose the Flask app instance to WSGI
api = app.app