Skip to content

Commit e61b32c

Browse files
authored
enable hot reloading (#392)
* * refactor makefile targets to be more clear and work better for dev * rework multi-stage Dockerfile build to run faster and remove unused directives * update README with better instructions for running * readd pgstac asyncio config * enable hot-reloading for apps * update changelog
1 parent 99ce774 commit e61b32c

File tree

6 files changed

+77
-69
lines changed

6 files changed

+77
-69
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Add support for PGStac Backend to use PyGeofilter to convert Get Request with cql2-text into cql2-json to send to PGStac backend ([#346](https://github.com/stac-utils/stac-fastapi/pull/346))
1616
* Bulk Transactions object Items iterator now returns the Item objects rather than the string IDs of the Item objects
1717
([#355](https://github.com/stac-utils/stac-fastapi/issues/355))
18+
* docker-compose now runs uvicorn with hot-reloading enabled
1819

1920
### Removed
2021

Dockerfile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
FROM python:3.8-slim as base
22

3-
FROM base as builder
43
# Any python libraries that require system libraries to be installed will likely
54
# need the following packages in order to build
6-
RUN apt-get update && apt-get install -y build-essential git
5+
RUN apt-get update && \
6+
apt-get -y upgrade && \
7+
apt-get install -y build-essential git && \
8+
apt-get clean && \
9+
rm -rf /var/lib/apt/lists/*
710

811
ENV CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
912

10-
ARG install_dev_dependencies=true
13+
FROM base as builder
1114

1215
WORKDIR /app
1316

14-
# Install stac_fastapi.types
1517
COPY . /app
1618

17-
ENV PATH=$PATH:/install/bin
18-
19-
RUN mkdir -p /install && \
20-
pip install -e ./stac_fastapi/types[dev] && \
19+
RUN pip install -e ./stac_fastapi/types[dev] && \
2120
pip install -e ./stac_fastapi/api[dev] && \
2221
pip install -e ./stac_fastapi/extensions[dev] && \
2322
pip install -e ./stac_fastapi/sqlalchemy[dev,server] && \

Makefile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
APP_HOST ?= 0.0.0.0
33
APP_PORT ?= 8080
44
EXTERNAL_APP_PORT ?= ${APP_PORT}
5-
run_docker = docker-compose run --rm \
5+
run_sqlalchemy = docker-compose run --rm \
66
-p ${EXTERNAL_APP_PORT}:${APP_PORT} \
77
-e APP_HOST=${APP_HOST} \
88
-e APP_PORT=${APP_PORT} \
@@ -18,25 +18,29 @@ run_pgstac = docker-compose run --rm \
1818
image:
1919
docker-compose build
2020

21-
.PHONY: docker-run
22-
docker-run: image
23-
$(run_docker)
21+
.PHONY: docker-run-all
22+
docker-run-all:
23+
docker-compose up
24+
25+
.PHONY: docker-run-sqlalchemy
26+
docker-run-sqlalchemy: image
27+
$(run_sqlalchemy)
2428

2529
.PHONY: docker-run-pgstac
2630
docker-run-pgstac: image
2731
$(run_pgstac)
2832

29-
.PHONY: docker-shell
30-
docker-shell:
31-
$(run_docker) /bin/bash
33+
.PHONY: docker-shell-sqlalchemy
34+
docker-shell-sqlalchemy:
35+
$(run_sqlalchemy) /bin/bash
3236

3337
.PHONY: docker-shell-pgstac
3438
docker-shell-pgstac:
3539
$(run_pgstac) /bin/bash
3640

3741
.PHONY: test-sqlalchemy
3842
test-sqlalchemy: run-joplin-sqlalchemy
39-
$(run_docker) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/sqlalchemy/tests/ && pytest'
43+
$(run_sqlalchemy) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/sqlalchemy/tests/ && pytest -vvv'
4044

4145
.PHONY: test-pgstac
4246
test-pgstac:

README.md

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,46 +63,70 @@ pip install -e stac_fastapi/pgstac
6363

6464
## Local Development
6565

66-
Use docker-compose to deploy the application, migrate the database, and ingest some example data:
66+
Use docker-compose via make to start the application, migrate the database, and ingest some example data:
6767
```bash
68-
docker-compose build
69-
docker-compose up
68+
make image
69+
make docker-run-all
70+
```
7071

71-
# You can also launch application with specific backend (PGSTac or sqlalchemy)
72-
docker-compose up app-sqlalchemy
73-
# or
74-
docker-compose up app-pgstac
72+
- The SQLAlchemy backend app will be available on <http://localhost:8081>.
73+
- The PGStac backend app will be available on <http://localhost:8082>.
74+
75+
You can also launch only one of the applications with either of these commands:
76+
77+
```shell
78+
make docker-run-pgstac
79+
make docker-run-sqlalchemy
7580
```
7681

77-
For local development it is often more convenient to run the application outside docker-compose:
78-
```bash
79-
make docker-run
82+
The application will be started on <http://localhost:8080>.
83+
84+
By default, the apps are run with uvicorn hot-reloading enabled. This can be turned off by changing the value
85+
of the `RELOAD` env var in docker-compose.yml to `false`.
86+
87+
#### Note to Docker for Windows users
88+
89+
You'll need to enable experimental features on Docker for Windows in order to run the docker-compose,
90+
due to the "--platform" flag that is required to allow the project to run on some Apple architectures.
91+
To do this, open Docker Desktop, go to settings, select "Docker Engine", and modify the configuration
92+
JSON to have `"experimental": true`.
93+
94+
### Testing
95+
96+
Before running the tests, ensure the database and apps run with docker-compose are down:
97+
98+
```shell
99+
docker-compose down
80100
```
81101

82-
Before commit, install the [pre-commit](https://pre-commit.com) hooks with:
102+
The database container provided by the docker-compose stack must be running. This can be started with:
83103

84104
```shell
85-
pre-commit install
105+
make run-database
86106
```
87107

88-
The pre-commit hooks can be run manually with:
108+
To run tests for both the pgstac and sqlalchemy backends, execute:
89109

90110
```shell
91-
pre-commit run --all-files
111+
make test
92112
```
93113

94-
#### Note to Docker for Windows users
114+
To only run pgstac backend tests:
95115

96-
You'll need to enable experimental features on Docker for Windows in order to run the docker-compose, due to the "--platform" flag that is required to allow the project to run on some Apple architectures. To do this, open Docker Desktop, go to settings, select "Docker Engine", and modify the configuration JSON to have `"experimental": true`.
116+
```shell
117+
make test-pgstac
118+
```
97119

98-
### Testing
99-
The database container provided by the docker-compose stack must be running. Run all tests:
100-
```bash
101-
make test
120+
To only run sqlalchemy backend tests:
121+
122+
```shell
123+
make test-sqlalchemy
102124
```
103125

104-
Run individual tests by running pytest within the docker container:
105-
```bash
106-
make docker-shell
107-
$ pytest -v
126+
Run individual tests by running pytest within a docker container:
127+
128+
```shell
129+
make docker-shell-pgstac # or docker-shell-sqlalchemy
130+
$ pip install -e stac_fastapi/pgstac[dev]
131+
$ pytest -v stac_fastapi/pgstac/tests/api/test_api.py
108132
```

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
environment:
1111
- APP_HOST=0.0.0.0
1212
- APP_PORT=8081
13-
- RELOAD=false
13+
- RELOAD=true
1414
- ENVIRONMENT=local
1515
- POSTGRES_USER=username
1616
- POSTGRES_PASS=password
@@ -36,7 +36,7 @@ services:
3636
environment:
3737
- APP_HOST=0.0.0.0
3838
- APP_PORT=8082
39-
- RELOAD=false
39+
- RELOAD=true
4040
- ENVIRONMENT=local
4141
- POSTGRES_USER=username
4242
- POSTGRES_PASS=password

stac_fastapi/pgstac/README.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ PGStac stores all collection and item records as jsonb fields exactly as they co
3232

3333
While the Stac Sort Extension is fully supported, [PGStac](https://github.com/stac-utils/pgstac) is particularly enhanced to be able to sort by datetime (either ascending or descending). Sorting by anything other than datetime (the default if no sort is specified) on very large Stac repositories without very specific query limits (ie selecting a single day date range) will not have the same performance. For more than millions of records it is recommended to either set a low connection timeout on PostgreSQL or to disable use of the Sort Extension.
3434

35-
3635
`stac-fastapi pgstac` was initially added to `stac-fastapi` by [developmentseed](https://github.com/developmentseed).
3736

3837
## Installation
3938

40-
```
39+
```shell
4140
git clone https://github.com/stac-utils/stac-fastapi.git
4241
cd stac-fastapi
4342
pip install -e \
@@ -47,35 +46,16 @@ pip install -e \
4746
stac_fastapi/pgstac[dev,server]
4847
```
4948

50-
## Local Development
51-
Use docker-compose to deploy the application with a PGStac database. The docker-compose environment will run stac-fastapi/sqlalchemy on port 8081 and stac-fastapi/pgstac on port 8082.
52-
```bash
53-
docker-compose build
54-
docker-compose up
55-
```
56-
57-
58-
### Testing
59-
The test suite will create a new database on the currently set up database that will have all data truncated between tests. The database must be running with docker-compose or you must have your environment pointing to a running database with PGStac installed to run the tests. All tests are run in the database named "pgstactestdb".
60-
61-
Run all tests:
62-
```bash
63-
make test-pgstac
64-
```
65-
66-
Run individual tests by running pytest within the docker container:
67-
```bash
68-
make docker-shell
69-
$ pytest -v
70-
```
71-
7249
### Migrations
7350
PGStac is an external project and the may be used by multiple front ends.
74-
For Stac FastAPI development, a docker image (which is pulled as part of the docker-compose) is available at bitner/pgstac:[version] that has the full database already set up for PGStac.
51+
For Stac FastAPI development, a docker image (which is pulled as part of the docker-compose) is available at
52+
bitner/pgstac:[version] that has the full database already set up for PGStac.
7553

76-
There is also a python utility as part of PGStac (pypgstac) that includes a migration utility. The pgstac version required by stac-fastapi/pgstac is pinned by using the pinned version of pypgstac in the [setup](setup.py) file.
54+
There is also a python utility as part of PGStac (pypgstac) that includes a migration utility. The pgstac
55+
version required by stac-fastapi/pgstac is pinned by using the pinned version of pypgstac in the [setup](setup.py) file.
7756

7857
In order to migrate database versions you can use the migration utility:
79-
```bash
58+
59+
```shell
8060
pypgstac migrate
8161
```

0 commit comments

Comments
 (0)