Skip to content

Commit

Permalink
Rename application package to src from api (#146)
Browse files Browse the repository at this point in the history
In PR #138 we renamed the route package to api, which makes it api.api.
This PR renames the top level package to src to avoid naming issues and
to represent the idea that the application contains other things like
CLI commands / background jobs. We considered naming it app, but that
would conflict with the app folder at the repo root, and it would also
create ambiguity with the flask app variable.
  • Loading branch information
lorenyu authored Feb 22, 2023
1 parent 000bced commit 6a3de8b
Show file tree
Hide file tree
Showing 90 changed files with 169 additions and 169 deletions.
2 changes: 1 addition & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ ENV HOST=0.0.0.0
# https://python-poetry.org/docs/basic-usage/#installing-dependencies
RUN poetry install
# Run the application.
CMD ["poetry", "run", "python", "-m", "api"]
CMD ["poetry", "run", "python", "-m", "src"]
22 changes: 11 additions & 11 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ APP_NAME := main-app
# Note that you can also change the LOG_FORMAT env var to switch
# between JSON & human readable format. This is left in place
# in the event JSON is output from a process we don't log.
DECODE_LOG := 2>&1 | python3 -u api/logging/util/decodelog.py
DECODE_LOG := 2>&1 | python3 -u src/logging/util/decodelog.py

# A few commands need adjustments if they're run in CI, specify those here
# TODO - when CI gets hooked up, actually test this.
ifdef CI
DOCKER_EXEC_ARGS := -T -e CI -e PYTEST_ADDOPTS="--color=yes"
FLAKE8_FORMAT := '::warning file=api/%(path)s,line=%(row)d,col=%(col)d::%(path)s:%(row)d:%(col)d: %(code)s %(text)s'
FLAKE8_FORMAT := '::warning file=src/%(path)s,line=%(row)d,col=%(col)d::%(path)s:%(row)d:%(col)d: %(code)s %(text)s'
MYPY_FLAGS := --no-pretty
MYPY_POSTPROC := | perl -pe "s/^(.+):(\d+):(\d+): error: (.*)/::warning file=api\/\1,line=\2,col=\3::\4/"
MYPY_POSTPROC := | perl -pe "s/^(.+):(\d+):(\d+): error: (.*)/::warning file=src\/\1,line=\2,col=\3::\4/"
else
FLAKE8_FORMAT := default
endif
Expand Down Expand Up @@ -92,7 +92,7 @@ db-recreate: clean-docker-volumes init-db
# DB Migrations
#########################

alembic_config := ./api/db/migrations/alembic.ini
alembic_config := ./src/db/migrations/alembic.ini
alembic_cmd := $(PY_RUN_CMD) alembic --config $(alembic_config)

db-upgrade: ## Apply pending migrations to db
Expand Down Expand Up @@ -139,7 +139,7 @@ test-watch:
$(PY_RUN_CMD) pytest-watch --clear $(args)

test-coverage:
$(PY_RUN_CMD) coverage run --branch --source=api -m pytest -m "not audit" $(args)
$(PY_RUN_CMD) coverage run --branch --source=src -m pytest -m "not audit" $(args)
$(PY_RUN_CMD) coverage report

test-coverage-report: ## Open HTML test coverage report
Expand All @@ -151,22 +151,22 @@ test-coverage-report: ## Open HTML test coverage report
##################################################

format:
$(PY_RUN_CMD) isort --atomic api tests
$(PY_RUN_CMD) black api tests
$(PY_RUN_CMD) isort --atomic src tests
$(PY_RUN_CMD) black src tests

format-check:
$(PY_RUN_CMD) isort --atomic --check-only api tests
$(PY_RUN_CMD) black --check api tests
$(PY_RUN_CMD) isort --atomic --check-only src tests
$(PY_RUN_CMD) black --check src tests

lint: lint-py

lint-py: lint-flake lint-mypy

lint-flake:
$(PY_RUN_CMD) flake8 --format=$(FLAKE8_FORMAT) api tests
$(PY_RUN_CMD) flake8 --format=$(FLAKE8_FORMAT) src tests

lint-mypy:
$(PY_RUN_CMD) mypy --show-error-codes $(MYPY_FLAGS) api $(MYPY_POSTPROC)
$(PY_RUN_CMD) mypy --show-error-codes $(MYPY_FLAGS) src $(MYPY_POSTPROC)

lint-security: # https://bandit.readthedocs.io/en/latest/index.html
$(PY_RUN_CMD) bandit -c pyproject.toml -r . --number 3 --skip B101 -ll -x ./.venv
Expand Down
4 changes: 2 additions & 2 deletions app/local.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Local environment variables
# Used by docker-compose and it can be loaded
# by calling load_local_env_vars() from app/api/util/local.py
# by calling load_local_env_vars() from app/src/util/local.py

ENVIRONMENT=local
PORT=8080
Expand All @@ -15,7 +15,7 @@ PYTHONPATH=/app/
# commands that can run in or out
# of the Docker container - defaults to outside

FLASK_APP=api.app:create_app
FLASK_APP=src.app:create_app

############################
# Logging
Expand Down
14 changes: 7 additions & 7 deletions app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "template-application-flask"
version = "0.1.0"
description = "A template flask API for building ontop of"
packages = [{ include = "api" }]
packages = [{ include = "src" }]
authors = ["Nava Engineering <[email protected]>"]

[tool.poetry.dependencies]
Expand Down Expand Up @@ -44,9 +44,9 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
db-migrate-up = "api.db.migrations.run:up"
db-migrate-down = "api.db.migrations.run:down"
db-migrate-down-all = "api.db.migrations.run:downall"
db-migrate-up = "src.db.migrations.run:up"
db-migrate-down = "src.db.migrations.run:down"
db-migrate-down-all = "src.db.migrations.run:downall"

[tool.black]
line-length = 100
Expand Down Expand Up @@ -85,14 +85,14 @@ plugins = ["sqlalchemy.ext.mypy.plugin"]

[tool.bandit]
# Ignore audit logging test file since test audit logging requires a lot of operations that trigger bandit warnings
exclude_dirs = ["./tests/api/logging/test_audit.py"]
exclude_dirs = ["./tests/src/logging/test_audit.py"]

[[tool.mypy.overrides]]
# Migrations are generated without "-> None"
# for the returns. Rather than require manually
# fixing this for every migration generated,
# disable the check for that folder.
module = "api.db.migrations.versions.*"
module = "src.db.migrations.versions.*"
disallow_untyped_defs = false

[tool.pytest.ini_options]
Expand All @@ -108,5 +108,5 @@ markers = [
"audit: mark a test as a security audit log test, to be run isolated from other tests"]

[tool.coverage.run]
omit = ["api/db/migrations/*.py"]
omit = ["src/db/migrations/*.py"]

File renamed without changes.
10 changes: 5 additions & 5 deletions app/api/__main__.py → app/src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import logging

import api.app
import api.logging
from api.app_config import AppConfig
from api.util.local import load_local_env_vars
import src.app
import src.logging
from src.app_config import AppConfig
from src.util.local import load_local_env_vars

logger = logging.getLogger(__package__)

Expand All @@ -19,7 +19,7 @@ def main() -> None:
load_local_env_vars()
app_config = AppConfig()

app = api.app.create_app()
app = src.app.create_app()

environment = app_config.environment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
To use this module with Flask, use the flask_db module.
Usage:
import api.adapters.db as db
import src.adapters.db as db
db_client = db.init()
Expand All @@ -23,7 +23,7 @@
"""

# Re-export for convenience
from api.adapters.db.client import Connection, DBClient, Session, init
from src.adapters.db.client import Connection, DBClient, Session, init

# Do not import flask_db here, because this module is not dependent on any specific framework.
# Code can choose to use this module on its own or with the flask_db module depending on needs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sqlalchemy.pool as pool
from sqlalchemy.orm import session

from api.adapters.db.config import DbConfig, get_db_config
from src.adapters.db.config import DbConfig, get_db_config

# Re-export the Connection type that is returned by the get_connection() method
# to be used for type hints.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pydantic import Field

from api.util.env_config import PydanticBaseEnvConfig
from src.util.env_config import PydanticBaseEnvConfig

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
of a Flask app and an instance of a DBClient.
Example:
import api.adapters.db as db
import api.adapters.db.flask_db as flask_db
import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
db_client = db.init()
app = APIFlask(__name__)
Expand All @@ -16,8 +16,8 @@
new database session that lasts for the duration of the request.
Example:
import api.adapters.db as db
import api.adapters.db.flask_db as flask_db
import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
@app.route("/health")
@flask_db.with_db_session
Expand All @@ -31,7 +31,7 @@ def health(db_session: db.Session):
Example:
from flask import current_app
import api.adapters.db.flask_db as flask_db
import src.adapters.db.flask_db as flask_db
@app.route("/health")
def health():
Expand All @@ -43,8 +43,8 @@ def health():

from flask import Flask, current_app

import api.adapters.db as db
from api.adapters.db.client import DBClient
import src.adapters.db as db
from src.adapters.db.client import DBClient

_FLASK_EXTENSION_KEY = "db"

Expand All @@ -67,7 +67,7 @@ def get_db(app: Flask) -> DBClient:
Example:
from flask import current_app
import api.adapters.db.flask_db as flask_db
import src.adapters.db.flask_db as flask_db
@app.route("/health")
def health():
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions app/api/api/healthcheck.py → app/src/api/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from sqlalchemy import text
from werkzeug.exceptions import ServiceUnavailable

import api.adapters.db.flask_db as flask_db
from api.api import response
from api.api.schemas import request_schema
import src.adapters.db.flask_db as flask_db
from src.api import response
from src.api.schemas import request_schema

logger = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions app/api/api/response.py → app/src/api/response.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dataclasses
from typing import Optional

from api.api.schemas import response_schema
from api.db.models.base import Base
from src.api.schemas import response_schema
from src.db.models.base import Base


@dataclasses.dataclass
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from apiflask import fields

from api.api.schemas import request_schema
from src.api.schemas import request_schema


class ValidationErrorSchema(request_schema.OrderedSchema):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from api.api.users.user_blueprint import user_blueprint
from src.api.users.user_blueprint import user_blueprint

# import user_commands module to register the CLI commands on the user_blueprint
import api.api.users.user_commands # noqa: F401 E402 isort:skip
import src.api.users.user_commands # noqa: F401 E402 isort:skip

# import user_commands module to register the API routes on the user_blueprint
import api.api.users.user_routes # noqa: F401 E402 isort:skip
import src.api.users.user_routes # noqa: F401 E402 isort:skip


__all__ = ["user_blueprint"]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import click

import api.adapters.db as db
import api.adapters.db.flask_db as flask_db
import api.services.users as user_service
from api.api.users.user_blueprint import user_blueprint
from api.util.datetime_util import utcnow
import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
import src.services.users as user_service
from src.api.users.user_blueprint import user_blueprint
from src.util.datetime_util import utcnow

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import logging
from typing import Any

import api.adapters.db as db
import api.adapters.db.flask_db as flask_db
import api.api.response as response
import api.api.users.user_schemas as user_schemas
import api.services.users as user_service
import api.services.users as users
from api.api.users.user_blueprint import user_blueprint
from api.auth.api_key_auth import api_key_auth
from api.db.models.user_models import User
import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
import src.api.response as response
import src.api.users.user_schemas as user_schemas
import src.services.users as user_service
import src.services.users as users
from src.api.users.user_blueprint import user_blueprint
from src.auth.api_key_auth import api_key_auth
from src.db.models.user_models import User

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from apiflask import fields
from marshmallow import fields as marshmallow_fields

from api.api.schemas import request_schema
from api.db.models import user_models
from src.api.schemas import request_schema
from src.db.models import user_models


class RoleSchema(request_schema.OrderedSchema):
Expand Down
18 changes: 9 additions & 9 deletions app/api/app.py → app/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
from flask import g
from werkzeug.exceptions import Unauthorized

import api.adapters.db as db
import api.adapters.db.flask_db as flask_db
import api.logging
import api.logging.flask_logger as flask_logger
from api.api.healthcheck import healthcheck_blueprint
from api.api.schemas import response_schema
from api.api.users import user_blueprint
from api.auth.api_key_auth import User, get_app_security_scheme
import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
import src.logging
import src.logging.flask_logger as flask_logger
from src.api.healthcheck import healthcheck_blueprint
from src.api.schemas import response_schema
from src.api.users import user_blueprint
from src.auth.api_key_auth import User, get_app_security_scheme

logger = logging.getLogger(__name__)


def create_app() -> APIFlask:
app = APIFlask(__name__)

root_logger = api.logging.init(__package__)
root_logger = src.logging.init(__package__)
flask_logger.init_app(root_logger, app)

db_client = db.init()
Expand Down
2 changes: 1 addition & 1 deletion app/api/app_config.py → app/src/app_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from api.util.env_config import PydanticBaseEnvConfig
from src.util.env_config import PydanticBaseEnvConfig


class AppConfig(PydanticBaseEnvConfig):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[alembic]
# path to migration scripts
script_location = api/db/migrations
script_location = src/db/migrations

file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(slug)s

Expand Down
Loading

0 comments on commit 6a3de8b

Please sign in to comment.