-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
75 lines (56 loc) · 2.23 KB
/
api.py
File metadata and controls
75 lines (56 loc) · 2.23 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
import os
import sys
from flasgger import Swagger
from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_restx import Api
from urllib.error import HTTPError
from commonsLib import loggerElk
app = Flask(__name__)
api = Api(app, version='1.0', prefix='/api', title='GBC OCR Pdf2Readable API',
description='Microservice for OCR text from an scanned PDF, and inserting in the same PDF',
)
# Enable Swagger and CORS
ns = api.namespace('gbc/ocr/image/pdf/readable',
description='OCR text from an scanned PDF, and inserting in the same PDF')
Swagger(app)
cors = CORS(app)
# JWT configuration
app.config['JWT_SECRET_KEY'] = 'jwt-secret-string'
jwt = JWTManager(app)
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
app.config.from_json('./config/config.json')
print('----- CONFIG TEST -----')
print(app.config['WELCOME'])
logger = loggerElk(__name__)
from GbcOcrImagePdf2ReadableResource import GbcOcrImagePdf2ReadableResource
ns.add_resource(GbcOcrImagePdf2ReadableResource, '')
from GbcOcrImagePdf2ReadableCacheCleanerResource import GbcOcrImagePdf2ReadableCacheCleanerResource
ns.add_resource(GbcOcrImagePdf2ReadableCacheCleanerResource, '/clear')
# HealthCheck
from healthcheck import HealthCheck, EnvironmentDump
health = HealthCheck()
envdump = EnvironmentDump()
def service_avaliable():
ELK_ENABLED = os.environ["ELK_ENABLED"]
if isinstance(ELK_ENABLED, str):
ELK_ENABLED = True if ELK_ENABLED == "True" else False
logger = loggerElk(__name__)
logger.LogResult("HealthCheck - OK", "service ok")
return True, "service ok"
health = HealthCheck(checkers=[service_avaliable])
app.add_url_rule("/healthcheck", "healthcheck", view_func=lambda: health.run())
@api.errorhandler(Exception)
def handle_error(e):
ELK_ENABLED = os.environ["ELK_ENABLED"]
if isinstance(ELK_ENABLED, str):
ELK_ENABLED = True if ELK_ENABLED == "True" else False
logger = loggerElk(__name__)
logger.Information("Error Handler")
code = 500
if isinstance(e, HTTPError):
code = e.code
logger.Error(str(e), sys.exc_info())
return {'message': 'Something went wrong: ' + str(e)}, code