Skip to content

Commit 8044754

Browse files
Merge pull request #1 from ProgrammingMuffin/create-flask-application
Create basic flask application
2 parents 299a008 + aa5704d commit 8044754

18 files changed

+384
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*/__pycache__

Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM ubuntu:focal
2+
3+
RUN apt update
4+
RUN apt -y install python3 pip mysql-client
5+
6+
RUN mkdir /www
7+
RUN mkdir /www/app
8+
9+
COPY ./ /www/app/
10+
11+
RUN pip install -r /www/app/requirements.txt
12+
13+
RUN ls /www/app
14+
15+
ENTRYPOINT sh /www/app/docker-entrypoint.sh

alembic.ini

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic/
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# sys.path path, will be prepended to sys.path if present.
11+
# defaults to the current working directory.
12+
prepend_sys_path = .
13+
14+
# timezone to use when rendering the date within the migration file
15+
# as well as the filename.
16+
# If specified, requires the python-dateutil library that can be
17+
# installed by adding `alembic[tz]` to the pip requirements
18+
# string value is passed to dateutil.tz.gettz()
19+
# leave blank for localtime
20+
# timezone =
21+
22+
# max length of characters to apply to the
23+
# "slug" field
24+
# truncate_slug_length = 40
25+
26+
# set to 'true' to run the environment during
27+
# the 'revision' command, regardless of autogenerate
28+
# revision_environment = false
29+
30+
# set to 'true' to allow .pyc and .pyo files without
31+
# a source .py file to be detected as revisions in the
32+
# versions/ directory
33+
# sourceless = false
34+
35+
# version location specification; This defaults
36+
# to alembic//versions. When using multiple version
37+
# directories, initial revisions must be specified with --version-path.
38+
# The path separator used here should be the separator specified by "version_path_separator"
39+
# version_locations = %(here)s/bar:%(here)s/bat:alembic//versions
40+
41+
# version path separator; As mentioned above, this is the character used to split
42+
# version_locations. Valid values are:
43+
#
44+
# version_path_separator = :
45+
# version_path_separator = ;
46+
# version_path_separator = space
47+
version_path_separator = os # default: use os.pathsep
48+
49+
# the output encoding used when revision files
50+
# are written from script.py.mako
51+
# output_encoding = utf-8
52+
53+
sqlalchemy.url = mysql+pymysql://root:password@mysql-clusterip/taskmanager
54+
55+
56+
[post_write_hooks]
57+
# post_write_hooks defines scripts or Python functions that are run
58+
# on newly generated revision scripts. See the documentation for further
59+
# detail and examples
60+
61+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
62+
# hooks = black
63+
# black.type = console_scripts
64+
# black.entrypoint = black
65+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
66+
67+
# Logging configuration
68+
[loggers]
69+
keys = root,sqlalchemy,alembic
70+
71+
[handlers]
72+
keys = console
73+
74+
[formatters]
75+
keys = generic
76+
77+
[logger_root]
78+
level = WARN
79+
handlers = console
80+
qualname =
81+
82+
[logger_sqlalchemy]
83+
level = WARN
84+
handlers =
85+
qualname = sqlalchemy.engine
86+
87+
[logger_alembic]
88+
level = INFO
89+
handlers =
90+
qualname = alembic
91+
92+
[handler_console]
93+
class = StreamHandler
94+
args = (sys.stderr,)
95+
level = NOTSET
96+
formatter = generic
97+
98+
[formatter_generic]
99+
format = %(levelname)-5.5s [%(name)s] %(message)s
100+
datefmt = %H:%M:%S

alembic/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

alembic/env.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from logging.config import fileConfig
2+
3+
from sqlalchemy import engine_from_config
4+
from sqlalchemy import pool
5+
6+
from alembic import context
7+
8+
# this is the Alembic Config object, which provides
9+
# access to the values within the .ini file in use.
10+
config = context.config
11+
12+
# Interpret the config file for Python logging.
13+
# This line sets up loggers basically.
14+
fileConfig(config.config_file_name)
15+
16+
# add your model's MetaData object here
17+
# for 'autogenerate' support
18+
# from myapp import mymodel
19+
# target_metadata = mymodel.Base.metadata
20+
target_metadata = None
21+
22+
# other values from the config, defined by the needs of env.py,
23+
# can be acquired:
24+
# my_important_option = config.get_main_option("my_important_option")
25+
# ... etc.
26+
27+
28+
def run_migrations_offline():
29+
"""Run migrations in 'offline' mode.
30+
31+
This configures the context with just a URL
32+
and not an Engine, though an Engine is acceptable
33+
here as well. By skipping the Engine creation
34+
we don't even need a DBAPI to be available.
35+
36+
Calls to context.execute() here emit the given string to the
37+
script output.
38+
39+
"""
40+
url = config.get_main_option("sqlalchemy.url")
41+
context.configure(
42+
url=url,
43+
target_metadata=target_metadata,
44+
literal_binds=True,
45+
dialect_opts={"paramstyle": "named"},
46+
)
47+
48+
with context.begin_transaction():
49+
context.run_migrations()
50+
51+
52+
def run_migrations_online():
53+
"""Run migrations in 'online' mode.
54+
55+
In this scenario we need to create an Engine
56+
and associate a connection with the context.
57+
58+
"""
59+
connectable = engine_from_config(
60+
config.get_section(config.config_ini_section),
61+
prefix="sqlalchemy.",
62+
poolclass=pool.NullPool,
63+
)
64+
65+
with connectable.connect() as connection:
66+
context.configure(
67+
connection=connection, target_metadata=target_metadata
68+
)
69+
70+
with context.begin_transaction():
71+
context.run_migrations()
72+
73+
74+
if context.is_offline_mode():
75+
run_migrations_offline()
76+
else:
77+
run_migrations_online()

alembic/script.py.mako

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""create lists subsystem
2+
3+
Revision ID: fb82c06d1fa2
4+
Revises:
5+
Create Date: 2021-10-09 08:37:34.878611
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'fb82c06d1fa2'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
op.create_table("lists",
21+
sa.Column("id", sa.BIGINT, primary_key=True),
22+
sa.Column("name", sa.Text, nullable=False),
23+
sa.Column("recurring_deadline", sa.Time, nullable=False),
24+
sa.Column("created_at", sa.DateTime, nullable=False),
25+
sa.Column("modified_at", sa.DateTime, nullable=False)
26+
)
27+
28+
29+
def downgrade():
30+
op.drop_table("lists")
31+

config.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql:////root:password@mysql-clusterip/taskmanager'

deploy.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
kubectl delete deployments taskmanager-deployment mysql-deployment
2+
3+
minikube image rm taskmanager
4+
5+
sudo docker build . -t taskmanager
6+
7+
minikube image load taskmanager
8+
9+
kubectl apply -f mysql-deployment.yaml
10+
11+
sleep 25
12+
13+
kubectl apply -f taskmanager-deployment.yaml

docker-entrypoint.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
run_migration() {
2+
cd /www/app
3+
alembic upgrade head
4+
}
5+
6+
run_taskmanager_application() {
7+
python3 /www/app/main.py
8+
}
9+
10+
run_migration
11+
run_taskmanager_application

docs/taskmanager-dbdiagram.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile host="app.diagrams.net" modified="2021-10-04T18:50:34.385Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" etag="2aQIa0IshziU_Q4f-3TO" version="15.4.1" type="device"><diagram id="R2lEEEUBdFMjLlhIrx00" name="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EDT340L1KHdzKU6wD+0g356GxvCw3AM01Q5h+WEjEkE8HVm7Wa/nyuky8r0yUpz0Z3/gYwt5QQrMGfE2vpNXS4OXf6dgkDs/BA750G6e7XYPHi1lHvJvYobLJRbmmIyp0P42rCflGPp+FToKQVLLCnEgKHn7Rs5fmzGN/K8Kk/RE3tZefhu9iPZSWfLftncu/4P</diagram></mxfile>

lists/__init__.py

Whitespace-only changes.

lists/models.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from main import db
2+
import flask_sqlalchemy as sa
3+
4+
class List(db.Model):
5+
''' This class represents a list '''
6+

lists/views.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Blueprint
2+
3+
4+
lists = Blueprint("list_controller", __name__)
5+
6+
@lists.route("", methods=["POST"])
7+
def createList():
8+
return "created a list"
9+

main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
from lists.views import lists
5+
6+
7+
app = Flask(__name__)
8+
app.config.from_pyfile(os.path.join('.', 'config.py'))
9+
db = SQLAlchemy(app)
10+
11+
app.register_blueprint(lists, url_prefix="/lists")
12+
13+
if __name__=='__main__':
14+
app.run(host='0.0.0.0', port=5000, debug=True)
15+

mysql-deployment.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: mysql-deployment
6+
spec:
7+
selector:
8+
matchLabels:
9+
database: mysql
10+
replicas: 1
11+
template:
12+
metadata:
13+
name: mysql
14+
labels:
15+
database: mysql
16+
spec:
17+
containers:
18+
- name: mysql
19+
image: mysql:8.0
20+
ports:
21+
- containerPort: 3306
22+
env:
23+
- name: MYSQL_ROOT_PASSWORD
24+
value: password
25+
- name: MYSQL_DATABASE
26+
value: taskmanager
27+
---
28+
apiVersion: v1
29+
kind: Service
30+
metadata:
31+
name: mysql-clusterip
32+
spec:
33+
selector:
34+
database: mysql
35+
ports:
36+
- port: 3306
37+
targetPort: 3306
38+
type: ClusterIP
39+
---

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Flask == 2.0.1
2+
Flask_SQLAlchemy == 2.5.1
3+
PyMySQL == 1.0.2
4+
alembic == 1.7.4
5+
cryptography == 35.0.0

0 commit comments

Comments
 (0)