Skip to content

Commit

Permalink
Add nginx service as second docker-compose stack (#503)
Browse files Browse the repository at this point in the history
* add nginx service to docker-compose, proxy pgstac and sqlalchemy apps

* Update docker-compose.yml

Co-authored-by: Christian Wygoda <[email protected]>

* feat: add nginx proxy in own docker-compose file

Includes a Makefile rule to run it, and some documentation in README.md. Also
adds a markdownlint silencer and one or two touchups to the whitespace.

* Update changelog

---------

Co-authored-by: Christian Wygoda <[email protected]>
Co-authored-by: Pete Gadomski <[email protected]>
Co-authored-by: Nathan Zimmerman <[email protected]>
  • Loading branch information
4 people authored Feb 27, 2023
1 parent 2482467 commit 1de7b8b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

* Nginx service as second docker-compose stack to demonstrate proxy ([#503](https://github.com/stac-utils/stac-fastapi/pull/503))
* Validation checks in CI using [stac-api-validator](github.com/stac-utils/stac-api-validator) ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
* Required links to the sqlalchemy ItemCollection endpoint ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
* Publication of docker images to GHCR ([#525](https://github.com/stac-utils/stac-fastapi/pull/525))
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ docker-run-sqlalchemy: image
docker-run-pgstac: image
$(run_pgstac)

.PHONY: docker-run-nginx-proxy
docker-run-nginx-proxy:
docker-compose -f docker-compose.yml -f docker-compose.nginx.yml up

.PHONY: docker-shell-sqlalchemy
docker-shell-sqlalchemy:
$(run_sqlalchemy) /bin/bash
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- markdownlint-disable MD033 MD041 -->

<p align="center">
<img src="https://github.com/radiantearth/stac-site/raw/master/images/logo/stac-030-long.png" width=400>
<p align="center">FastAPI implemention of the STAC API spec.</p>
Expand Down Expand Up @@ -101,7 +103,23 @@ The application will be started on <http://localhost:8080>.
By default, the apps are run with uvicorn hot-reloading enabled. This can be turned off by changing the value
of the `RELOAD` env var in docker-compose.yml to `false`.

#### Note to Docker for Windows users
### nginx proxy

This repo includes an example nginx proxy service.
To start:

```shell
make docker-run-nginx-proxy
```

The proxy will be started on <http://localhost>, with the pgstac app available at <http://localhost/api/v1/pgstac/> and the sqlalchemy app at <http://localhost/api/v1/sqlalchemy/>.
If you need to customize the proxy port, use the `STAC_FASTAPI_NGINX_PORT` environment variable:

```shell
STAC_FASTAPI_NGINX_PORT=7822 make docker-run-nginx-proxy
```

### Note to Docker for Windows users

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.
Expand Down
18 changes: 18 additions & 0 deletions docker-compose.nginx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'
services:
nginx:
image: nginx
ports:
- ${STAC_FASTAPI_NGINX_PORT:-80}:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app-pgstac
- app-sqlalchemy
command: [ "nginx-debug", "-g", "daemon off;" ]
app-pgstac:
environment:
- UVICORN_ROOT_PATH=/api/v1/pgstac
app-sqlalchemy:
environment:
- UVICORN_ROOT_PATH=/api/v1/sqlalchemy
29 changes: 29 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
events {}

http {
server {
listen 80;

location /api/v1/pgstac {
rewrite ^/api/v1/pgstac(.*)$ $1 break;
proxy_pass http://app-pgstac:8082;
proxy_set_header HOST $host;
proxy_set_header Referer $http_referer;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /api/v1/sqlalchemy {
rewrite ^/api/v1/sqlalchemy(.*)$ $1 break;
proxy_pass http://app-sqlalchemy:8081;
proxy_set_header HOST $host;
proxy_set_header Referer $http_referer;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}

location / {
proxy_redirect off;
}
}
}
1 change: 1 addition & 0 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def run():
port=settings.app_port,
log_level="info",
reload=settings.reload,
root_path=os.getenv("UVICORN_ROOT_PATH", ""),
)
except ImportError:
raise RuntimeError("Uvicorn must be installed in order to use command")
Expand Down
3 changes: 3 additions & 0 deletions stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""FastAPI application."""
import os

from stac_fastapi.api.app import StacApi
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
from stac_fastapi.extensions.core import (
Expand Down Expand Up @@ -55,6 +57,7 @@ def run():
port=settings.app_port,
log_level="info",
reload=settings.reload,
root_path=os.getenv("UVICORN_ROOT_PATH", ""),
)
except ImportError:
raise RuntimeError("Uvicorn must be installed in order to use command")
Expand Down

0 comments on commit 1de7b8b

Please sign in to comment.