-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
31 lines (21 loc) · 740 Bytes
/
Copy pathapp.py
File metadata and controls
31 lines (21 loc) · 740 Bytes
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
import os
import requests
from flask import Flask, request, jsonify
from cache import Cache
def create_app():
''' Creates the Flask app '''
app = Flask(__name__)
cache = Cache()
origin_server = os.getenv('ORIGIN_URL')
@app.route('/<path:url>', methods=['GET'])
def proxy(url):
full_url = f'{origin_server}/{url}'
if cache.exists(full_url):
response = cache.get(full_url)
print('X-Cache: HIT')
return jsonify(response), 200, {'X-Cache': 'HIT'}
response = requests.get(full_url)
cache.set(full_url, response.json())
print('X-Cache: MISS')
return jsonify(response.json()), 200, {'X-Cache': 'MISS'}
return app