-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_app.py
61 lines (48 loc) · 1.56 KB
/
sample_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
import os
from flask import Flask, request, jsonify, send_file, Response
from middleware import setup_middleware
app = Flask(__name__)
setup_middleware(app)
# GET endpoint
@app.route('/get', methods=['GET'])
def get_endpoint():
response_data = {
"message": "This is a GET request",
"status": "success"
}
return jsonify(response_data)
# POST endpoint
@app.route('/post', methods=['POST'])
def post_endpoint():
data = request.json
return jsonify(data)
# File upload endpoint
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({"msg": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"msg": "No selected file"}), 400
if file:
filename = file.filename
file.save(os.path.join('uploads', filename))
return jsonify({"msg": "success"}), 200
@app.route('/download', methods=['GET'])
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename=None):
if filename:
file_path = os.path.join('uploads', filename)
if os.path.exists(file_path):
return send_file(file_path, as_attachment=True)
else:
return "File not found", 404
else:
files = os.listdir('uploads')
if not files:
return jsonify({"message": "No files found"}), 404
return jsonify({"files": files})
if __name__ == '__main__':
# Ensure the upload directory exists
os.makedirs('uploads', exist_ok=True)
app.run(debug=True)