Skip to content

Commit 6e85f67

Browse files
authored
Merge pull request #5 from cweidner3/refactor
Refactor
2 parents 1788fb9 + 424b7fa commit 6e85f67

File tree

230 files changed

+12077
-506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+12077
-506
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length=100

.gitignore

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
1-
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1+
__pycache__/
2+
*.pyc
23

3-
# dependencies
4-
/node_modules
5-
/.pnp
6-
.pnp.js
7-
8-
# testing
9-
/coverage
10-
11-
# production
12-
/build
13-
14-
# misc
15-
.DS_Store
16-
.env.local
17-
.env.development.local
18-
.env.test.local
19-
.env.production.local
20-
21-
npm-debug.log*
22-
yarn-debug.log*
23-
yarn-error.log*
4+
/secrets/
5+
/.prod*.yml

Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.PHONY: default
2+
default: up
3+
4+
.PHONY: up
5+
up:
6+
docker-compose up --build -d
7+
8+
.PHONY: down
9+
down:
10+
docker-compose down $(ARGS)
11+
12+
.PHONY: build
13+
build:
14+
docker-compose build
15+
16+
.PHONY: logs
17+
logs:
18+
docker-compose logs -f $(CONT)
19+
20+
.PHONY: status
21+
status:
22+
docker-compose ps
23+
24+
.PHONY: stat
25+
stat: status
26+
27+
.PHONY: localup
28+
localup: .prod-local.yml
29+
docker-compose -f $< up --build -d
30+
31+
.PHONY: localdown
32+
localdown: .prod-local.yml
33+
docker-compose -f $< down $(ARGS)
34+
35+
.PHONY: locallogs
36+
locallogs: .prod-local.yml
37+
docker-compose -f $< logs -f $(CONT)
38+
39+
################################################################################
40+
41+
.PHONY: clean
42+
clean:
43+
rm -rf .prod-local.yml
44+
45+
.prod-local.yml: docker-compose.yml prod-local.yml
46+
@python ./utils/dc_merge.py $@ $^

api/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.coverage
2+
/htmlcov/

api/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.11
2+
3+
RUN apt update -y && apt install -y \
4+
gcc openssl \
5+
mariadb-common libmariadb3 libmariadb-dev \
6+
libpq5 libpq-dev postgresql-common
7+
8+
RUN python -m venv /venv
9+
10+
COPY requirements.txt /requirements.txt
11+
RUN /venv/bin/pip install -r /requirements.txt
12+
13+
COPY ./entrypoint.sh /entrypoint
14+
15+
WORKDIR /app
16+
17+
COPY ./src ./src
18+
19+
EXPOSE 5000
20+
21+
ENTRYPOINT ["/entrypoint"]
22+
CMD ["/venv/bin/python", "-m", "flask", "--app", "src.main", "--debug", "run", "--host", "0.0.0.0"]

api/Dockerfile.prod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM --platform=$BUILDPLATFORM python:3.11
2+
3+
RUN apt update -y && apt install -y \
4+
gcc openssl \
5+
mariadb-common libmariadb3 libmariadb-dev \
6+
libpq5 libpq-dev postgresql-common
7+
8+
RUN python -m venv /venv
9+
10+
RUN groupadd -g 1000 worker \
11+
&& useradd -u 1000 -g 1000 worker \
12+
&& mkdir -p /app \
13+
&& chown 1000:1000 /app
14+
15+
COPY requirements.txt /requirements.txt
16+
RUN /venv/bin/pip install -r /requirements.txt gunicorn
17+
18+
USER worker
19+
WORKDIR /app
20+
21+
COPY ./src ./src
22+
23+
EXPOSE 5000
24+
25+
CMD ["/venv/bin/python", "-m", "gunicorn", "-b", "0.0.0.0:5000", "src.main:app"]

api/Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
REV = head
2+
3+
SRCS = alembic.ini
4+
SRCS += $(shell find src -iname '*.py')
5+
SRCS += $(shell find tests -iname '*.py')
6+
7+
.PHONY: default
8+
default: test
9+
10+
.PHONY: help
11+
help:
12+
@echo "ACTIONS"
13+
@echo " test [default] Run unittests."
14+
@echo " upgrade [REV=] Migrate database upwards to REV [default head]"
15+
@echo " downgrade [REV=] Migrate database downwards to REV."
16+
@echo " testhike Upload test data for viewing the hike page."
17+
@echo " cov Show report of test coverage of the app."
18+
@echo " html Show report of test coverage of the app in html."
19+
20+
.PHONY: test
21+
test:
22+
python -m unittest -v $(TESTS)
23+
24+
.PHONY: upgrade
25+
upgrade:
26+
alembic upgrade $(REV)
27+
28+
.PHONY: downgrade
29+
downgrade:
30+
alembic downgrade $(REV)
31+
32+
.PHONY: testhike
33+
testhike:
34+
python -m tests.scripts.upload_test_hike $(REPLACE)
35+
36+
.PHONY: cov
37+
cov: .coverage
38+
python -m coverage report
39+
40+
.PHONY: html
41+
html: htmlcov
42+
43+
################################################################################
44+
45+
.PHONY: clean
46+
clean:
47+
rm -rf .coverage htmlcov
48+
49+
htmlcov: .coverage
50+
python -m coverage html
51+
52+
.coverage: $(SRCS) Makefile
53+
python -m coverage run --source=src -m unittest -v

api/alembic.ini

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 file names; The default value is %%(rev)s_%%(slug)s
8+
# Uncomment the line below if you want the files to be prepended with date and time
9+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
10+
# for all available tokens
11+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
12+
file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13+
14+
# sys.path path, will be prepended to sys.path if present.
15+
# defaults to the current working directory.
16+
prepend_sys_path = .
17+
18+
# timezone to use when rendering the date within the migration file
19+
# as well as the filename.
20+
# If specified, requires the python-dateutil library that can be
21+
# installed by adding `alembic[tz]` to the pip requirements
22+
# string value is passed to dateutil.tz.gettz()
23+
# leave blank for localtime
24+
# timezone =
25+
26+
# max length of characters to apply to the
27+
# "slug" field
28+
# truncate_slug_length = 40
29+
30+
# set to 'true' to run the environment during
31+
# the 'revision' command, regardless of autogenerate
32+
# revision_environment = false
33+
34+
# set to 'true' to allow .pyc and .pyo files without
35+
# a source .py file to be detected as revisions in the
36+
# versions/ directory
37+
# sourceless = false
38+
39+
# version location specification; This defaults
40+
# to alembic/versions. When using multiple version
41+
# directories, initial revisions must be specified with --version-path.
42+
# The path separator used here should be the separator specified by "version_path_separator" below.
43+
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
44+
version_locations = %(here)s/src/migrations
45+
46+
# version path separator; As mentioned above, this is the character used to split
47+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
48+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
49+
# Valid values for version_path_separator are:
50+
#
51+
# version_path_separator = :
52+
# version_path_separator = ;
53+
# version_path_separator = space
54+
version_path_separator = os
55+
56+
# the output encoding used when revision files
57+
# are written from script.py.mako
58+
# output_encoding = utf-8
59+
60+
sqlalchemy.url = postgresql://postgres:secret@localhost:5432/db
61+
62+
63+
[post_write_hooks]
64+
# post_write_hooks defines scripts or Python functions that are run
65+
# on newly generated revision scripts. See the documentation for further
66+
# detail and examples
67+
68+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
69+
# hooks = black
70+
# black.type = console_scripts
71+
# black.entrypoint = black
72+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
73+
74+
# Logging configuration
75+
[loggers]
76+
keys = root,sqlalchemy,alembic
77+
78+
[handlers]
79+
keys = console
80+
81+
[formatters]
82+
keys = generic
83+
84+
[logger_root]
85+
level = WARN
86+
handlers = console
87+
qualname =
88+
89+
[logger_sqlalchemy]
90+
level = WARN
91+
handlers =
92+
qualname = sqlalchemy.engine
93+
94+
[logger_alembic]
95+
level = INFO
96+
handlers =
97+
qualname = alembic
98+
99+
[handler_console]
100+
class = StreamHandler
101+
args = (sys.stderr,)
102+
level = NOTSET
103+
formatter = generic
104+
105+
[formatter_generic]
106+
format = %(levelname)-5.5s [%(name)s] %(message)s
107+
datefmt = %H:%M:%S

api/alembic/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

api/alembic/env.py

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

0 commit comments

Comments
 (0)