Skip to content

Commit

Permalink
add demo example
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Jul 5, 2024
1 parent 9423731 commit b34b955
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 1 deletion.
35 changes: 35 additions & 0 deletions examples/voting-app/load-generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import requests
import time

# The URL to send the POST requests to
host = "prod.app.localhost"
url = "http://127.0.0.1/"

# Headers to be included in the POST requests
headers = {
"Origin": f"http://{host}",
"Host": host,
}

# Data to be sent in the POST requests
data_options = ["option1", "option2"]
data_index = 0


# Function to send a burst of 5 POST requests
def send_burst(data):
print(f"New burst of {data}")
for _ in range(5):
response = None
try:
response = requests.post(url, headers=headers, data={"vote": data})
print(f"Sent '{data}' - Response status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error sending '{data}' - {e}")


# Send bursts of 5 POST requests every 5 seconds, alternating between 'Cats' and 'Dogs'
while True:
send_burst(data_options[data_index])
data_index = (data_index + 1) % 2
time.sleep(5)
27 changes: 27 additions & 0 deletions examples/voting-app/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{pkgs, ...}: let
pyEnv = pkgs.python3.buildEnv.override {
extraLibs = [pkgs.python3Packages.click pkgs.python3Packages.requests];
ignoreCollisions = true;
};

pname = "demo-load-generator";
demo-load-genarator = pkgs.stdenv.mkDerivation {
inherit pname;
version = "1.0.0";

src = ./.;

installPhase = ''
mkdir -p $out/bin
echo "#!${pyEnv}/bin/python3" > $out/bin/${pname}
cat load-generator.py >> $out/bin/${pname}
chmod +x $out/bin/${pname}
'';
};
in
pkgs.mkShell {
buildInputs = [
demo-load-genarator
pyEnv
];
}
12 changes: 12 additions & 0 deletions examples/voting-app/voting-app-ui/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN pip install Flask redis

EXPOSE 5000

CMD ["python", "app.py"]

14 changes: 14 additions & 0 deletions examples/voting-app/voting-app-ui/Dockerfile-v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN pip install Flask redis

ENV APP_VERSION v2

EXPOSE 5000

CMD ["python", "app.py"]

94 changes: 94 additions & 0 deletions examples/voting-app/voting-app-ui/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from flask import Flask, render_template, request, redirect, url_for
import redis
import os

app = Flask(__name__)

redis_server = os.environ["REDIS"]

# Initialize Redis
r = redis.Redis(host=redis_server, port=6379)

# Getting app version
if "APP_VERSION" in os.environ and os.environ["APP_VERSION"]:
app_version = os.environ["APP_VERSION"]
else:
app_version = "v1"

print("app_version is: " + app_version)

if "OPTION1" in os.environ and os.environ["OPTION1"]:
option1 = os.environ["OPTION1"]
else:
option1 = "Option 1"

if "OPTION2" in os.environ and os.environ["OPTION2"]:
option2 = os.environ["OPTION2"]
else:
option2 = "Option 2"

if "OPTION3" in os.environ and os.environ["OPTION3"] and app_version != "v1":
option3 = os.environ["OPTION3"]
elif app_version != "v1":
option3 = "Option 3"

if "TITLE" in os.environ and os.environ["TITLE"]:
title = os.environ["TITLE"]
else:
title = "Vote For Your Favorite Option"

# Set up initial vote counts
# TODO: implement this on redis proxy
if not r.exists("option1"):
r.set("option1", 0)
if not r.exists("option2"):
r.set("option2", 0)

if app_version == "v1":
if not r.exists("option3"):
r.set("option3", 0)


@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
vote = request.form["vote"]
if vote == "option1":
r.incr("option1")
elif vote == "option2":
r.incr("option2")
elif vote == "option3" and app_version != "v1":
r.incr("option3")
return redirect(url_for("index"))

# Get current vote counts
option1_votes = int(r.get("option1") or 0)
option2_votes = int(r.get("option2") or 0)
if app_version != "v1":
option3_votes = int(r.get("option3") or 0)

if app_version != "v1":
return render_template(
"index.html",
option1_votes=option1_votes,
option2_votes=option2_votes,
option3_votes=option3_votes,
title=title,
option1=option1,
option2=option2,
option3=option3,
)
else:
return render_template(
"index.html",
option1_votes=option1_votes,
option2_votes=option2_votes,
title=title,
option1=option1,
option2=option2,
)



if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)
142 changes: 142 additions & 0 deletions examples/voting-app/voting-app-ui/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voting App</title>
</head>
<style>
body {
background-color: #fff;
}

div#logo {
isplay: block;
margin: 0 auto;
width: 500px;
text-align: center;
font-size: 30px;
font-family: Helvetica;
}

div#container {
margin-top: 5%;
}

div.space {
display: block;
margin: 0 auto;
width: 500px;
height: 10px;
}

div#form {
padding: 40px;
padding-right: 20px;
padding-top: 20px;
display: block;
margin: 0 auto;
width: 500px;
text-align: center;
font-size: 30px;
font-family: Helvetica;
border-bottom: 1px solid black;
border-top: 1px solid black;
}

div#results {
display: block;
margin: 0 auto;
width: 500px;
text-align: center;
font-size: 26px;
font-family: Helvetica;
color: #8d8d8d;
}

div#result-title {
font-size: 26px;
margin-bottom: 20px;
margin-top: 20px;
color: #8d8d8d;
}

.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s;
transition-duration: 0.4s;
cursor: pointer;
width: 250px;
}

.button1 {
background-color: white;
color: black;
border: 2px solid #008CBA;
}

.button1:hover {
background-color: #008CBA;
color: white;
}

.button2 {
background-color: white;
color: black;
border: 2px solid #18a42d;
}

.button2:hover {
background-color: #18a42d;
color: white;
}

.button3 {
background-color: white;
color: black;
border: 2px solid #f44336;
}

.button3:hover {
background-color: #f44336;
color: white;
}
</style>
<body>
<div id="container">
<div id="logo">{{ title }}</div>
<div class="space"></div>
<div class="space"></div>
<div id="form">
<form action="/" method="POST">
<center>
<button type="submit" name="vote" value="option1" class="button button1">Vote for {{ option1 }}</button>
<button type="submit" name="vote" value="option2" class="button button2">Vote for {{ option2 }}</button>
{% if option3 %}
<button type="submit" name="vote" value="option3" class="button button3">Vote for {{ option3 }}</button>
{% endif %}
</center>
</form>
<div class="space"></div>
<div class="space"></div>
<div id="result-title">Current votes</div>
<div id="results">
{{ option1 }}: {{ option1_votes }} |
{{ option2 }}: {{ option2_votes }}
{% if option3 %}
|
{{ option3 }}: {{ option3_votes }}
{% endif %}
</div>
</div>
</div>
</body>
</html>
10 changes: 9 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
manager_shell = pkgs.callPackage ./kardinal-manager/shell.nix {inherit pkgs;};
cli_shell = pkgs.callPackage ./kardinal-cli/shell.nix {inherit pkgs;};
cli_kontrol_api_shell = pkgs.callPackage ./libs/cli-kontrol-api/shell.nix {inherit pkgs;};
demo_shell = pkgs.callPackage ./examples/voting-app/shell.nix {inherit pkgs;};

kardinal_shell = with pkgs;
pkgs.mkShell {
nativeBuildInputs = [bashInteractive bash-completion];
Expand Down Expand Up @@ -64,4 +66,10 @@
'';
};
in
mergeShells [manager_shell cli_shell kardinal_shell cli_kontrol_api_shell]
mergeShells [
manager_shell
cli_shell
kardinal_shell
cli_kontrol_api_shell
demo_shell
]

0 comments on commit b34b955

Please sign in to comment.