Skip to content

Commit 11859f8

Browse files
committed
Create new flask-openai sample app
1 parent f10d86b commit 11859f8

9 files changed

Lines changed: 1073 additions & 0 deletions

File tree

sample-apps/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ Overview :
4141
- `flask-clickhouse-uwsgi/` is a Flask UWSGI app using Clickhouse
4242
- It runs **multi-threaded**
4343
- Runs on 8106. Without Aikido runs on 8107
44+
- `flask-openai/` is a Flask app with openai
45+
- it runs **multi-threaded**
46+
- Runs on 8108. Without Aikido runs on 8109

sample-apps/flask-openai/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.PHONY: install
2+
install:
3+
poetry install
4+
5+
.PHONY: run
6+
run: install
7+
@echo "Running sample app flask-openai with Zen on port 8108"
8+
AIKIDO_DEBUG=true AIKIDO_BLOCK=true AIKIDO_TOKEN="AIK_secret_token" \
9+
AIKIDO_REALTIME_ENDPOINT="http://localhost:5000/" \
10+
AIKIDO_ENDPOINT="http://localhost:5000/" AIKIDO_DISABLE=0 \
11+
poetry run flask --app app.py run --host=0.0.0.0 --port=8108 --no-reload
12+
13+
.PHONY: runZenDisabled
14+
runZenDisabled: install
15+
@echo "Running sample app flask-openai without Zen on port 8109"
16+
AIKIDO_DISABLE=1 \
17+
poetry run flask --app app.py run --host=0.0.0.0 --port=8109 --no-reload

sample-apps/flask-openai/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Flask + OpenAI + Postgres
2+
It runs **multi-threaded**
3+
4+
## Getting started
5+
Run :
6+
```bash
7+
make run # Runs app with zen
8+
make runZenDisabled # Runs app with zen disabled.
9+
```
10+
11+
- You'll be able to access the Flask Server at : [localhost:8108](http://localhost:8090)
12+
- To Create a reference test dog use `http://localhost:8108/create/`
13+
- To Create a reference test dog (with executemany) use `http://localhost:8108/create_many/`
14+
15+
- To test a sql injection enter the following dog name : `Malicious dog', TRUE); -- `

sample-apps/flask-openai/app.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import json
3+
4+
import aikido_zen # Aikido package import
5+
aikido_zen.protect()
6+
7+
from flask import Flask, render_template, request
8+
import psycopg2
9+
10+
app = Flask(__name__)
11+
12+
def get_db_connection():
13+
return psycopg2.connect(
14+
host="localhost",
15+
database="db",
16+
user="user",
17+
password="password")
18+
19+
@app.route("/")
20+
def homepage():
21+
cursor = get_db_connection().cursor()
22+
cursor.execute("SELECT * FROM dogs")
23+
dogs = cursor.fetchall()
24+
return render_template('index.html', title='Homepage', dogs=dogs)
25+
26+
27+
@app.route('/dogpage/<int:dog_id>')
28+
def get_dogpage(dog_id):
29+
cursor = get_db_connection().cursor()
30+
cursor.execute("SELECT * FROM dogs WHERE id = " + str(dog_id))
31+
dog = cursor.fetchmany(1)[0]
32+
return render_template('dogpage.html', title=f'Dog', dog=dog, isAdmin=("Yes" if dog[2] else "No"))
33+
34+
@app.route("/create", methods=['GET'])
35+
def show_create_dog_form():
36+
return render_template('create_dog.html')
37+
38+
@app.route("/create_many", methods=['GET'])
39+
def show_create_dog_form_many():
40+
return render_template('create_dog.html')
41+
42+
@app.route("/create", methods=['POST'])
43+
def create_dog():
44+
dog_name = request.form['dog_name']
45+
conn = get_db_connection()
46+
cursor = conn.cursor()
47+
cursor.execute(f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name))
48+
conn.commit()
49+
cursor.close()
50+
conn.close()
51+
return f'Dog {dog_name} created successfully'
52+
53+
@app.route("/create/:id", methods=["GET"])
54+
@app.route("/create_many", methods=['POST'])
55+
def create_dog_many():
56+
dog_name = request.form['dog_name']
57+
conn = get_db_connection()
58+
cursor = conn.cursor()
59+
cursor.executemany([f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name)], [])
60+
conn.commit()
61+
cursor.close()
62+
conn.close()
63+
return f'Dog {dog_name} created successfully'
64+
65+
@app.route("/create_with_cookie", methods=['GET'])
66+
def create_dog_with_cookie():
67+
dog_name = request.cookies.get('dog_name')
68+
69+
conn = get_db_connection()
70+
cursor = conn.cursor()
71+
cursor.execute(f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name))
72+
conn.commit()
73+
cursor.close()
74+
conn.close()
75+
return f'Dog {dog_name} created successfully'

sample-apps/flask-openai/poetry.lock

Lines changed: 851 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[project]
2+
name = "flask-openai"
3+
version = "0.1.0"
4+
description = ""
5+
readme = "README.md"
6+
requires-python = ">3.9.1,<4.0"
7+
dependencies = [
8+
"flask (>=3.1.0,<4.0.0)",
9+
"psycopg2-binary (>=2.9.10,<3.0.0)",
10+
"cryptography (>=44.0.0,<45.0.0)",
11+
"aikido_zen"
12+
]
13+
14+
[build-system]
15+
requires = ["poetry-core>=2.0.0,<3.0.0"]
16+
build-backend = "poetry.core.masonry.api"
17+
18+
[tool.poetry]
19+
package-mode = false
20+
21+
[tool.poetry.dependencies]
22+
aikido_zen = { path = "../../", develop = true }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Create Dog</title>
8+
</head>
9+
<body>
10+
<h1>Create a Dog</h1>
11+
<form method="post">
12+
<label for="dog_name">Dog Name:</label>
13+
<input type="text" id="dog_name" name="dog_name" required>
14+
<button type="submit">Create Dog</button>
15+
</form>
16+
</body>
17+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{{ title }}</title>
5+
<style>
6+
body {
7+
font-family: sans-serif;
8+
}
9+
h1 {
10+
font-family: monospace;
11+
text-align: center;
12+
border: 1px solid black;
13+
border-left: none;
14+
border-right: none;
15+
margin: 4px;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<h1>{{ title }}</h1>
21+
<p><em>Name :</em> {{dog[1]}}</p>
22+
<p><em>Is admin dog? </em>{{ isAdmin }}</p>
23+
<p><em>ID :</em> {{dog[0]}}</p>
24+
</body>
25+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{{ title }}</title>
5+
<style>
6+
body {
7+
font-family: sans-serif;
8+
}
9+
/* Style for the list */
10+
ul {
11+
list-style-type:disc;
12+
}
13+
14+
/* Style for list items */
15+
li {
16+
margin-bottom: 10px;
17+
}
18+
19+
/* Style for links */
20+
a {
21+
text-decoration: none;
22+
color: #48507f;
23+
}
24+
25+
/* Hover effect for links */
26+
a:hover {
27+
color: #007bff;
28+
}
29+
h1 {
30+
font-family: monospace;
31+
text-align: center;
32+
border: 1px solid black;
33+
border-left: none;
34+
border-right: none;
35+
margin: 4px;
36+
}
37+
</style>
38+
</head>
39+
<body>
40+
<h1>{{ title }}</h1>
41+
<h2>List</h2>
42+
<ul>
43+
{% for dog in dogs %}
44+
<li><a href="/dogpage/{{ dog[0] }}">{{ dog[1] }}</a></li>
45+
{% endfor %}
46+
</ul>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)