diff --git a/README.md b/README.md new file mode 100644 index 0000000..56374b3 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Flask Project Boilerplate + + +#### deployment using gunicorn, nginx + + + +#### TODOs +1. [ ] read configs from .env +2. [ ] docker \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..1581b08 --- /dev/null +++ b/app.py @@ -0,0 +1,6 @@ +from flask_boilerplate import create_app + +app = create_app() + +if __name__ == "__main__": + app.run(debug=True, threaded=True) diff --git a/config.py b/config.py new file mode 100644 index 0000000..847d35f --- /dev/null +++ b/config.py @@ -0,0 +1,14 @@ +class Config: + DEBUG = True + + +class Development(Config): + pass + + +class Testing(Config): + pass + + +class Production(Config): + DEBUG = False diff --git a/flask_boilerplate/__init__.py b/flask_boilerplate/__init__.py new file mode 100644 index 0000000..bd7c38a --- /dev/null +++ b/flask_boilerplate/__init__.py @@ -0,0 +1,13 @@ +from flask import Flask + + +def create_app(): + app = Flask(__name__) + app.config.from_pyfile('../config.py') + + from .views.api import api + from .views.error_handler import error_handler + app.register_blueprint(api) + app.register_blueprint(error_handler) + + return app diff --git a/flask_boilerplate/middlewares.py b/flask_boilerplate/middlewares.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_boilerplate/views/api.py b/flask_boilerplate/views/api.py new file mode 100644 index 0000000..037911b --- /dev/null +++ b/flask_boilerplate/views/api.py @@ -0,0 +1,12 @@ +import json +import logging + +from flask import Response, Blueprint + +api = Blueprint("api", import_name=__name__) +logger = logging.getLogger(__name__) + + +@api.route("/test") +def test(): + return Response(json.dumps({"message": "success"}), status=200, mimetype='application/json') diff --git a/flask_boilerplate/views/error_handler.py b/flask_boilerplate/views/error_handler.py new file mode 100644 index 0000000..cf9f9f8 --- /dev/null +++ b/flask_boilerplate/views/error_handler.py @@ -0,0 +1,39 @@ +import json + +from flask import Blueprint +from flask import Response + +error_handler = Blueprint('error_handler', import_name=__name__) + + +@error_handler.errorhandler(404) +def page_not_found(e): + return Response( + json.dumps({ + 'status': False, + 'message': "page not found", + }), + mimetype='application/json' + ), 404 + + +@error_handler.errorhandler(500) +def internal_server_error(e): + return Response( + json.dumps({ + 'status': False, + 'message': "internal server error", + }), + mimetype='application/json' + ), 500 + + +@error_handler.errorhandler(429) +def too_many_requests_error(e): + return Response( + json.dumps({ + 'status': False, + 'message': "Sorry, you are making too many requests to our servers", + }), + mimetype='application/json' + ), 429 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f163f4d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +gunicorn \ No newline at end of file diff --git a/scripts/create_venv.sh b/scripts/create_venv.sh new file mode 100644 index 0000000..2588d98 --- /dev/null +++ b/scripts/create_venv.sh @@ -0,0 +1 @@ +# generates gunicorn.service and nginx.conf file \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 0000000..6e0a96b --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,5 @@ +sudo systemctl restart nginx +sudo systemctl restart gunicorn + +sudo systemctl status nginx +sudo systemctl status gunicorn \ No newline at end of file diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..6cddf52 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,5 @@ +sudo systemctl start nginx +sudo systemctl start gunicorn + +sudo systemctl status nginx +sudo systemctl status gunicorn \ No newline at end of file diff --git a/scripts/status.sh b/scripts/status.sh new file mode 100644 index 0000000..e6499eb --- /dev/null +++ b/scripts/status.sh @@ -0,0 +1,2 @@ +sudo systemctl status nginx +sudo systemctl status gunicorn \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100644 index 0000000..82b9ac4 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,5 @@ +sudo systemctl stop nginx +sudo systemctl stop gunicorn + +sudo systemctl status nginx +sudo systemctl status gunicorn \ No newline at end of file