Skip to content

Commit 5e1cec2

Browse files
committed
Updated to latest code without redis
1 parent f11ebf9 commit 5e1cec2

File tree

9 files changed

+157
-229
lines changed

9 files changed

+157
-229
lines changed

Dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN useradd -m -r service && \
1616
chown -R service:service /app
1717
USER service
1818

19-
# Run the service on port 8080
20-
EXPOSE 8080
21-
CMD ["gunicorn", "service:app", "--bind", "0.0.0.0:8080"]
19+
# Run the service on port 8000
20+
ENV PORT 8000
21+
EXPOSE $PORT
22+
CMD ["gunicorn", "service:app", "--bind", "0.0.0.0:8000"]

requirements.txt

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Werkzeug==2.1.2
44
# Runtime dependencies
55
Flask==2.1.2
66
python-dotenv==0.20.0
7-
redis==3.5.3
87

98
# Runtime tools
109
gunicorn==20.1.0
@@ -18,7 +17,6 @@ black==22.3.0
1817
# Testing dependencies
1918
nose==1.3.7
2019
pinocchio==0.4.3
21-
factory-boy==2.12.0
2220
coverage==6.3.2
2321
codecov==2.1.12
2422

service/__init__.py

+6-37
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
1-
# Copyright 2016, 2021 John J. Rofrano. All Rights Reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# https://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
151
"""
16-
Package: service
17-
Package for the application models and service routes
18-
This module creates and configures the Flask app and sets up the logging
19-
and SQL database
2+
Service Package
203
"""
214
from flask import Flask
225

23-
# Create Flask application
246
app = Flask(__name__)
25-
# app.config.from_object("config")
267

27-
# Import the routes AFTER the Flask app is created
28-
# pylint: disable=wrong-import-position, cyclic-import
29-
from service import routes
30-
from service.utils import log_handler
8+
# This must be imported after the Flask app is created
9+
from service import routes # pylint: disable=wrong-import-position,cyclic-import
10+
from service.common import log_handlers # pylint: disable=wrong-import-position
3111

32-
# Set up logging for production
33-
print(f"Setting up logging for {__name__}...")
34-
log_handler.initialize_logging()
12+
log_handlers.init_logging(app, "gunicorn.error")
3513

3614
app.logger.info(70 * "*")
37-
app.logger.info(" H I T C O U N T E R S E R V I C E ".center(70, "*"))
15+
app.logger.info(" S E R V I C E R U N N I N G ".center(70, "*"))
3816
app.logger.info(70 * "*")
39-
40-
# try:
41-
# models.init_db(app) # make our sqlalchemy tables
42-
# except Exception as error:
43-
# app.logger.critical("%s: Cannot continue", error)
44-
# # gunicorn requires exit code 4 to stop spawning workers when they die
45-
# sys.exit(4)
46-
47-
app.logger.info("Service initialized!")

service/utils/error_handlers.py renamed to service/common/error_handlers.py

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copyright 2016, 2021 John J. Rofrano. All Rights Reserved.
1+
######################################################################
2+
# Copyright 2016, 2022 John J. Rofrano. All Rights Reserved.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -11,6 +12,7 @@
1112
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1213
# See the License for the specific language governing permissions and
1314
# limitations under the License.
15+
######################################################################
1416

1517
"""
1618
Module: error_handlers
@@ -23,6 +25,19 @@
2325
######################################################################
2426
# Error Handlers
2527
######################################################################
28+
@app.errorhandler(status.HTTP_400_BAD_REQUEST)
29+
def bad_request(error):
30+
"""Handles bad requests with 400_BAD_REQUEST"""
31+
message = str(error)
32+
app.logger.warning(message)
33+
return (
34+
jsonify(
35+
status=status.HTTP_400_BAD_REQUEST, error="Bad Request", message=message
36+
),
37+
status.HTTP_400_BAD_REQUEST,
38+
)
39+
40+
2641
@app.errorhandler(status.HTTP_404_NOT_FOUND)
2742
def not_found(error):
2843
"""Handles resources not found with 404_NOT_FOUND"""
@@ -49,31 +64,46 @@ def method_not_supported(error):
4964
)
5065

5166

52-
@app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
53-
def internal_server_error(error):
54-
"""Handles unexpected server error with 500_SERVER_ERROR"""
67+
@app.errorhandler(status.HTTP_409_CONFLICT)
68+
def resource_conflict(error):
69+
"""Handles resource conflicts with HTTP_409_CONFLICT"""
5570
message = str(error)
56-
app.logger.error(message)
71+
app.logger.warning(message)
5772
return (
5873
jsonify(
59-
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
60-
error="Internal Server Error",
74+
status=status.HTTP_409_CONFLICT,
75+
error="Conflict",
6176
message=message,
6277
),
63-
status.HTTP_500_INTERNAL_SERVER_ERROR,
78+
status.HTTP_409_CONFLICT,
6479
)
6580

6681

67-
@app.errorhandler(status.HTTP_503_SERVICE_UNAVAILABLE)
68-
def service_unavailable(error):
69-
"""Handles service not available HTTP methods with HTTP_503_SERVICE_UNAVAILABLE"""
82+
@app.errorhandler(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
83+
def mediatype_not_supported(error):
84+
"""Handles unsupported media requests with 415_UNSUPPORTED_MEDIA_TYPE"""
7085
message = str(error)
7186
app.logger.warning(message)
7287
return (
7388
jsonify(
74-
status=status.HTTP_503_SERVICE_UNAVAILABLE,
75-
error="Service not available",
89+
status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
90+
error="Unsupported media type",
91+
message=message,
92+
),
93+
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
94+
)
95+
96+
97+
@app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
98+
def internal_server_error(error):
99+
"""Handles unexpected server error with 500_SERVER_ERROR"""
100+
message = str(error)
101+
app.logger.error(message)
102+
return (
103+
jsonify(
104+
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
105+
error="Internal Server Error",
76106
message=message,
77107
),
78-
status.HTTP_503_SERVICE_UNAVAILABLE,
108+
status.HTTP_500_INTERNAL_SERVER_ERROR,
79109
)

service/utils/log_handler.py renamed to service/common/log_handlers.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
######################################################################
2-
# Copyright 2016, 2021 John J. Rofrano. All Rights Reserved.
2+
# Copyright 2016, 2022 John J. Rofrano. All Rights Reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -15,24 +15,20 @@
1515
######################################################################
1616

1717
"""
18-
Log Handler module
19-
"""
18+
Log Handlers
2019
20+
This module contains utility functions to set up logging
21+
consistently
22+
"""
2123
import logging
22-
from service import app
2324

2425

25-
############################################################
26-
# set up logging for Flask applications
27-
############################################################
28-
def initialize_logging(log_level=None):
29-
gunicorn_logger = logging.getLogger("gunicorn.error")
30-
app.logger.handlers = gunicorn_logger.handlers
31-
if log_level:
32-
app.logger.setLevel(log_level)
33-
else:
34-
app.logger.setLevel(gunicorn_logger.level)
26+
def init_logging(app, logger_name: str):
27+
"""Set up logging for production"""
3528
app.logger.propagate = False
29+
gunicorn_logger = logging.getLogger(logger_name)
30+
app.logger.handlers = gunicorn_logger.handlers
31+
app.logger.setLevel(gunicorn_logger.level)
3632
# Make all log formats consistent
3733
formatter = logging.Formatter(
3834
"[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s", "%Y-%m-%d %H:%M:%S %z"

service/utils/status.py renamed to service/common/status.py

-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
# coding: utf8
2-
3-
######################################################################
4-
# Copyright 2016, 2021 John J. Rofrano. All Rights Reserved.
5-
#
6-
# Licensed under the Apache License, Version 2.0 (the "License");
7-
# you may not use this file except in compliance with the License.
8-
# You may obtain a copy of the License at
9-
#
10-
# https://www.apache.org/licenses/LICENSE-2.0
11-
#
12-
# Unless required by applicable law or agreed to in writing, software
13-
# distributed under the License is distributed on an "AS IS" BASIS,
14-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
# See the License for the specific language governing permissions and
16-
# limitations under the License.
17-
######################################################################
18-
191
"""
202
Descriptive HTTP status codes, for code readability.
213
See RFC 2616 and RFC 6585.

0 commit comments

Comments
 (0)