Skip to content

Commit

Permalink
Added logging for Application Insights
Browse files Browse the repository at this point in the history
  • Loading branch information
hvalfangst committed Jan 4, 2024
1 parent 298996a commit 60e2f5e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions flask_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from flask import Flask, jsonify, request
import logging

from flask_api.crypto_utils.module import aes_encrypt, aes_decrypt

app = Flask(__name__)

# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


@app.route("/")
def index():
logger.info("Received request to index endpoint.")
return (
"Welcome to Hvalfangst Crypto API!\n\n"
"Available Endpoints:\n"
Expand All @@ -19,12 +25,16 @@ def index():

@app.route("/encrypt", methods=["POST"])
def encrypt():
logger.info("Received request to encrypt endpoint.")

# Extract data from JSON body
request_data = request.get_json()

# Check if 'plain_text' and 'encryption_key' fields are present in the JSON
if "plain_text" not in request_data or "encryption_key" not in request_data:
return jsonify({"error": "Missing 'plain_text' or 'encryption_key' in the request body"}), 400
error_msg = "Missing 'plain_text' or 'encryption_key' in the request body"
logger.error(error_msg)
return jsonify({"error": error_msg}), 400

plain_text = request_data["plain_text"]
encryption_key = request_data["encryption_key"]
Expand All @@ -39,18 +49,24 @@ def encrypt():
"encrypted_text": encrypted_text
}

logger.info("Encryption successful.")

# Return JSON response
return jsonify(response)


@app.route("/decrypt", methods=["POST"])
def decrypt():
logger.info("Received request to decrypt endpoint.")

# Extract data from JSON body
request_data = request.get_json()

# Check if 'encrypted_text' and 'encryption_key' fields are present in the JSON
if "encrypted_text" not in request_data or "encryption_key" not in request_data:
return jsonify({"error": "Missing 'encrypted_text' or 'encryption_key' in the request body"}), 400
error_msg = "Missing 'encrypted_text' or 'encryption_key' in the request body"
logger.error(error_msg)
return jsonify({"error": error_msg}), 400

encrypted_text = request_data["encrypted_text"]
encryption_key = request_data["encryption_key"]
Expand All @@ -65,9 +81,12 @@ def decrypt():
"plain_text": plain_text
}

logger.info("Decryption successful.")

# Return JSON response
return jsonify(response)


if __name__ == "__main__":
app.run()
# Run the application with the built-in development server
app.run()

0 comments on commit 60e2f5e

Please sign in to comment.