PUBLIC DIRECTORY FASTAPI — INSTALLATION

1) CREATE API DIRECTORY
/usr/bin/mkdir -p /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api
/usr/bin/chown -R mostincr:mostincr /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api

Copy app.py, create_api_key.py, requirements.txt, api_schema.sql into that directory.

2) CREATE VENV
/usr/bin/python3 -m venv /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/.venv
/home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/.venv/bin/python -m pip install --upgrade pip
/home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/.venv/bin/python -m pip install -r /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/requirements.txt

3) CREATE API TABLES
/usr/bin/mysql -u directory_api -p public_directory < /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/api_schema.sql

4) CREATE EXPORT DIRECTORY
/usr/bin/mkdir -p /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/exports
/usr/bin/chown mostincr:mostincr /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/exports
/usr/bin/chmod 700 /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/exports

5) CREATE ENV FILE
Generate secret:
/usr/bin/openssl rand -hex 32

Copy .env.example to /etc/public-directory-api.env and set the real DB password and signing secret.
Then:
/usr/bin/chown root:root /etc/public-directory-api.env
/usr/bin/chmod 600 /etc/public-directory-api.env

6) MANUAL TEST
/usr/bin/bash -c 'set -a; source /etc/public-directory-api.env; set +a; exec /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/.venv/bin/uvicorn app:app --app-dir /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api --host 127.0.0.1 --port 8086'

In another shell:
/usr/bin/curl http://127.0.0.1:8086/health

7) CREATE FIRST API KEY
/usr/bin/bash -c 'set -a; source /etc/public-directory-api.env; set +a; exec /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/.venv/bin/python /home/mostincr/maillists.mostincrediblevalue.com/data/public-directory/api/create_api_key.py --name "Replit Production" --rate-limit 120 --max-export-records 100000'

Save the printed key. It is shown only once.

8) TEST SEARCH
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/search?city=ACUSHNET&page_size=5"
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/search?zip=02743&page_size=5"
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/search?q=ALVES%20ACUSHNET&page_size=10"

9) TEST AUTOCOMPLETE
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/autocomplete/cities?q=ACU"
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/autocomplete/zips?q=0274"
/usr/bin/curl -H "X-API-Key: YOUR_KEY" "http://127.0.0.1:8086/v1/autocomplete/streets?q=MYRT&city=ACUSHNET"

10) TEST COUNT
/usr/bin/curl -X POST -H "X-API-Key: YOUR_KEY" -H "Content-Type: application/json" -d '{"filters":{"city":"ACUSHNET","zip":"02743"}}' http://127.0.0.1:8086/v1/search/count

11) TEST EXPORT
/usr/bin/curl -X POST -H "X-API-Key: YOUR_KEY" -H "Content-Type: application/json" -d '{"order_id":"test-order-001","search_snapshot":{"city":"ACUSHNET","zip":"02743"},"record_quantity":100}' http://127.0.0.1:8086/v1/exports

Then poll:
/usr/bin/curl -H "X-API-Key: YOUR_KEY" http://127.0.0.1:8086/v1/exports/EXPORT_ID

12) SYSTEMD
Copy public-directory-api.service to /etc/systemd/system/public-directory-api.service
/usr/bin/systemctl daemon-reload
/usr/bin/systemctl enable --now public-directory-api.service
/usr/bin/systemctl status public-directory-api.service

13) REVERSE PROXY
Create api.maillists.mostincrediblevalue.com and proxy HTTPS to 127.0.0.1:8086.
On cPanel/WHM use a supported Apache include, not a generated vhost file.

14) REPLIT SECRETS
PUBLIC_DIRECTORY_API_BASE_URL=https://api.maillists.mostincrediblevalue.com
PUBLIC_DIRECTORY_SERVER_API_KEY=<created key>

Keep the API key server-side only.
