This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
53 lines (41 loc) · 1.57 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
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
import json
import os
from BookGenerator.BookGenerator import BookGenerator
from BookGenerator.Book import Book
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
CORS(app) # This will enable CORS for all route
@app.route('/book', methods=['POST', 'OPTIONS'])
def book():
if request.method == 'OPTIONS':
# Preflight request. Reply successfully:
return jsonify({'success': True}), 200
if not request.json:
return jsonify({'error': 'No id provided'}), 400
print(request.json)
if not os.path.isdir("tmpAssets"):
os.mkdir("tmpAssets")
if not os.path.isdir("tmpAssets/Images"):
os.mkdir("tmpAssets/Images")
result: Book = BookGenerator().getBook(request.json)
print(result.to_dict().get('text_pages'))
return jsonify(result.to_dict()), 200
# return jsonify("Hello, World!"), 200
@app.route('/send_pdf', methods=['GET', 'OPTIONS'])
def send_pdf():
if request.method == 'OPTIONS':
# Preflight request. Reply successfully:
return jsonify({'success': True}), 200
pdf_file = "/tmpAssets/output.pdf"
with open(pdf_file, 'rb') as f:
data = f.read()
response = make_response(data)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline;filename=tmpAssets/output.pdf'
return response
if __name__ == '__main__':
# print(BookGenerator().getBook(dict()))
app.run(port=5000) # Runs the server in development mode on port 5000.