Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
430 lines (392 loc) · 12.5 KB
/
Copy pathapp.py
File metadata and controls
430 lines (392 loc) · 12.5 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from flask import Flask, render_template, request, send_file, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from functools import wraps
from dotenv import load_dotenv
import markdown2
from weasyprint import HTML, CSS
from io import BytesIO
import os
# Cargar variables de entorno desde .env
load_dotenv()
app = Flask(__name__)
# Configuración
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
API_KEY = os.getenv('API_KEY') # API Key OBLIGATORIA desde .env
# Verificar que existe API_KEY
if not API_KEY:
raise ValueError(
"⚠️ API_KEY no configurada! "
"Crea un archivo .env con tu API_KEY "
"(usa .env.example como plantilla)"
)
# Configurar Rate Limiting (protección contra fuerza bruta)
limiter = Limiter(
app=app,
key_func=get_remote_address,
default_limits=[os.getenv('RATE_LIMIT_DEFAULT', '20 per minute')],
storage_uri=os.getenv('RATE_LIMIT_STORAGE_URL', 'memory://')
)
def get_rate_limit(env_var, default):
"""
Helper para obtener rate limit desde variable de entorno.
Convierte 'unlimited', '0' o '' a un valor muy alto (ilimitado).
"""
limit = os.getenv(env_var, default)
if limit.lower() in ['unlimited', '0', '']:
return '100000 per hour' # Prácticamente ilimitado
return limit
def require_api_key(f):
"""
Decorador para requerir API Key en todos los endpoints.
La API Key puede venir de:
- Header 'X-API-Key' (para API REST)
- Form field 'api_key' (para interfaz web)
- Query parameter 'api_key' (alternativa)
"""
@wraps(f)
def decorated_function(*args, **kwargs):
# Obtener API Key de diferentes fuentes
provided_key = (
request.headers.get('X-API-Key') or
request.form.get('api_key') or
request.args.get('api_key')
)
if not provided_key:
# Si es una petición JSON, responder con JSON
if request.is_json or request.path.startswith('/api/'):
return jsonify({
'error': 'API Key requerida',
'message': 'Incluye el header X-API-Key en tu petición'
}), 401
# Si es interfaz web, mostrar mensaje HTML
return '''
<!DOCTYPE html>
<html>
<head>
<title>API Key Requerida</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.container {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
max-width: 500px;
}
h1 { color: #333; margin-bottom: 20px; }
p { color: #666; line-height: 1.6; }
code {
background: #f4f4f4;
padding: 2px 6px;
border-radius: 3px;
color: #e83e8c;
}
</style>
</head>
<body>
<div class="container">
<h1>🔐 API Key Requerida</h1>
<p>Este microservicio requiere autenticación.</p>
<p>Accede con tu API Key mediante:</p>
<ul style="text-align: left; color: #666;">
<li>URL: <code>?api_key=TU_CLAVE</code></li>
<li>Header: <code>X-API-Key: TU_CLAVE</code></li>
</ul>
<p style="margin-top: 20px; color: #999; font-size: 14px;">
Contacta al administrador para obtener tu API Key
</p>
</div>
</body>
</html>
''', 401
if provided_key != API_KEY:
if request.is_json or request.path.startswith('/api/'):
return jsonify({
'error': 'API Key inválida',
'message': 'La API Key proporcionada no es válida'
}), 403
return '''
<!DOCTYPE html>
<html>
<head>
<title>API Key Inválida</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
.container {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
}
h1 { color: #e74c3c; }
p { color: #666; }
</style>
</head>
<body>
<div class="container">
<h1>❌ API Key Inválida</h1>
<p>La API Key proporcionada no es válida.</p>
<p>Verifica tu configuración e intenta nuevamente.</p>
</div>
</body>
</html>
''', 403
return f(*args, **kwargs)
return decorated_function
def process_markdown_to_pdf(markdown_content):
"""
Función helper para convertir Markdown a PDF.
Retorna un BytesIO con el PDF generado.
"""
if not markdown_content:
raise ValueError("No se proporcionó contenido Markdown")
# Preprocesar: Asegurar línea en blanco antes de tablas
lines = markdown_content.split('\n')
processed_lines = []
for i, line in enumerate(lines):
if line.strip().startswith('|') and i > 0:
prev_line = lines[i-1].strip()
if prev_line and not prev_line.startswith('|'):
if processed_lines and processed_lines[-1].strip():
processed_lines.append('')
processed_lines.append(line)
markdown_content = '\n'.join(processed_lines)
# Convertir Markdown a HTML
html_content = markdown2.markdown(
markdown_content,
extras=[
'tables',
'fenced-code-blocks',
'strike',
'task_list'
]
)
# CSS para mejorar el estilo del PDF
css_style = """
@page {
margin: 1.2cm 0.8cm;
size: A4 portrait;
}
body {
font-family: 'Arial', 'Helvetica', sans-serif;
line-height: 1.4;
color: #222;
font-size: 9pt;
}
h1, h2, h3, h4, h5, h6 {
color: #2c3e50;
margin-top: 0.6em;
margin-bottom: 0.3em;
}
h1 {
font-size: 16pt;
border-bottom: 2px solid #2c3e50;
padding-bottom: 0.2em;
}
h2 {
font-size: 13pt;
border-bottom: 1px solid #bdc3c7;
padding-bottom: 0.2em;
}
h3 {
font-size: 11pt;
}
code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 2px;
font-family: 'Courier New', monospace;
font-size: 8pt;
}
pre {
background-color: #f4f4f4;
padding: 8px;
border-radius: 3px;
border-left: 3px solid #3498db;
overflow: visible;
white-space: pre-wrap;
word-wrap: break-word;
page-break-inside: avoid;
margin: 0.5em 0;
}
pre code {
background-color: transparent;
padding: 0;
font-size: 7pt;
white-space: pre-wrap;
word-break: break-word;
}
blockquote {
border-left: 4px solid #3498db;
padding-left: 15px;
color: #555;
font-style: italic;
margin: 1em 0;
}
table {
border-collapse: collapse;
width: 100%;
margin: 0.5em 0;
page-break-inside: auto;
font-size: 6.5pt;
table-layout: fixed;
}
table th, table td {
border: 1px solid #555;
padding: 3px 4px;
text-align: left;
word-wrap: break-word;
vertical-align: top;
overflow-wrap: anywhere;
line-height: 1.25;
hyphens: auto;
}
table th {
background-color: #2c7cbd;
color: white;
font-weight: bold;
font-size: 6.5pt;
}
table tr:nth-child(even) {
background-color: #f8f8f8;
}
table tr {
page-break-inside: avoid;
}
table code {
font-size: 6pt;
word-break: break-word;
white-space: normal;
}
a {
color: #3498db;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
ul, ol {
margin: 1em 0;
padding-left: 2em;
}
li {
margin: 0.5em 0;
}
img {
max-width: 100%;
height: auto;
}
"""
# Crear el HTML completo
full_html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Documento PDF</title>
</head>
<body>
{html_content}
</body>
</html>
"""
# Generar PDF
pdf_buffer = BytesIO()
html_obj = HTML(string=full_html)
pdf_bytes = html_obj.write_pdf(stylesheets=[CSS(string=css_style)])
pdf_buffer.write(pdf_bytes)
pdf_buffer.seek(0)
return pdf_buffer
@app.route('/')
@limiter.limit(lambda: get_rate_limit('RATE_LIMIT_WEB_INDEX', '20 per minute'))
def index():
"""Página principal con el formulario (sin autenticación para ver el form)"""
return render_template('index.html')
@app.route('/convert', methods=['POST'])
@limiter.limit(
lambda: get_rate_limit('RATE_LIMIT_WEB_CONVERT', '10 per minute')
)
@require_api_key
def convert_to_pdf():
"""Endpoint web: Convierte el contenido Markdown a PDF desde formulario"""
try:
markdown_content = request.form.get('markdown_content', '')
pdf_buffer = process_markdown_to_pdf(markdown_content)
return send_file(
pdf_buffer,
mimetype='application/pdf',
as_attachment=True,
download_name='documento.pdf'
)
except ValueError as e:
return str(e), 400
except Exception as e:
return f"Error al generar el PDF: {str(e)}", 500
@app.route('/api/convert', methods=['POST'])
@limiter.limit(
lambda: get_rate_limit('RATE_LIMIT_API_CONVERT', '10 per minute')
)
@require_api_key
def api_convert_to_pdf():
"""
Endpoint API REST: Convierte Markdown a PDF desde JSON.
Requiere API Key obligatoriamente.
"""
try:
# Validar Content-Type
if not request.is_json:
return jsonify({
'error': 'Content-Type debe ser application/json'
}), 400
data = request.get_json()
markdown_content = data.get('markdown', '')
if not markdown_content:
return jsonify({
'error': 'El campo "markdown" es requerido'
}), 400
pdf_buffer = process_markdown_to_pdf(markdown_content)
return send_file(
pdf_buffer,
mimetype='application/pdf',
as_attachment=True,
download_name='documento.pdf'
)
except ValueError as e:
return jsonify({'error': str(e)}), 400
except Exception as e:
return jsonify({'error': f'Error al generar PDF: {str(e)}'}), 500
@app.route('/api/health', methods=['GET'])
@limiter.limit(
lambda: get_rate_limit('RATE_LIMIT_API_HEALTH', '60 per minute')
)
def api_health():
"""Endpoint de healthcheck para monitoreo (sin autenticación)"""
return jsonify({
'status': 'healthy',
'service': 'markdown-to-pdf',
'version': '1.0.0'
}), 200
if __name__ == '__main__':
# Usar variable de entorno para debug
debug_env = os.getenv('FLASK_DEBUG', 'False').lower()
debug_mode = debug_env in ('true', '1', 't')
app.run(host='0.0.0.0', port=5000, debug=debug_mode)