-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
62 lines (52 loc) · 1.73 KB
/
server.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
from flask import Flask, request, jsonify, Response
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask import send_from_directory
from query import query
import json
import requests
from pinecone import Pinecone
with open('keys.json', 'r') as f:
keys = json.load(f)
pc = Pinecone(api_key=keys["pinecone"])
app = Flask(__name__)
limiter = Limiter(
get_remote_address,
app=app,
default_limits=[],
storage_uri="memory://",
)
@app.route("/")
def index():
return send_from_directory("site/dist", "index.html")
@app.route('/<path:path>')
def send_static_files(path):
return send_from_directory("site/dist", path)
@app.route("/search")
@limiter.limit("50/day;10/minute")
def search():
res = query(request.args.get("q"))
matches = res["matches"]
response = jsonify(matches)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/fetchxkcd/<int:id>")
def fetchxkcd(id):
data = requests.get(f"https://xkcd.com/{id}/info.0.json")
response = Response(data.text, mimetype="text/json")
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/getMapURL")
def getMapURL():
with open("data/mapURL.txt", "r") as f:
response = Response(f.read(), mimetype="text/plain")
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/getMostRecentComicID")
def getMostRecentComicID():
index = pc.Index("openai")
response = Response(str(index.describe_index_stats().total_vector_count), mimetype="text/plain")
response.headers.add("Access-Control-Allow-Origin", "*")
return response
if __name__ == '__main__':
app.run(port=80, debug=False)