Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (43 loc) · 1.87 KB
/
Copy pathapp.py
File metadata and controls
54 lines (43 loc) · 1.87 KB
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
from flask import Flask, render_template, request, redirect, url_for, jsonify
import os, sys
from src.sender import enviar
app = Flask(__name__, template_folder="templates")
@app.route('/', methods=['GET', 'POST', 'PUT'])
def img():
"""Define la ruta a la que se enviarán imágenes para ser procesadas.
Los métodos aceptados son PUT y POST, que requieren de una imagen al enviar
la petición. Se devuelve un 200 si todo ha ido bien.
"""
if request.method == 'POST' or request.method == 'PUT':
img = request.files['data']
enviar(img.read())
return render_template('home.html')
else:
return "Bienvenido al microservicio de tratamiento de imágenes"
@app.route('/status', methods=['GET'])
def status():
"""Define la ruta de comprobación de funcionamiento del servidor.
Se accede mediante un GET y se devuelve un 200 si todo ha ido bien.
"""
body = {'status': 'OK', 'Code': 200, 'Instructions': 'post an image to /, get the image from /img/procesado.png'}
response = jsonify(body)
response.status_code = 200
return response
@app.route('/img/<archivo>', methods=['GET'])
def perfil_usuario(archivo):
"""Define la ruta para devolver una imagen. Se accede con el método GET y
devuelve la imagen dada por el string introducido.
Args:
archivo: nombre del archivo que se desea recuperar.
"""
if os.path.isfile(archivo):
dir_path = os.path.dirname(os.path.realpath(archivo))
dir_path = dir_path + "/" + archivo
return render_template('mostrar_imagen.html', src=dir_path)
else:
return redirect(url_for('error_404'))
@app.errorhandler(404)
def error_404(error):
return "Error 404, page not found. We can't find the page or file you are looking for", 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('POST', 5000)), debug=True)