Internal guide for developing, deploying, and managing the Currency Service on Google Cloud.
- Database: Google Cloud Firestore (Native Mode).
- Service: Cloud Run (Node.js/Fastify) for HTTP API.
- Background Jobs: Cloud Run Jobs for scheduled tasks (e.g., fetching rates).
- Schema:
currencies: Collection of currency definitions including translations.exchange_rates: Collection of daily exchange rates.
Ensure you have the Google Cloud SDK installed and authenticated.
# Login to Google Cloud
gcloud auth login
# Set your project ID
gcloud config set project auto-currency-service # Replace with actual project ID if different-
Install Dependencies
npm install
-
Environment Setup Create a
.envfile (see.env.exampleif available) with:PORT=3000 PROJECT_ID=your-project-id EXCHANGE_API_KEY=your-api-key APP_KEY=your-secret-key
Note: All API requests must include the
x-app-keyheader with the value ofAPP_KEY. -
Authentication For local development (accessing Firestore), use Application Default Credentials or a Service Account Key.
gcloud auth application-default login
If that fails due to policy, use a service account key:
export GOOGLE_APPLICATION_CREDENTIALS="./service-account.json"
-
Run Locally
npm start
Example request:
curl -H "x-app-key: <APP_KEY>" http://localhost:3000/currencies
Deploys the Fastify API to Cloud Run. The script automatically syncs .env variables to Cloud Run.
./deploy.sh <PROJECT_ID>
# Example
./deploy.sh project-5b899a43-ec93-4625-ae9Deploys the fetchRates.js script as a Cloud Run Job.
./deploy_job.sh <PROJECT_ID>View details about the running Web Service.
gcloud run services describe currency-service --region us-central1View details about the Background Job.
gcloud run jobs describe fetch-rates-job --region us-central1Trigger the rates update manually (e.g., if it failed or you need immediate data).
gcloud run jobs execute fetch-rates-job --region us-central1View logs for the service or job.
# Stream logs for the service
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=currency-service" --limit 20 --format "value(textPayload)"
# Stream logs for the job
gcloud logging read "resource.type=cloud_run_job AND resource.labels.job_name=fetch-rates-job" --limit 20 --format "value(textPayload)"If the service fails to start with "Missing permissions" to Firestore:
- Ensure the Cloud Run Service Account has the
Cloud Datastore Userrole. - Run the fix script:
./fix_permissions.sh <PROJECT_ID>
If npm start fails locally:
lsof -i :3000
kill -9 <PID>If you need to re-import data from SQL dump:
node scripts/migrate_sql_to_firestore.js