-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·54 lines (44 loc) · 1.59 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
#!flask/bin/python2.7
from flask import Flask, jsonify, request, redirect, url_for, send_from_directory, make_response
from werkzeug.utils import secure_filename
from PIL import Image
import os
import json
from solver import Solver
from parse_path import path_to_moves
UPLOAD_FOLDER = 'uploads'
app = Flask(__name__, static_url_path='')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/uploads/<filename>')
def uploaded_file(filename):
resp = make_response(send_from_directory(app.config['UPLOAD_FOLDER'], filename))
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/api/maze', methods=['POST'])
def upload_maze():
def get_response_for_path(path):
if path:
return jsonify({ 'solveable': True, 'moves': path_to_moves(path) })
else:
return jsonify({ 'solveable': False })
file = request.files['file']
filename = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename))
file.save(filename)
s = Solver(filename)
path = s.solve()
response = get_response_for_path(path)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)