Skip to content

Commit

Permalink
Dev and Docker mode now working both locally and in prod environments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispinkney committed Mar 28, 2021
1 parent be88475 commit 3aa1840
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
4 changes: 2 additions & 2 deletions config/env.development
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ IMAGE_URL=http://localhost/v1/image
USERS_PORT=6666

# Users Service URL
USERS_URL=http://localhost/v1/user
FIRESTORE_EMULATOR_HOST=localhost:8088
USERS_URL=http://localhost/v1/users
FIRESTORE_EMULATOR_HOST=firebase:8088


################################################################################
Expand Down
7 changes: 7 additions & 0 deletions docker/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ services:
elasticsearch:
ports:
- '9200:9200'

# Users
users:
ports:
- ${USERS_PORT}:${USERS_PORT}
environment:
- FIRESTORE_EMULATOR_HOST
2 changes: 0 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ services:
dockerfile: Dockerfile
environment:
- USERS_PORT
ports:
- ${USERS_PORT}
depends_on:
- traefik
labels:
Expand Down
15 changes: 4 additions & 11 deletions docker/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,17 @@ services:

# users service
users:
ports:
- ${USERS_PORT}
restart: unless-stopped
environment:
- NODE_ENV=production
- USERS_PORT
- FIREBASE_TYPE
- FIREBASE_PROJECT_ID
- FIREBASE_PRIVATE_KEY_ID
- FIREBASE_PRIVATE_KEY
- FIREBASE_CLIENT_EMAIL
- FIREBASE_CLIENT_ID
- FIREBASE_AUTH_URI
- FIREBASE_TOKEN_URI
- FIREBASE_AUTH_PROVIDER_X509_CERT_URL
- FIREBASE_CLIENT_X509_CERT_URL
# TODO
# - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
# - ELASTIC_APM_SERVICE_NAME=user
# - ELASTIC_APM_SERVER_URL=http://apm:8200
volumes:
- ../../config/firebase:/app

##############################################################################
# Third-Party Dependencies and Support Services
Expand Down
11 changes: 6 additions & 5 deletions src/api/users/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ npm install:users-service
# normal mode
npm start
# running firestore emulator locally
# running firestore emulator and users microservice locally
npm run services:start firebase users
# dev mode with automatic restarts
npm run services:start firebase
npm start
cd src/api/users
npm run dev
# test runner (must be used in conjunction with the firebase service)
npm run jest:e2e (or npm run jest:e2e src\api\users\test\e2e)
# dev mode with automatic restarts
npm run dev
```

By default the server is running on <http://localhost:6666/>.
Expand Down
51 changes: 35 additions & 16 deletions src/api/users/src/services/firestore.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
const admin = require('firebase-admin');
const { logger } = require('@senecacdot/satellite');
const fs = require('fs');
const path = require('path');

const {
FIREBASE_TYPE,
FIREBASE_PROJECT_ID,
FIREBASE_PRIVATE_KEY_ID,
FIREBASE_PRIVATE_KEY,
FIREBASE_CLIENT_EMAIL,
FIREBASE_CLIENT_ID,
FIREBASE_AUTH_URI,
FIREBASE_TOKEN_URI,
FIREBASE_AUTH_PROVIDER_X509_CERT_URL,
FIREBASE_CLIENT_X509_CERT_URL,
} = process.env;
let FIREBASE_TYPE;
let FIREBASE_PROJECT_ID;
let FIREBASE_PRIVATE_KEY_ID;
let FIREBASE_PRIVATE_KEY;
let FIREBASE_CLIENT_EMAIL;
let FIREBASE_CLIENT_ID;
let FIREBASE_AUTH_URI;
let FIREBASE_TOKEN_URI;
let FIREBASE_AUTH_PROVIDER_X509_CERT_URL;
let FIREBASE_CLIENT_X509_CERT_URL;

try {
const privateKey = fs.readFileSync(path.join('/app', 'serviceAccountKey.json'));
const values = JSON.parse(privateKey);

FIREBASE_TYPE = values.type;
FIREBASE_PROJECT_ID = values.project_id;
FIREBASE_PRIVATE_KEY_ID = values.private_key_id;
FIREBASE_PRIVATE_KEY = values.private_key;
FIREBASE_CLIENT_EMAIL = values.client_email;
FIREBASE_CLIENT_ID = values.client_id;
FIREBASE_AUTH_URI = values.auth_uri;
FIREBASE_TOKEN_URI = values.token_uri;
FIREBASE_AUTH_PROVIDER_X509_CERT_URL = values.auth_provider_x509_cert_url;
FIREBASE_CLIENT_X509_CERT_URL = values.client_x509_cert_url;
} catch (err) {
logger.debug(err);
}

// If a private key exists in env.development, run in online mode
// else run in emulator mode
if (
(FIREBASE_PRIVATE_KEY,
(FIREBASE_TYPE,
FIREBASE_PROJECT_ID,
FIREBASE_PRIVATE_KEY_ID,
FIREBASE_PRIVATE_KEY,
Expand All @@ -42,14 +60,15 @@ if (
client_x509_cert_url: FIREBASE_CLIENT_X509_CERT_URL,
}),
});
logger.info('Server running in online mode');
logger.debug('Server running in online mode');
} else {
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8088';
// hardcode the environment variable if we're not using docker
if (!process.env.FIRESTORE_EMULATOR_HOST) process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8088';
admin.initializeApp({
projectId: 'telescope',
credential: admin.credential.applicationDefault(),
});
logger.info('Server running in emulator mode');
logger.debug('Server running in emulator mode');
}

module.exports = admin.firestore();

0 comments on commit 3aa1840

Please sign in to comment.