forked from allout/solidarity-map-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·75 lines (62 loc) · 2.31 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
#!/usr/bin/env python
import importlib
import json
import logging
import os
import requests
import sys
from eve import Eve, auth
from flask import abort
from hashlib import sha256
from pymongo import MongoClient
CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(CURRENT_PATH, 'settings'))
EVE_SETTINGS = os.environ.get('EVE_SETTINGS', 'settings/production.py')
EVE_SETTINGS_ROOT, _ = os.path.splitext(EVE_SETTINGS)
EVE_SETTINGS_PROFILE = os.path.basename(EVE_SETTINGS_ROOT)
ABS_SETTINGS_PATH = os.path.join(CURRENT_PATH, EVE_SETTINGS)
# Allow current settings to be available to this code
imported_settings = importlib.import_module(f'settings.{EVE_SETTINGS_PROFILE}')
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(getattr(logging, imported_settings.LOG_LEVEL))
class BasicAuth(auth.BasicAuth):
def check_auth(self, username, password, allowed_roles, resource, method):
return (
username == imported_settings.API_USER
and password == imported_settings.API_PASSWORD
)
def recaptcha_hook(resource, request, lookup=None):
recaptcha_payload = {
'response': request.json.pop('gRecaptchaResponse', None),
'secret': imported_settings.RECAPTCHA_SECRET_KEY,
}
logger.debug(recaptcha_payload)
recaptcha_response = requests.post(
'https://www.google.com/recaptcha/api/siteverify', recaptcha_payload,
)
response_text = json.loads(recaptcha_response.text)
if not response_text['success']:
logger.debug(response_text)
abort(403)
# Initialize MongoDB client with Cosmos DB connection string
client = MongoClient(imported_settings.MONGO_URI)
db = client[imported_settings.MONGO_DBNAME]
def get_eve_app():
"""
Initializes Eve app with given configuration.
Main entry point for wsgi (gunicorn) server.
:param config: Configuration dictionary
:return: app
"""
app = Eve(settings=ABS_SETTINGS_PATH, auth=BasicAuth)
if imported_settings.RECAPTCHA_ENABLED:
app.on_pre_POST = recaptcha_hook
app.on_pre_PATCH = recaptcha_hook
app.on_pre_PUT = recaptcha_hook
return app
app = get_eve_app()
if __name__ == '__main__':
# Main entry point when run in stand-alone mode.
logger.setLevel(logging.DEBUG)
app.run(debug=True,host='0.0.0.0')