Skip to content

Commit

Permalink
Associated Flask endpoints with their own dedicated Azure Function
Browse files Browse the repository at this point in the history
  • Loading branch information
hvalfangst committed Jan 14, 2024
1 parent 298996a commit 4063397
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python version
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

Expand All @@ -31,7 +31,7 @@ jobs:
run: pip install -r requirements.txt

- name: Zip artifact for deployment
run: zip -r release.zip host.json requirements.txt wsgi_middleware/* flask_api/*
run: zip -r release.zip host.json requirements.txt function_app.py crypto_utils/*

- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
Expand Down
File renamed without changes.
Empty file removed flask_api/crypto_utils/__init__.py
Empty file.
31 changes: 25 additions & 6 deletions flask_api/__init__.py → function_app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from flask import Flask, jsonify, request
import logging

from flask_api.crypto_utils.module import aes_encrypt, aes_decrypt
import azure.functions as func
from flask import Flask, jsonify, request
from crypto_utils 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 +26,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 +50,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 +82,11 @@ def decrypt():
"plain_text": plain_text
}

logger.info("Decryption successful.")

# Return JSON response
return jsonify(response)


if __name__ == "__main__":
app.run()
# Create an Azure Function which serves the above routes in our WSGI runtime (Gunicorn)
app = func.WsgiFunctionApp(app=app.wsgi_app, http_auth_level=func.AuthLevel.ANONYMOUS)
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"info": {
"_postman_id": "edde8ba3-fc1a-43ca-9534-9ff5ec08969e",
"name": "Hvalfangst Azure Function Crypto API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "29899311"
},
"item": [
{
"name": "Index",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://hvalfangstlinuxfunctionapp.azurewebsites.net/index",
"protocol": "https",
"host": [
"hvalfangstlinuxfunctionapp",
"azurewebsites",
"net"
],
"path": [
"index"
]
}
},
"response": []
},
{
"name": "Encrypt",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"plain_text\": \"Goofy on Acid\",\r\n \"encryption_key\": \"ballyr\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://hvalfangstlinuxfunctionapp.azurewebsites.net/encrypt",
"protocol": "https",
"host": [
"hvalfangstlinuxfunctionapp",
"azurewebsites",
"net"
],
"path": [
"encrypt"
]
}
},
"response": []
},
{
"name": "Decrypt",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"encrypted_text\": \"f=\",\r\n \"encryption_key\": \"ballyr\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://hvalfangstlinuxfunctionapp.azurewebsites.net/decrypt",
"protocol": "https",
"host": [
"hvalfangstlinuxfunctionapp",
"azurewebsites",
"net"
],
"path": [
"decrypt"
]
}
},
"response": []
}
]
}
8 changes: 0 additions & 8 deletions wsgi_middleware/__init__.py

This file was deleted.

21 changes: 0 additions & 21 deletions wsgi_middleware/function.json

This file was deleted.

0 comments on commit 4063397

Please sign in to comment.