Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Enabling containerized development #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[run]
source = project_name
source = fastapi_project_template
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this change because of the CI rename workflow?

maybe we need to add a way to prevent that workflow to run on forks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rochacbruno Looks like it. Please reject this PR.


[report]
omit =
*/python?.?/*
*/site-packages/nose/*
project_name/__main__.py
fastapi_project_template/__main__.py
10 changes: 5 additions & 5 deletions .github/rename_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ echo "Description: $description";

echo "Renaming project..."

original_author="author_name"
original_name="project_name"
original_urlname="project_urlname"
original_description="project_description"
original_author="sarath-ps"
original_name="fastapi_project_template"
original_urlname="fastapi-project-template"
original_description="Awesome fastapi_project_template created by sarath-ps"
# for filename in $(find . -name "*.*")
for filename in $(git ls-files)
do
Expand All @@ -30,7 +30,7 @@ do
echo "Renamed $filename"
done

mv project_name $name
mv fastapi_project_template $name

# This command runs only once on GHA!
rm -rf .github/template.yml
1 change: 0 additions & 1 deletion .github/template.yml

This file was deleted.

4 changes: 2 additions & 2 deletions ABOUT_THIS_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Lets take a look at the structure of this template:
├── Makefile # A collection of utilities to manage the project
├── MANIFEST.in # A list of files to include in a package
├── mkdocs.yml # Configuration for documentation site
├── project_name # The main python package for the project
├── fastapi_project_template # The main python package for the project
│   ├── base.py # The base module for the project
│   ├── __init__.py # This tells Python that this is a package
│   ├── __main__.py # The entry point for the project
Expand Down Expand Up @@ -109,7 +109,7 @@ I had to do some tricks to read that version variable inside the setuptools
I decided to keep the version in a static file because it is easier to read from
wherever I want without the need to install the package.

e.g: `cat project_name/VERSION` will get the project version without harming
e.g: `cat fastapi_project_template/VERSION` will get the project version without harming
with module imports or anything else, it is useful for CI, logs and debugging.

### Why to include `tests`, `history` and `Containerfile` as part of the release?
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# How to develop on this project

project_name welcomes contributions from the community.
fastapi_project_template welcomes contributions from the community.

**You need PYTHON3!**

This instructions are for linux base systems. (Linux, MacOS, BSD, etc.)
## Setting up your own fork of this repo.

- On github interface click on `Fork` button.
- Clone your fork of this repo. `git clone git@github.com:YOUR_GIT_USERNAME/project_urlname.git`
- Enter the directory `cd project_urlname`
- Add upstream repo `git remote add upstream https://github.com/author_name/project_urlname`
- Clone your fork of this repo. `git clone git@github.com:YOUR_GIT_USERNAME/fastapi-project-template.git`
- Enter the directory `cd fastapi-project-template`
- Add upstream repo `git remote add upstream https://github.com/sarath-ps/fastapi-project-template`

## Setting up your own virtual environment

Expand Down
5 changes: 0 additions & 5 deletions Containerfile

This file was deleted.

36 changes: 36 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# Base Image for builder
FROM python:3.9 as builder

# Install Requirements
COPY requirements.txt /
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txt


# Build the app image
FROM python:3.9

# Create directory for the app user
RUN mkdir -p /home/app

# Create the app user
RUN groupadd app && useradd -g app app

# Create the home directory
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# Install Requirements
COPY --from=builder /wheels /wheels
COPY --from=builder requirements.txt .
RUN pip install --no-cache /wheels/*

COPY . $APP_HOME

RUN chown -R app:app $APP_HOME

USER app

CMD ["uvicorn", "fastapi_project_template.app:app", "--host=0.0.0.0","--port=8000","--reload"]
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ include LICENSE
include HISTORY.md
include Containerfile
graft tests
graft project_name
graft fastapi_project_template
34 changes: 23 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ install: ## Install the project in dev mode.

.PHONY: fmt
fmt: ## Format code using black & isort.
$(ENV_PREFIX)isort project_name/
$(ENV_PREFIX)black -l 79 project_name/
$(ENV_PREFIX)isort fastapi_project_template/
$(ENV_PREFIX)black -l 79 fastapi_project_template/
$(ENV_PREFIX)black -l 79 tests/

.PHONY: lint
lint: ## Run pep8, black, mypy linters.
$(ENV_PREFIX)flake8 project_name/
$(ENV_PREFIX)black -l 79 --check project_name/
$(ENV_PREFIX)flake8 fastapi_project_template/
$(ENV_PREFIX)black -l 79 --check fastapi_project_template/
$(ENV_PREFIX)black -l 79 --check tests/
$(ENV_PREFIX)mypy --ignore-missing-imports project_name/
$(ENV_PREFIX)mypy --ignore-missing-imports fastapi_project_template/

.PHONY: test
test: lint ## Run tests and generate coverage report.
$(ENV_PREFIX)pytest -v --cov-config .coveragerc --cov=project_name -l --tb=short --maxfail=1 tests/
$(ENV_PREFIX)pytest -v --cov-config .coveragerc --cov=fastapi_project_template -l --tb=short --maxfail=1 tests/
$(ENV_PREFIX)coverage xml
$(ENV_PREFIX)coverage html

Expand Down Expand Up @@ -80,9 +80,9 @@ release: ## Create a new tag for release.
@read -p "Version? (provide the next x.y.z semver) : " TAG
@echo "creating git tag : $${TAG}"
@git tag $${TAG}
@echo "$${TAG}" > project_name/VERSION
@echo "$${TAG}" > fastapi_project_template/VERSION
@$(ENV_PREFIX)gitchangelog > HISTORY.md
@git add project_name/VERSION HISTORY.md
@git add fastapi_project_template/VERSION HISTORY.md
@git commit -m "release: version $${TAG} 🚀"
@git push -u origin HEAD --tags
@echo "Github Actions will detect the new tag and release the new version."
Expand All @@ -101,15 +101,15 @@ switch-to-poetry: ## Switch to poetry package manager.
@poetry init --no-interaction --name=a_flask_test --author=rochacbruno
@echo "" >> pyproject.toml
@echo "[tool.poetry.scripts]" >> pyproject.toml
@echo "project_name = 'project_name.__main__:main'" >> pyproject.toml
@echo "fastapi_project_template = 'fastapi_project_template.__main__:main'" >> pyproject.toml
@cat requirements.txt | while read in; do poetry add --no-interaction "$${in}"; done
@cat requirements-test.txt | while read in; do poetry add --no-interaction "$${in}" --dev; done
@poetry install --no-interaction
@mkdir -p .github/backup
@mv requirements* .github/backup
@mv setup.py .github/backup
@echo "You have switched to https://python-poetry.org/ package manager."
@echo "Please run 'poetry shell' or 'poetry run project_name'"
@echo "Please run 'poetry shell' or 'poetry run fastapi_project_template'"

.PHONY: init
init: ## Initialize the project based on an application template.
Expand All @@ -118,7 +118,19 @@ init: ## Initialize the project based on an application template.
.PHONY: shell
shell: ## Open a shell in the project.
@if [ "$(USING_POETRY)" ]; then poetry shell; exit; fi
@./.venv/bin/ipython -c "from project_name import *"
@./.venv/bin/ipython -c "from fastapi_project_template import *"

.PHONY: docker-build
docker-build: ## Builder docker images
@docker-compose -f docker-compose-dev.yaml -p fastapi_project_template build

.PHONY: docker-dev-run
docker-run: ## Run docker development images
@docker-compose -f docker-compose-dev.yaml -p fastapi_project_template up -d

.PHONY: docker-dev-stop
docker-stop: ## Bring down docker dev environment
@docker-compose -f docker-compose-dev.yaml -p fastapi_project_template down

# This project has been generated from rochacbruno/fastapi-project-template
# __author__ = 'rochacbruno'
Expand Down
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See also
- ✅ Code linting using [flake8](https://flake8.pycqa.org/en/latest/)
- 📊 Code coverage reports using [codecov](https://about.codecov.io/sign-up/)
- 🛳️ Automatic release to [PyPI](https://pypi.org) using [twine](https://twine.readthedocs.io/en/latest/) and github actions.
- 🎯 Entry points to execute your program using `python -m <project_name>` or `$ project_name` with basic CLI argument parsing.
- 🎯 Entry points to execute your program using `python -m <fastapi_project_template>` or `$ fastapi_project_template` with basic CLI argument parsing.
- 🔄 Continuous integration using [Github Actions](.github/workflows/) with jobs to lint, test and release your project on Linux, Mac and Windows environments.

> Curious about architectural decisions on this template? read [ABOUT_THIS_TEMPLATE.md](ABOUT_THIS_TEMPLATE.md)
Expand All @@ -49,51 +49,51 @@ See also
<!-- DELETE THE LINES ABOVE THIS AND WRITE YOUR PROJECT README BELOW -->

---
# project_name
# fastapi_project_template

[![codecov](https://codecov.io/gh/author_name/project_urlname/branch/main/graph/badge.svg?token=project_urlname_token_here)](https://codecov.io/gh/author_name/project_urlname)
[![CI](https://github.com/author_name/project_urlname/actions/workflows/main.yml/badge.svg)](https://github.com/author_name/project_urlname/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/sarath-ps/fastapi-project-template/branch/main/graph/badge.svg?token=fastapi-project-template_token_here)](https://codecov.io/gh/sarath-ps/fastapi-project-template)
[![CI](https://github.com/sarath-ps/fastapi-project-template/actions/workflows/main.yml/badge.svg)](https://github.com/sarath-ps/fastapi-project-template/actions/workflows/main.yml)

project_description
Awesome fastapi_project_template created by sarath-ps

## Install

from source
```bash
git clone https://github.com/author_name/project_urlname project_name
cd project_name
git clone https://github.com/sarath-ps/fastapi-project-template fastapi_project_template
cd fastapi_project_template
make install
```

from pypi

```bash
pip install project_name
pip install fastapi_project_template
```

## Executing

```bash
$ project_name run --port 8080
$ fastapi_project_template run --port 8080
```

or

```bash
python -m project_name run --port 8080
python -m fastapi_project_template run --port 8080
```

or

```bash
$ uvicorn project_name:app
$ uvicorn fastapi_project_template:app
```

## CLI

```bash
project_name --help
Usage: project_name [OPTIONS] COMMAND [ARGS]...
fastapi_project_template --help
Usage: fastapi_project_template [OPTIONS] COMMAND [ARGS]...

Options:
--install-completion [bash|zsh|fish|powershell|pwsh]
Expand All @@ -112,8 +112,8 @@ Commands:
### Creating a user

```bash
project_name create-user --help
Usage: project_name create-user [OPTIONS] USERNAME PASSWORD
fastapi_project_template create-user --help
Usage: fastapi_project_template create-user [OPTIONS] USERNAME PASSWORD

Create user

Expand All @@ -129,15 +129,15 @@ Options:
**IMPORTANT** To create an admin user on the first run:

```bash
project_name create-user admin admin --superuser
fastapi_project_template create-user admin admin --superuser
```

### The Shell

You can enter an interactive shell with all the objects imported.

```bash
project_name shell
fastapi_project_template shell
Auto imports: ['app', 'settings', 'User', 'engine', 'cli', 'create_user', 'select', 'session', 'Content']

In [1]: session.query(Content).all()
Expand All @@ -151,12 +151,12 @@ Out[3]: [Content(text='string', title='string', created_time='2021-09-14T19:25:0

## API

Run with `project_name run` and access http://127.0.0.1:8000/docs
Run with `fastapi_project_template run` and access http://127.0.0.1:8000/docs

![](https://raw.githubusercontent.com/rochacbruno/fastapi-project-template/master/docs/api.png)


**For some api calls you must authenticate** using the user created with `project_name create-user`.
**For some api calls you must authenticate** using the user created with `fastapi_project_template create-user`.

## Testing

Expand Down Expand Up @@ -189,18 +189,18 @@ tests/test_user_api.py::test_user_create PASSED [100%]
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover
-----------------------------------------------------
project_name/__init__.py 4 0 100%
project_name/app.py 16 1 94%
project_name/cli.py 21 0 100%
project_name/config.py 5 0 100%
project_name/db.py 10 0 100%
project_name/models/__init__.py 0 0 100%
project_name/models/content.py 47 1 98%
project_name/routes/__init__.py 11 0 100%
project_name/routes/content.py 52 25 52%
project_name/routes/security.py 15 1 93%
project_name/routes/user.py 52 26 50%
project_name/security.py 103 12 88%
fastapi_project_template/__init__.py 4 0 100%
fastapi_project_template/app.py 16 1 94%
fastapi_project_template/cli.py 21 0 100%
fastapi_project_template/config.py 5 0 100%
fastapi_project_template/db.py 10 0 100%
fastapi_project_template/models/__init__.py 0 0 100%
fastapi_project_template/models/content.py 47 1 98%
fastapi_project_template/routes/__init__.py 11 0 100%
fastapi_project_template/routes/content.py 52 25 52%
fastapi_project_template/routes/security.py 15 1 93%
fastapi_project_template/routes/user.py 52 26 50%
fastapi_project_template/security.py 103 12 88%
-----------------------------------------------------
TOTAL 336 66 80%

Expand All @@ -222,7 +222,7 @@ make fmt # formats the code
This project uses [Dynaconf](https://dynaconf.com) to manage configuration.

```py
from project_name.config import settings
from fastapi_project_template.config import settings
```

## Acessing variables
Expand Down Expand Up @@ -251,14 +251,14 @@ dynaconf_merge = true
echo = true
```

> `dynaconf_merge` is a boolean that tells if the settings should be merged with the default settings defined in project_name/default.toml.
> `dynaconf_merge` is a boolean that tells if the settings should be merged with the default settings defined in fastapi_project_template/default.toml.

### As environment variables
```bash
export PROJECT_NAME_KEY=value
export PROJECT_NAME_KEY="@int 42"
export PROJECT_NAME_KEY="@jinja {{ this.db.uri }}"
export PROJECT_NAME_DB__uri="@jinja {{ this.db.uri | replace('db', 'data') }}"
export fastapi_project_template_KEY=value
export fastapi_project_template_KEY="@int 42"
export fastapi_project_template_KEY="@jinja {{ this.db.uri }}"
export fastapi_project_template_DB__uri="@jinja {{ this.db.uri | replace('db', 'data') }}"
```

### Secrets
Expand All @@ -272,7 +272,7 @@ can read those variables.
### Switching environments

```bash
PROJECT_NAME_ENV=production project_name run
fastapi_project_template_ENV=production fastapi_project_template run
```

Read more on https://dynaconf.com
Expand Down
Loading