Skip to content

Commit 8fec342

Browse files
authored
chore(iatlas): migrate iAtlas API project to an Nx project (ARCH-339) (#2930)
1 parent 7849b72 commit 8fec342

31 files changed

+1134
-143
lines changed

.devcontainer/devcontainer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
}
6969
},
7070
"forwardPorts": [
71-
2432, 3000, 3306, 3333, 4200, 4211, 5200, 5432, 5601, 7010, 7443, 7200, 7888, 8010, 8071, 8000,
72-
8080, 8081, 8082, 8084, 8085, 8086, 8090, 8200, 8443, 8888, 8889, 9090, 9104, 9200, 9411, 27017,
73-
18080
71+
2432, 3000, 3306, 3333, 4200, 4211, 5000, 5200, 5432, 5601, 7010, 7443, 7200, 7888, 8010, 8071,
72+
8000, 8080, 8081, 8082, 8084, 8085, 8086, 8090, 8200, 8443, 8888, 8889, 9090, 9104, 9200, 9411,
73+
27017, 18080
7474
],
7575
"portsAttributes": {
7676
"2432": {
@@ -97,6 +97,10 @@
9797
"label": "nx graph",
9898
"onAutoForward": "silent"
9999
},
100+
"5000": {
101+
"label": "iatlas-api",
102+
"onAutoForward": "silent"
103+
},
100104
"5432": {
101105
"label": "openchallenges-postgres",
102106
"onAutoForward": "silent"

apps/iatlas/api/.env.example

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FLASK_APP=iatlasapi.py
2+
FLASK_ENV=development
3+
FLASK_RUN_PORT=5000
4+
POSTGRES_DB=iatlas
5+
POSTGRES_HOST=iatlas-postgres
6+
POSTGRES_PORT=5432
7+
POSTGRES_PASSWORD=postgres
8+
POSTGRES_USER=changeme
9+
SNAKEVIZ_PORT=8020

apps/iatlas/api/.python-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.8.20

apps/iatlas/api/Dockerfile

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1-
# Start with a bare Alpine Linux to keep the container image small
2-
FROM tiangolo/uwsgi-nginx-flask:python3.8
1+
FROM python:3.8.20-slim-bullseye
32

4-
WORKDIR /app
5-
COPY . /app
3+
ENV APP_DIR=/opt/app
64

7-
# Install the PyPI dependencies using pip
8-
RUN pip3 install --no-cache-dir -r requirements.txt
5+
RUN apt-get update -qq -y && export DEBIAN_FRONTEND=noninteractive \
6+
&& apt-get install --no-install-recommends -qq -y \
7+
# Required to install uWSGI
8+
build-essential \
9+
# Used in docker-entrypoint.sh
10+
gosu \
11+
&& apt-get -y autoclean \
12+
&& apt-get -y autoremove \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
WORKDIR ${APP_DIR}
16+
COPY api ./api
17+
COPY pyproject.toml poetry.lock iatlasapi.py config.py uwsgi.ini ./
18+
19+
# Install the app dependencies.
20+
# Install the version of Poetry used inside the dev container.
21+
# See /workspaces/sage-monorepo/tools/devcontainers/sage/.devcontainer/Dockerfile
22+
RUN pip install --no-cache-dir poetry==1.8.3 \
23+
&& poetry config --local virtualenvs.create false \
24+
&& poetry install --with prod --no-root --no-interaction --no-ansi \
25+
&& pip cache purge \
26+
&& chown -R www-data:www-data ${APP_DIR}
27+
28+
WORKDIR /
29+
COPY docker-entrypoint.sh .
30+
RUN chmod +x docker-entrypoint.sh
31+
32+
EXPOSE 5000
33+
34+
ENTRYPOINT ["/docker-entrypoint.sh"]
35+
36+
# Run production server
37+
CMD ["uwsgi", "--ini", "uwsgi.ini", "--lazy", "--http", ":5000"]

apps/iatlas/api/README.md

+30-97
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,54 @@
11
# iAtlas API
22

3-
A GraphQL API that serves data from the iAtlas Data Database. This is built in Python and developed and deployed in Docker.
3+
A GraphQL API that serves data from the iAtlas Data Database. This is built in Python and developed
4+
and deployed in Docker.
45

5-
## Status
6+
## Usage
67

7-
### Staging
8+
Show the tasks available to this project:
89

9-
[![coverage report](https://gitlab.com/cri-iatlas/iatlas-api/badges/staging/coverage.svg?style=flat)](https://cri-iatlas.gitlab.io/iatlas-api/)
10-
11-
## Dependencies
12-
13-
- [Docker Desktop](https://www.docker.com/products/docker-desktop) (`docker`)
14-
- [Visual Studio Code](https://code.visualstudio.com/) (`code`) - this is optional, but sure makes everything a lot easier.
15-
16-
## Development
17-
18-
The instructions below assume that there is a PostgreSQL server running locally with the iAtlas Database installed (see [iAtlas-Data](https://gitlab.com/cri-iatlas/iatlas-data)). If this is not the case, please see information on [running PostgreSQL in Docker](#running-postgres-in-docker) below.
19-
20-
To change any of the environment variables used by the app see [Environment Variables](#environment-variables) below.
21-
22-
The first time you checkout the project, run the following command to build the docker image, start the container, and start the API:
23-
24-
```sh
25-
./start.sh
10+
```console
11+
nx show project iatlas-api
2612
```
2713

28-
This will build the Docker image and run the container. Once the container is created, the Flask server will be started.
29-
30-
The GraphiQL playground interface should open automatically in your browser.
31-
32-
**Note:** If you get _'Version in "./docker-compose.yml" is unsupported.'_, please update your version of Docker Desktop.
14+
## Create the project configuration file
3315

34-
**Optional:** If you choose to use VS Code, you can use the [Dev-Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension to develop from within the container itself. Using this approach, you don't need to install Python or any dependencies (besides Docker and VS Code itself) as everything is already installed inside the container. There is a volume mapped to your user .ssh folder so that your ssh keys are available inside the container as well as your user .gitconfig file. The user folder inside the container is also mapped to a volume so that it persists between starts and stops of the container. This means you may create a .bash_profile or similar for yourself within the container and it will persist between container starts and stops.
35-
36-
The following command will stop the server and container:
37-
38-
```sh
39-
./stop.sh
16+
```console
17+
nx create-config iatlas-api
4018
```
4119

42-
Restart the container with the following command:
20+
## Prepare the Python environment
4321

44-
```sh
45-
./start.sh
22+
```console
23+
nx prepare iatlas-api
4624
```
4725

48-
If there are changes made to the container or image, first, stop the container `./stop.sh`, then rebuild it and restarted it with `./start.sh --build` or `./start.sh -b`.
49-
50-
Remote into iatlas-dev container.
51-
52-
Open the workspace by file -> Open workspace from file -> /project/iatlas-api.code-workspace
53-
54-
### Non-Dockerized
55-
56-
If you choose NOT to use the dockerized development method above, please ensure the following are installed:
57-
58-
- [Python](https://www.python.org/) - version 3.8
59-
- All the packages in the [`requirements.txt`](./requirements.txt) file at the versions specified.
60-
- All the packages in the [`requirements-dev.txt`](./requirements-dev.txt) file at the versions specified.
61-
62-
See [https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) for information on installing Python packages for a specific project.
26+
## Build the Docker image
6327

64-
Start the app with the following called from the root of the project. (Please note the dot(`.`) at the very beginning of the command. This will "source" the script.):
65-
66-
```sh
67-
. set_env_variables.sh && python run.py
28+
```console
29+
nx build-image iatlas-api
6830
```
6931

70-
### Running Postgres in Docker
71-
72-
A simple way to get PostgreSQL running locally is to use Docker. Here is a simple Dockerized PostgreSQL server with pgAdmin:
73-
74-
["postgres_docker" on Github](https://github.com/generalui/postgres_docker)
75-
76-
#### Linux ONLY
77-
78-
If you are running on a Linux operating system the default connection to the docker container `host.docker.internal` will not work. To connect to the local dockerized PostgreSQL DB, ensure there is a `.env-dev` file ([`.env-SAMPLE`](./.env-SAMPLE) can be used as a reference.) In the `.env-dev` file, ensure the `POSTGRES_HOST` variable is set to `172.17.0.1`
32+
## Start the API with Docker Compose
7933

80-
```.env-dev
81-
POSTGRES_HOST=172.17.0.1
34+
```console
35+
nx serve-detach iatlas-api
8236
```
8337

84-
### Connecting to a different Database
38+
## Open the GraphQL Playground
8539

86-
Alternatively, the app may be set up to connect to the existing staging database or another database.
40+
To access the GraphQL Playground, open your browser and navigate to:
8741

88-
To connect to a different database (ie staging), the `.env-dev` file must also be used with values similar to:
42+
`http://http://localhost:5000/graphiql`
8943

90-
```.env-dev
91-
POSTGRES_DB=iatlas_staging
92-
POSTGRES_HOST=iatlas-staging-us-west-2.cluster-cfb68nhqxoz9.us-west-2.rds.amazonaws.com
93-
POSTGRES_PASSWORD={Get_the_staging_password}
94-
POSTGRES_USER=postgres
95-
```
96-
97-
### Environment Variables
44+
Use the query shown below to test the API or use one included in `./example_queries`.
9845

99-
All the environment variables used by the app have defaults. To set the environment variables, simply run the following bash script from the root of the project. (Please note the dot(`.`) at the very beginning of the command. This will "source" the script.):
100-
101-
```sh
102-
. set_env_variables.sh
46+
```json
47+
{
48+
__schema {
49+
queryType {
50+
name
51+
}
52+
}
53+
}
10354
```
104-
105-
The default environment variables' values may be over-written by adding the value to a `.env-dev` file in the root of the project. This file is not versioned in the repository.
106-
107-
The [`.env-SAMPLE`](./.env-SAMPLE) file is an example of what the `.env-dev` could be like and may be used as a reference.
108-
109-
## Testing
110-
111-
All tests are in the [`tests/`](./tests/) folder.
112-
113-
See: [TESTING.md](./tests/TESTING.md) in the [`tests/`](./tests/) folder
114-
115-
## Performance Profiling
116-
117-
See: [PROFILING.md](./api/telemetry/PROFILING.md) in the [`api/telemetry/`](./api/telemetry/) folder
118-
119-
## Logging
120-
121-
See: [LOGGING.md](./api/logger/LOGGING.md) in the [`api/logger/`](./api/logger/) folder

apps/iatlas/api/docker-entrypoint.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [ "$1" = 'uwsgi' ] || [ "$1" = 'python' ]; then
5+
cd ${APP_DIR}
6+
exec gosu www-data "$@"
7+
fi
8+
9+
exec "$@"

apps/iatlas/api/install.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
PYTHON_VERSION="3.8.20"
4+
5+
pyenv install --skip-existing $PYTHON_VERSION
6+
7+
# Initializing pyenv again solves an issue encountered by GitHub action where the version of Python
8+
# installed above is not detected.
9+
eval "$(pyenv init -)"
10+
11+
pyenv local $PYTHON_VERSION
12+
poetry env use $(pyenv which python)
13+
poetry install --with dev

0 commit comments

Comments
 (0)