Skip to content

Commit

Permalink
Merge pull request #13 from billsioros/feat--deployment-using-docker-…
Browse files Browse the repository at this point in the history
…and-docker-compose

feat: deployment using docker and docker-compose
  • Loading branch information
billsioros authored Jul 27, 2024
2 parents 61dae86 + 9e6b208 commit 691dbbc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 149 deletions.
49 changes: 0 additions & 49 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,6 @@ body:
placeholder: Describe the problem!
validations:
required: true
- type: textarea
id: reproduction-steps
attributes:
label: How can we reproduce the bug?
description: Please provide the reproduction steps corresponding to the bug.
placeholder: Provide us with some python code!
value: |
from heartbeat import heartbeat
render: python
validations:
required: true
- type: textarea
id: stack-trace
attributes:
label: Relevant stack trace
description: Please provide us with the program's stack trace, if appropriate.
render: shell
- type: dropdown
id: operating-system
attributes:
label: Operating System (OS)
multiple: true
description: In what OS(s) does the problem occur?
options:
- macOS
- Windows
- Linux
validations:
required: true
- type: dropdown
id: python-version
attributes:
label: Python version
multiple: true
description: In what python version(s) does the problem occur?
options:
- 3.7
- 3.8
- 3.9
validations:
required: true
- type: checkboxes
id: duplicate-issue
attributes:
Expand All @@ -62,14 +21,6 @@ body:
options:
- label: I confirm that this is not a duplicate issue
required: true
- type: checkboxes
id: latest-version
attributes:
label: Have you upgraded to the latest version?
description: By submitting this issue, you confirm that you have already upgraded to the latest version
options:
- label: I confirm that I have upgraded to the latest version
required: true
- type: checkboxes
id: code-of-conduct
attributes:
Expand Down
117 changes: 101 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
alt="Open in GitHub Codespaces"
/>
</a>
<a href="https://github.com/billsioros/cookiecutter-pypackage">
<a href="https://github.com/billsioros/heartbeat">
<img
src="https://img.shields.io/badge/cookiecutter-template-D4AA00.svg?style=flat&logo=cookiecutter"
alt="Cookiecutter Template">
Expand All @@ -45,34 +45,119 @@
</a>
</p>

## :cd: Running the project
## :rocket: Running the project

### NVM
> [!IMPORTANT]
> The following instructions assume you have [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) installed.
1. **Clone the repository** and navigate to the project directory.

```shell
git clone https://github.com/billsioros/heartbeat
cd heartbeat
```

2. **Edit the `.env` file**.

```shell
POSTGRES_DB='heartbeat'
POSTGRES_USER='guest'
POSTGRES_PASSWORD=')M8z*yss$cRxw7(&'
BACKEND_DATABASE__URI = 'postgresql://guest:)M8z*yss$cRxw7(&@database:5432/heartbeat'
```

3. **Build and start the services** using Docker Compose.

```shell
docker-compose up --build
```

> The frontend and the backend should be now available at [`http://localhost:80`](http://localhost:80) and [`http://localhost:8000`](http://localhost:8000), respectively.

### Troubleshooting

- To view the logs of a specific service, run:

```shell
docker-compose logs <service_name>
```

- To stop the services, run:

```shell
docker-compose down
```

- To remove the volumes along with stopping the services, run:

```shell
docker compose down -v
```

### Running the services outside of Docker

> [!IMPORTANT]
> Ensure PostgreSQL is set up before running the project by using `docker compose up database --d`. Afterward, run `poe manage database create` to create the actual database. To tear down PostgreSQL, use `docker compose down database -v`.

Running the backend:

```shell
nvm use --lts --global
npm install
npm run dev -- --host --port 3000
export BACKEND_DATABASE__URI='postgresql://guest:)M8z*yss$cRxw7(&@localhost:5432/heartbeat'
poetry env use 3.11
poetry install
python src/api/app.py
```

```bash
docker compose up
> [!NOTE]
> We use the [Poetry](https://python-poetry.org/) Python package manager. [Having installed Poetry](https://python-poetry.org/docs/#installing-with-the-official-installer) you may now create a brand new [virtual environment](https://docs.python.org/3/tutorial/venv.html) and install the project's dependencies.

Running the frontend:

```shell
cd src/web
nvm use 20
npm install
npm run dev -- --host --port 80
```

In order to locally set up the project please follow the instructions below:
> [!NOTE]
> [nvm](https://github.com/nvm-sh/nvm) enables you to quickly install and switch between different versions of Node.js via the command line. Ensure you install nvm, then install Node.js 20 by running `nvm install 20`.

## :computer: Deploying to production

## Deploying to Production

Deploying to production involves several crucial steps, including setting up a server, configuring DNS, and managing SSL certificates. [`Traefik`](https://github.com/traefik/traefik) simplifies many of these tasks, acting as a powerful reverse proxy and load balancer. For a comprehensive guide on deploying your FastAPI application with Traefik, read the full article [here](https://github.com/tiangolo/blog-posts/blob/master/deploying-fastapi-apps-with-https-powered-by-traefik/README.md).

## :bookmark_tabs: Contributing

In order to locally set up the project first clone the repository:

```shell
# Set up the GitHub repository
git clone https://github.com/billsioros/heartbeat
```

# Create a virtual environment using poetry and install the required dependencies
poetry shell
poetry install
Optionally, you can install [pyenv](https://github.com/pyenv/pyenv?tab=readme-ov-file#installation) to easily install Python 3.11:

# Install pre-commit hooks
pre-commit install --install-hooks
```bash
pyenv install 3.11.4
pyenv local 3.11.4
```

### Installing pre-commit hooks

Git hooks are scripts that run automatically to perform tasks like linting and formatting code at different stages of the development process. [pre-commit](https://pre-commit.com/) is a tool designed to manage and share these hooks across projects easily. Having created a virtual environment and installed the required dependencies, you may run `pre-commit install --install-hooks` to install the [git hook scripts](https://github.com/billsioros/heartbeat/blob/master/.pre-commit-config.yaml).


### Performing development operations via `poethepoet`

We are using [poethepoet](https://github.com/nat-n/poethepoet), to perform various development oriented tasks. Formatting, type-checking, as well as a few other operations, can be performed by running `poe <task>`. Please run `poe --help` (or `poetry run poe --help`), to list all available operations.

### Writing your commit message

The project's version number and [Changelog](https://github.com/billsioros/heartbeat/blob/master/CHANGELOG.md), depend on a consistent commit history. As a result, your commit message's format is extremely important. Before opening a pull request, please make sure that your commits strictly follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format.

## :label: Credits

This project was generated with [`billsioros/cookiecutter-pypackage`](https://github.com/billsioros/cookiecutter-pypackage) cookiecutter template.
This project was generated with [`billsioros/heartbeat`](https://github.com/billsioros/heartbeat) cookiecutter template.
4 changes: 2 additions & 2 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def register_controllers(app: FastAPI, prefix: str = "") -> FastAPI:
def register_middlewares(app: FastAPI) -> FastAPI:
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
Expand All @@ -56,9 +56,9 @@ def register_events(app: FastAPI) -> FastAPI:
def create_app() -> FastAPI:
app = initialize_api()
app = register_configuration(app)
app = register_middlewares(app)
app = register_events(app)
app = register_controllers(app)
app = register_middlewares(app)

return app

Expand Down
20 changes: 1 addition & 19 deletions src/api/cli/manage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import subprocess
import webbrowser

import typer

from api.cli.commands.database import group as database
Expand All @@ -12,7 +9,7 @@

@cli.callback(name="heartbeat")
def main(ctx: typer.Context):
"""NotOnMyWatch CLI."""
"""HeartBeat CLI."""
settings = Settings()
database = Database(str(settings.database.uri))

Expand All @@ -22,20 +19,5 @@ def main(ctx: typer.Context):
cli.add_typer(database, name="database")


@cli.command()
def serve(port: int = 8000, build: bool = False):
"""Serve the application on localhost."""
cmd = ["docker-compose", "up", "--force-recreate", "-d"]
if build:
cmd = ["docker-compose", "up", "--force-recreate", "--build", "-d"]

try:
subprocess.run(cmd, check=False)
except subprocess.CalledProcessError as e:
raise typer.Exit(code=e.returncode)

webbrowser.open(f"http://localhost:{port}")


if __name__ == "__main__":
cli()
2 changes: 1 addition & 1 deletion src/api/controllers/monitor_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
api: Controller = create_controller(__file__, post_fix=None, tags=["Monitoring"])


@api.get("/database", status_code=status.HTTP_204_NO_CONTENT)
@api.get("/", status_code=status.HTTP_204_NO_CONTENT)
async def database_health_check(
database: Database = Depends(get_database),
):
Expand Down
Loading

0 comments on commit 691dbbc

Please sign in to comment.