Skip to content

Commit

Permalink
major refactoring for version 2.
Browse files Browse the repository at this point in the history
- includes easier usage of external_resources (databases and services).

- bugfix in logger (previously wrong function was declared as source of log-message)

- updated documentation with more details and an additional exampleservice-section

- now mainly using poetry. dephell is now used for package creation.

- add additional tests for previously untested parts

- added function for easier usage of versioned endpoints
  • Loading branch information
skallfass committed Nov 10, 2019
1 parent a717234 commit 1333a14
Show file tree
Hide file tree
Showing 118 changed files with 3,716 additions and 918 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,3 @@ repos:
hooks:
- id: flake8
types: [python]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: name-tests-test
types: [python]
6 changes: 3 additions & 3 deletions .tmuxp.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
session_name: fastapi_serviceutils
start_directory: "${PWD}"
shell_command_before: source "/home/skallfass/.local/share/dephell/venvs/fastapi_serviceutils-7Jam/devs/bin/activate"
shell_command_before: source "$VIRTUAL_ENV/bin/activate"
environment:
PYTHONPATH: ${PWD}
SHELL: /usr/bin/zsh
Expand All @@ -14,5 +14,5 @@ windows:
- focus: true
- shell_command:
- make update
#- shell_command:
# - pytest --looponfail
- shell_command:
- pytest --looponfail
99 changes: 51 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.DEFAULT_GOAL := help
SHELL := bash
SHELL := zsh

.PHONY : check clean docker docs finalize info init tests update help
.SILENT: check clean docker docs finalize info init tests update _create_devs_env _create_main_env _update_devs_env _update_main_env
.SILENT: check clean docker docs finalize info init tests update _create_env _update_env

python_meant=$(cat .python-version)
python_used=$(which python)
Expand All @@ -15,72 +15,75 @@ info:
echo "Project:"
echo "========"
echo "version: $(value project_version)"
echo "" && \
echo 'Python:' && \
echo '=======' && \
echo "used python: $(value python_used)" && \
echo "version: $(value python_version)" && \
echo "" && \
echo "Info about current environment:" && \
echo '===============================' && \
dephell inspect venv && \
echo "" && \
echo "Outdated dependencies:" && \
echo '=====================' && \
dephell deps outdated
echo ""
echo 'Python:'
echo '======='
echo "used python: $(value python_used)"
echo "version: $(value python_version)"
echo ""
echo "Info about current environment:"
echo '==============================='
poetry show

# Clean the working directory from temporary files and caches.
clean:
rm -rf htmlcov && \
rm -rf *.egg-info && \
rm -rf dist && \
rm -rf **/__pycache__ && \
rm -rf **/**/__pycache__ && \
rm -rf **/**/**/__pycache__ && \
rm -rf docs/_build && \
rm -rf docs/api && \
rm -rf .pytest_cache && \
rm -f .coverage
rm -rf htmlcov; \
rm -rf *.egg-info; \
rm -rf dist; \
rm -rf **/__pycache__; \
rm -rf docs/_build; \
rm -rf docs/api; \
rm -rf .pytest_cache; \
rm -rf .coverage; \
rm -rf log; \
rm -rf pip-wheel-metadata

_create_env:
poetry install

_update_env:
poetry update

_coverage_badge:
coverage-badge -f -o docs/_static/coverage.svg

_lock_it:
poetry lock
dephell deps convert --to requirements.txt
cp requirements.txt docs/doc_requirements.txt

_create_devs_env:
dephell venv create --env devs --python=$(value python_meant)
_makefile_doc:
make help > docs/makefile_help.txt

_update_devs_env:
dephell deps install --env devs
_extract_docstrings:
sphinx-apidoc -o docs/api --force --implicit-namespaces --module-first fastapi_serviceutils

_create_main_env:
dephell venv create --env main --python=$(value python_meant)
_html_documentation:
PYTHONPATH=. sphinx-build -b html docs docs/_build

_update_main_env:
dephell deps install --env main
_servicetools_doc:
poetry run create_service --help > docs/create_service_help.txt && \

# Run tests using pytest.
tests:
PYTHONPATH=. pytest -s
docker-compose down; docker-compose up -d; sleep 2; pytest tests; docker-compose down

# Finalize the main env.
finalize: update tests
finalize: tests _lock_it

# Create sphinx documentation for the project.
docs: update
make help > docs/makefile_help.txt && \
poetry run create_service --help > docs/create_service_help.txt && \
coverage-badge -f -o docs/_static/coverage.svg ; \
sphinx-apidoc -o docs/api --force --module-first fastapi_serviceutils && \
PYTHONPATH=. sphinx-build -b html docs docs/_build
docs: tests _coverage_badge _makefile_doc _servicetools_doc _extract_docstrings _html_documentation _lock_it

doc: _makefile_doc _servicetools_doc _extract_docstrings _html_documentation

# Initialize project
init: _create_main_env _create_devs_env _update_main_env _update_devs_env
poetry lock
init: _create_env _lock_it

# Update environments based on pyproject.toml definitions.
update: _update_main_env _update_devs_env
poetry lock; \
dephell deps convert --to requirements.txt && \
cp requirements.txt docs/doc_requirements.txt
update: _update_env _lock_it

# Run all checks defined in .pre-commit-config.yaml.
check: update
check:
pre-commit run --all-files

# Show the help prompt.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ After cloning the repository initialize the development environment using:

This will create the dev environment exampleservice/dev. Activate it using:
```bash
dephell venv shell --env devs
poetry shell
```

**Note:**

Make sure to always activate the environment when you start working on the
project in a new terminal using
```bash
dephell venv shell --env devs
poetry shell
```

**ATTENTION:** the environment should also be activated before using ``make``.
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This will create the dev environment exampleservice/dev. Activate it using:

.. code-block:: bash
dephell venv shell --env devs
poetry shell
**Note:**

Expand All @@ -67,7 +67,7 @@ project in a new terminal using

.. code-block:: bash
dephell venv shell --env devs
poetry shell
**ATTENTION:** the environment should also be activated before using ``make``.

Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

services:
testdb:
image: postgres:11.2-alpine
ports:
- "5434:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=1234
- POSTGRES_DB=monitordb
6 changes: 3 additions & 3 deletions docs/_static/coverage.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 23 additions & 17 deletions docs/_static/example_config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
service:
name: <SERVICE_NAME>
name: 'exampleservice'
mode: 'devl'
development_port: <SERVICE_PORT>
description: 'Some short description about the service.'
port: 50001
description: 'Example tasks'
apidoc_dir: 'docs/_build'
readme: 'README.md'
allowed_hosts:
Expand All @@ -11,25 +11,31 @@ service:
- alive
- config
external_resources:
api:
url: <EXTERNAL_SERVICE_URL>
services:
testservice:
url: http://something:someport
servicetype: 'rest'
databases:
testdb:
dsn: 'postgresql://postgres:1234@localhost:5434/monitordb'
databasetype: 'postgres'
other: null
logger:
path: <PATH_TO_STORE_LOGFILES>
path: './log/EXAMPLESERVICE'
filename: 'service_{mode}.log'
level: 'debug'
rotation: '1 days'
retention: '1 months'
format: '{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}'
format: "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> [{extra[request_id]}] - <level>{message}</level>"
available_environment_variables:
env_vars:
- SERVICE__MODE
- SERVICE__DEVELOPMENT_PORT
- LOGGER__LEVEL
- LOGGER__PATH
- LOGGER__FILENAME
- LOGGER__ROTATION
- LOGGER__RETENTION
- LOGGER__FORMAT
external_resources_env_vars:
- EXTERNAL_RESOURCES__API__URL
- EXAMPLESERVICE_SERVICE__MODE
- EXAMPLESERVICE_SERVICE__PORT
- EXAMPLESERVICE_LOGGER__LEVEL
- EXAMPLESERVICE_LOGGER__PATH
- EXAMPLESERVICE_LOGGER__FILENAME
- EXAMPLESERVICE_LOGGER__ROTATION
- EXAMPLESERVICE_LOGGER__RETENTION
- EXAMPLESERVICE_LOGGER__FORMAT
external_resources_env_vars: []
rules_env_vars: []
1 change: 1 addition & 0 deletions docs/_static/exampleservice/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.1.0'
15 changes: 10 additions & 5 deletions tests/config.yml → docs/_static/exampleservice/app/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service:
name: 'exampleservice'
mode: 'devl'
development_port: 50001
port: 50001
description: 'Example tasks'
apidoc_dir: 'docs/_build'
readme: 'README.md'
Expand All @@ -10,23 +10,28 @@ service:
use_default_endpoints:
- alive
- config
external_resources: {}
external_resources:
services: null
databases: null
other: null
logger:
path: './log/EXAMPLESERVICE'
filename: 'service_{mode}.log'
level: 'debug'
rotation: '1 days'
retention: '1 months'
format: '{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}'
format: "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> [id: {extra[request_id]}] - <level>{message}</level>"
available_environment_variables:
env_vars:
- SERVICE__MODE
- SERVICE__DEVELOPMENT_PORT
- SERVICE__PORT
- LOGGER__LEVEL
- LOGGER__PATH
- LOGGER__FILENAME
- LOGGER__ROTATION
- LOGGER__RETENTION
- LOGGER__FORMAT
external_resources_env_vars: []
external_resources_env_vars:
- EXTERNAL_RESOURCES__API__URL
- EXTERNAL_RESOURCES__API__SCHEMA
rules_env_vars: []
13 changes: 13 additions & 0 deletions docs/_static/exampleservice/app/endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from app.endpoints.v1 import ENDPOINTS as v1

from fastapi_serviceutils.app.endpoints import set_version_endpoints

LATEST = set_version_endpoints(
endpoints=v1,
version='latest',
prefix_template='{route}'
)

ENDPOINTS = LATEST + v1

__all__ = ['ENDPOINTS']
11 changes: 11 additions & 0 deletions docs/_static/exampleservice/app/endpoints/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from app.endpoints.v1 import example

from fastapi_serviceutils.app.endpoints import set_version_endpoints

ENDPOINTS = set_version_endpoints(
endpoints=[example],
version='v1',
prefix_template='/api/{version}{route}'
)

__all__ = ['ENDPOINTS']
1 change: 1 addition & 0 deletions docs/_static/exampleservice/app/endpoints/v1/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Contain error-messages to be used for the endpoints of v1."""
19 changes: 19 additions & 0 deletions docs/_static/exampleservice/app/endpoints/v1/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from app.endpoints.v1.models import Example as Output
from app.endpoints.v1.models import GetExample as Input
from fastapi import APIRouter
from fastapi import Body
from starlette.requests import Request

from fastapi_serviceutils.app import create_id_logger
from fastapi_serviceutils.app import Endpoint

ENDPOINT = Endpoint(router=APIRouter(), route='/example', version='v1')
SUMMARY = 'Example request.'
EXAMPLE = Body(..., example={'msg': 'some message.'})


@ENDPOINT.router.post('/', response_model=Output, summary=SUMMARY)
async def example(request: Request, params: Input = EXAMPLE) -> Output:
_, log = create_id_logger(request=request, endpoint=ENDPOINT)
log.debug(f'received request for {request.url} with params {params}.')
return Output(msg=params.msg)
12 changes: 12 additions & 0 deletions docs/_static/exampleservice/app/endpoints/v1/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pydantic import BaseModel


class GetExample(BaseModel):
msg: str


class Example(BaseModel):
msg: str


__all__ = ['Example', 'GetExample']

0 comments on commit 1333a14

Please sign in to comment.