From ec5a98c9ba807d274132ea480073ed1135665dcf Mon Sep 17 00:00:00 2001 From: David Paul Graham <43794491+dpgraham4401@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:10:31 -0500 Subject: [PATCH] Reverse proxy updates (#780) * update traefik to use dynamic configs instead of static traefik.toml * remove static traefik file * update vite config * remove docker compose health checks * remove references to localhost outside configs --- client/vite.config.ts | 3 -- compose.yaml | 31 +++++++------ configs/.env.dev | 4 +- docs/guide/src/design/server.md | 18 +++++--- .../src/development/local-development.md | 2 +- docs/guide/src/operations/configuration.md | 4 +- server/haztrak/settings/dev.py | 2 +- traefik.toml | 43 ------------------- 8 files changed, 34 insertions(+), 73 deletions(-) delete mode 100644 traefik.toml diff --git a/client/vite.config.ts b/client/vite.config.ts index 8898999bf..7051366ca 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -41,9 +41,6 @@ export default defineConfig({ server: { host: true, port: 3000, - hmr: { - clientPort: 8080, - }, }, test: { environment: 'jsdom', diff --git a/compose.yaml b/compose.yaml index 99f065140..c493c60aa 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,7 +11,7 @@ services: - ./server:/app environment: DJANGO_SETTINGS_MODULE: "haztrak.settings.dev" - HT_HOST: "localhost" + HT_HOST: ${HT_HOST} HT_SECRET_KEY: ${HT_SECRET_KEY} HT_DEBUG: ${HT_DEBUG} HT_LOG_LEVEL: ${HT_LOG_LEVEL} @@ -27,19 +27,17 @@ services: HT_DB_PASSWORD: ${HT_DB_PASSWORD} HT_DB_HOST: db HT_DB_PORT: ${HT_DB_PORT} + labels: + - "traefik.enable=true" + - "traefik.http.routers.trak.rule=Host(`${HT_HOST}`)" + - "traefik.http.routers.trak.rule=PathPrefix(`/api`) || PathPrefix(`/admin`) || PathPrefix(`/static`)" + - "traefik.http.routers.trak.entrypoints=web" command: | sh -c " python manage.py makemigrations && python manage.py migrate && python manage.py loaddata core_dev.yaml rcrasite_dev.yaml org_dev.yaml profile_dev.yaml && python manage.py runserver 0.0.0.0:8000" - healthcheck: - test: - ["CMD", "curl", "-f", "http://localhost:8000/api/health/startup-probe/"] - interval: 30s - timeout: 20s - retries: 5 - start_period: 10s depends_on: db: condition: service_healthy @@ -71,9 +69,6 @@ services: HT_DB_HOST: db HT_DB_PORT: ${HT_DB_PORT} command: python manage.py celery_worker - depends_on: - trak: - condition: service_healthy celery-beat: restart: unless-stopped @@ -94,9 +89,6 @@ services: HT_DB_HOST: db HT_DB_PORT: ${HT_DB_PORT} command: python manage.py celery_beat - depends_on: - trak: - condition: service_healthy db: restart: unless-stopped @@ -129,6 +121,10 @@ services: VITE_HT_API_URL: ${VITE_HT_API_URL} VITE_HT_ENV: ${VITE_HT_ENV} VITE_GITHUB_URL: ${VITE_GITHUB_URL} + labels: + - "traefik.enable=true" + - "traefik.http.routers.client.rule=Host(`${HT_HOST}`)" + - "traefik.http.routers.client.entrypoints=web" command: sh -c "cd /app; npm install; npm start" proxy: @@ -137,11 +133,14 @@ services: command: - "--api.insecure=true" - "--providers.docker=true" + - "--log.level=DEBUG" + - "--providers.docker.exposedbydefault=false" + - "--entrypoints.web.address=:80" ports: - "8080:8080" + - "80:80" volumes: - - ./traefik.toml:/etc/traefik/traefik.toml - - /var/run/docker.sock:/var/run/docker.sock + - "/var/run/docker.sock:/var/run/docker.sock:ro" mail: container_name: mail diff --git a/configs/.env.dev b/configs/.env.dev index c20075836..01f5016b4 100644 --- a/configs/.env.dev +++ b/configs/.env.dev @@ -5,7 +5,7 @@ HT_SECRET_KEY='django-insecure-%btjqoun@6ps$e@8bw$48s+!x1e4aiz&5p2nrf6cmiw4)jsx5d' HT_HOST=localhost HT_TIMEZONE=America/New_York -HT_CORS_DOMAIN=http://localhost:8080 +HT_CORS_DOMAIN=http://localhost HT_RCRAINFO_ENV=preprod HT_CACHE_URL=redis://redis:6379 HT_SIGNING_KEY=0dd3f4e68730bedfb07e6bc2e8f00a56c4db2d4a4b37e64ac0a83b8c97ec55dd @@ -31,6 +31,6 @@ CELERY_RESULT_BACKEND=django-db CELERY_BROKER_URL=redis://redis:6379 ### React App configs -VITE_HT_API_URL=http://localhost:8080 +VITE_HT_API_URL=http://localhost VITE_HT_ENV=DEV VITE_GITHUB_URL=https://github.com/USEPA/haztrak diff --git a/docs/guide/src/design/server.md b/docs/guide/src/design/server.md index b99ef0d84..2e870a64a 100644 --- a/docs/guide/src/design/server.md +++ b/docs/guide/src/design/server.md @@ -1,10 +1,13 @@ ## Server and Rest API -The REST API is the primary way users read and change data in the Haztrak system. It is implemented using Python, the [Django Framework](https://www.djangoproject.com/), and [Django Rest Framework](https://www.django-rest-framework.org/). +The REST API is the primary way users read and change data in the Haztrak system. It is implemented using Python, +the [Django Framework](https://www.djangoproject.com/), +and [Django Rest Framework](https://www.django-rest-framework.org/). The choice to use Python stemmed from a desire to use a language that the team was already familiar with, something that is easy to learn (regardless of whether Python is your primary language, it's easy to read). -The choice to use Django was made because it is a mature, well-documented, and widely used framework that provides a lot of functionality out of the box. +The choice to use Django was made because it is a mature, well-documented, and widely used framework that provides a lot +of functionality out of the box. Many Django developer have turned to what they call the 'service layer', which usually encapsulated the use cases business logic. It's a contentious topic whether the policy/business logic should be @@ -25,8 +28,10 @@ The Admin site provides a quick, model-centric interface where trusted users can manage content. It's not intended to provide a process centric interface, admin user's should not be, for example, signing manifests through the admin site. -The admin interface is an out-of-the-box feature of the [Django framework](https://docs.djangoproject.com/en/4.1/ref/contrib/admin/). -It can be found by appending `/admin` to the URL of the host and port of HTTP server, for example `http://localhost:8000/admin` +The admin interface is an out-of-the-box feature of +the [Django framework](https://docs.djangoproject.com/en/4.1/ref/contrib/admin/). +It can be found by appending `/admin` to the URL of the host and port of HTTP server, for example +`http://localhost/admin` ## In-memory Database @@ -35,6 +40,9 @@ The in-memory data store serves a couple purposes, - As a message broker for Haztrak's [task queue](./task-queue.md) - A cache for the [http server](./http-server.md) -As a cache, the in-memory data store is utilized to increase performance by allowing Haztrak to cut down on latency for recently used resources including recent database queries, computed values, and external service calls. As a message broker, the data store provides a reliable way for the back end service to communicate which each other (e.g., launch background tasks). +As a cache, the in-memory data store is utilized to increase performance by allowing Haztrak to cut down on latency for +recently used resources including recent database queries, computed values, and external service calls. As a message +broker, the data store provides a reliable way for the back end service to communicate which each other (e.g., launch +background tasks). The Haztrak project currently uses [Redis](https://redis.io/) as both the message broker and in-memory data store. diff --git a/docs/guide/src/development/local-development.md b/docs/guide/src/development/local-development.md index 7bafc0f1b..621d5adf9 100644 --- a/docs/guide/src/development/local-development.md +++ b/docs/guide/src/development/local-development.md @@ -42,7 +42,7 @@ that we've specified in the [docker-compose.yaml](https://github.com/USEPA/haztrak/blob/main/docker-compose.yaml) file._ After all containers are successfully running (you can inspect with `docker ps`), -visit [localhost:8080](http://localhost:8080) +visit [localhost:8080](http://localhost) ## Fixtures (logging in) diff --git a/docs/guide/src/operations/configuration.md b/docs/guide/src/operations/configuration.md index 875300a83..77f93bffa 100644 --- a/docs/guide/src/operations/configuration.md +++ b/docs/guide/src/operations/configuration.md @@ -63,7 +63,7 @@ The follow variables are required, haztrak will exit if not present. the [e-Manifest API Client Library](https://github.com/USEPA/e-manifest/tree/master/emanifest-py) - `HT_CORS_DOMAIN` - Value for cross-origin resource sharing, the domain that the React app is deployed - - Example for local development: should be something like `http://localhost:3000` + - Example for local development: should be something like `http://localhost` - `HT_CACHE_URL` - URL that points to the cache, in this instance Redis - Example for local development: should be something like `redis://localhost:6379` @@ -137,7 +137,7 @@ will default to `HT_LOG_LEVEL`s default. - `VITE_HT_API_URL` - Value: host/domain name of the haztrak back end - - Default: `http://localhost:8000` + - Default: `http://localhost` - `VITE_HT_ENV` - Default: `PROD` - Options: `PROD`, `DEV`, `TEST` diff --git a/server/haztrak/settings/dev.py b/server/haztrak/settings/dev.py index 9facc1fc3..ebdc23df6 100644 --- a/server/haztrak/settings/dev.py +++ b/server/haztrak/settings/dev.py @@ -5,7 +5,7 @@ # General SECRET_KEY = "django-insecure-%btjqoun@6ps$e@8bw$48s+!x1e4aiz&5p2nrf6cmiw4)jsx5d" DEBUG = True -CORS_ORIGIN_WHITELIST = [os.getenv("HT_CORS_DOMAIN", "http://localhost:3000")] +CORS_ORIGIN_WHITELIST = [os.getenv("HT_CORS_DOMAIN", "http://localhost")] REST_FRAMEWORK["DEFAULT_RENDERER_CLASSES"] = [ # noqa: F405 "rest_framework.renderers.JSONRenderer", diff --git a/traefik.toml b/traefik.toml deleted file mode 100644 index a5911de1a..000000000 --- a/traefik.toml +++ /dev/null @@ -1,43 +0,0 @@ -defaultEntryPoints = ["http"] - -[log] -level = "DEBUG" - -[api] -dashboard = true - -[accessLog] - - -[providers] -providersThrottleDuration = 10 -[providers.file] -filename = "traefik.toml" -directory = "/etc/traefik/" -watch = true - - -[entryPoints] -[entryPoints.web] -address = ":8080" - -[http.routers] - -[http.routers.django] -service = "django" -rule = "PathPrefix(`/api`) || PathPrefix(`/admin`) || PathPrefix(`/static`)" -entrypoints = ["web"] - - -[http.routers.react] -service = "react" -rule = "PathPrefix(`/`)" -entrypoints = ["web"] - -[http.services] -[http.services.react.loadBalancer] -[[http.services.react.loadBalancer.servers]] -url = "http://client:3000" -[http.services.django.loadBalancer] -[[http.services.django.loadBalancer.servers]] -url = "http://trak:8000"