Feedback

Chat Icon

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

16%

Setting Up the Foundation: The Application

SSH into the workspace server and start by installing a virtual environment manager. We are going to use virtualenvwrapper for this guide:

apt update

# Install pip
apt install python3-pip -y

# Install virtualenvwrapper
pip3 install virtualenvwrapper --break-system-packages

After installing virtualenvwrapper, you need to add some lines to your .bashrc file to load the virtualenvwrapper commands automatically:

cat <>~/.bashrc && source ~/.bashrc
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
EOF

Create a file tree for the application containing a folder called RestQR with two subfolders: menu and qr:

mkdir -p $HOME/RestQR/{menu,qr}

Now create two virtual environments for the microservices:

mkvirtualenv menu
mkvirtualenv qr

Activate the menu virtual environment and install the required packages:

# Activate the virtual environment
workon menu

# Install the required packages
pip install \
    flask==3.1.0 \
    psycopg2-binary==2.9.10 \
    flask_sqlalchemy==3.1.1

Create the requirements file for the menu service:

pip freeze > $HOME/RestQR/menu/requirements.txt

Use the following command to create a file called app.py in the menu folder:

cat <<EOF >$HOME/RestQR/menu/app.py
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)

# Database Configuration
app.config["SQLALCHEMY_DATABASE_URI"] =  os.environ["DATABASE_URL"]

db = SQLAlchemy(app)

# Database Model
class Menu(db.Model):
    restaurant_id = db.Column(db.String(50), primary_key=True)
    menu_data = db.Column(db.JSON, nullable=False)

# Create tables if they don't exist
with app.app_context():
    db.create_all()

# Save or update a restaurant menu
@app.route("/menu/", methods=["POST"])
def save_menu(restaurant_id):
    menu = request.json
    existing_menu = Menu.query.get(restaurant_id)

    if existing_menu:
        existing_menu.menu_data = menu
    else:
        new_menu = Menu(restaurant_id=restaurant_id, menu_data=menu)
        db.session.add(new_menu)

    db.session.commit()
    return jsonify({"message": "Menu saved successfully"})

# Get a restaurant menu
@app.route

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

Enroll now to unlock all content and receive all future updates for free.