From d9634ceb3dda7e86625f536ebb51df0cf9caec2e Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 12 Feb 2019 17:50:34 +0200 Subject: [PATCH] Use `.env` config file according to `NODE_ENV` --- .gitignore | 3 ++- client/firebase/firebase-api.js | 4 +++- client/http.js | 5 ++++- client/next.config.js | 16 +++++++++++++++- client/package.json | 2 +- package.json | 2 +- readme.md | 2 +- server/Dockerfile | 3 +++ server/cloud/lib/stack.ts | 8 ++++---- server/components/tags/tag-controllers.js | 9 +++++++-- server/config.js | 13 ++++++++++--- server/docker-compose.yml | 3 ++- server/index.js | 2 +- server/package.json | 1 + server/sample.env | 1 + 15 files changed, 56 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 8a74b0e..b7471e0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ yarn.lock node_modules .next -.env +*.env +!sample.env diff --git a/client/firebase/firebase-api.js b/client/firebase/firebase-api.js index 0c6e0f2..2c1e480 100644 --- a/client/firebase/firebase-api.js +++ b/client/firebase/firebase-api.js @@ -1,5 +1,7 @@ import firebase from "firebase/app"; +import getConfig from "next/config"; -import {credentials} from "../config"; +const {publicRuntimeConfig} = getConfig(); +const {credentials} = publicRuntimeConfig; export default firebase.apps.length > 0 ? firebase.app() : firebase.initializeApp(credentials); diff --git a/client/http.js b/client/http.js index 82e029d..5d2c263 100644 --- a/client/http.js +++ b/client/http.js @@ -1,4 +1,7 @@ -import {apiUrl} from "./config"; +import getConfig from "next/config"; + +const {publicRuntimeConfig} = getConfig(); +const {apiUrl} = publicRuntimeConfig; const requestUrl = path => new URL(path, apiUrl); diff --git a/client/next.config.js b/client/next.config.js index 8c50698..19e1561 100644 --- a/client/next.config.js +++ b/client/next.config.js @@ -1,3 +1,17 @@ const withCss = require("@zeit/next-css"); -module.exports = withCss({}); +const stage = process.env.NODE_ENV || "production"; + +module.exports = withCss({ + publicRuntimeConfig: { + credentials: { + apiKey: "AIzaSyCpU4SIDfsgqZ2bBaoDaQza4I-0fsocfR8", + authDomain: "react-firebase-85039.firebaseapp.com", + databaseURL: "https://react-firebase-85039.firebaseio.com", + projectId: "react-firebase-85039", + storageBucket: "react-firebase-85039.appspot.com", + messagingSenderId: "55358337129" + }, + apiUrl: stage === "production" ? "http://knowl-knowl-ciw3basidwqs-321112760.eu-central-1.elb.amazonaws.com" : "http://localhost:5000" + } +}); diff --git a/client/package.json b/client/package.json index 226bfba..0fb89bf 100644 --- a/client/package.json +++ b/client/package.json @@ -6,7 +6,7 @@ "node": ">=8" }, "scripts": { - "dev": "next dev", + "dev": "NODE_ENV=\"development\" next dev", "start": "next start", "build": "next build", "heroku:start": "next start --port $PORT", diff --git a/package.json b/package.json index d7cefa0..b9fb2d0 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "dev:client": "cd client && npm run dev", "start:client": "cd client && npm run build && npm start", "dev:server": "npm run start:server", - "start:server": "cd server && npm run compose:start", + "start:server": "cd server && npm run compose:build && npm run compose:start", "start": "concurrently npm:start:*", "dev": "concurrently npm:dev:*", "install:client": "cd client && npm install", diff --git a/readme.md b/readme.md index fb7bb5d..0dc22a7 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ See [the MVP spec](docs/mvp.md). ### Getting started -First of all, obtain your Firebase SDK service account credentials (more info on this can be found in [this guide](https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app)). Rename `server/sample.env` to `.env` (keep it in the `server` directory), and replace the placeholders with your credentials. +First of all, obtain your Firebase SDK service account credentials (more info on this can be found in [this guide](https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app)). Rename `server/sample.env` to `prod.env` (keep it in the `server` directory), and replace the placeholders with your credentials. Then, run `npm run dev` to start the app in development mode or `npm start` to run it in production mode. It should then be running on http://localhost:3000, with the backend API running on http://localhost:5000. diff --git a/server/Dockerfile b/server/Dockerfile index a29002b..7a8f545 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -9,4 +9,7 @@ RUN npm install --production EXPOSE 5000 +ARG STAGE="production" +ENV NODE_ENV=$STAGE + CMD [ "npm", "start" ] diff --git a/server/cloud/lib/stack.ts b/server/cloud/lib/stack.ts index 4afca73..96e4255 100644 --- a/server/cloud/lib/stack.ts +++ b/server/cloud/lib/stack.ts @@ -6,20 +6,20 @@ export class KnowledgeStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); - // Create VPC and Fargate Cluster - // NOTE: Limit AZs to avoid reaching resource quotas + // Create VPC and Fargate Cluster. + // NOTE: Limit AZs to avoid reaching resource quotas. const vpc = new ec2.VpcNetwork(this, "KnowledgeVpc", { maxAZs: 2 }); const cluster = new ecs.Cluster(this, "KnowledgeCluster", {vpc}); - // Instantiate Fargate Service with just cluster and image + // Instantiate Fargate Service with cluster and Docker image. const fargateService = new ecs.LoadBalancedFargateService(this, "KnowledgeService", { cluster, image: ecs.ContainerImage.fromAsset(this, "KnowledgeImage", { directory: ".." }), - containerPort: 5000, + containerPort: 5000, // Expose port 5000, on which the server listens. desiredCount: 2 // Meant to make sure at least 1 task is always running. }); diff --git a/server/components/tags/tag-controllers.js b/server/components/tags/tag-controllers.js index 44de8c0..ae1d138 100644 --- a/server/components/tags/tag-controllers.js +++ b/server/components/tags/tag-controllers.js @@ -24,14 +24,19 @@ exports.getTags = async (req, res) => { exports.getTagQuestions = async (req, res) => { const {tagName} = req.params; - const {_id: tagId} = await Tag.findOne({name: { + const tag = await Tag.findOne({name: { $regex: tagName, $options: "i" }}).select("_id"); + if (!tag || !tag._id) { + res.status(404).json({error: "Couldn't find a tag matching the given query."}); + return; + } + try { const questions = await Question - .find({tags: tagId}) + .find({tags: tag._id}) .sort({createdAt: "desc"}) .limit(20) .select("-__v"); diff --git a/server/config.js b/server/config.js index 0ef3923..ed9c254 100644 --- a/server/config.js +++ b/server/config.js @@ -1,8 +1,15 @@ +const path = require("path"); + const dotenv = require("dotenv"); -dotenv.config(); +console.log("env", process.env.NODE_ENV); + +const stage = process.env.NODE_ENV || "production"; +dotenv.config({ + path: path.resolve(process.cwd(), `${stage === "production" ? "prod" : "dev"}.env`) +}); -const {projectId, privateKeyId, privateKey, clientEmail, clientId, clientCertUrl, databaseUrl} = process.env; +const {projectId, privateKeyId, privateKey, clientEmail, clientId, clientCertUrl, databaseUrl, clientUrl} = process.env; module.exports = { credentials: { @@ -18,5 +25,5 @@ module.exports = { client_x509_cert_url: clientCertUrl }, databaseUrl, - clientUrl: "http://knowledge2019.herokuapp.com" + clientUrl }; diff --git a/server/docker-compose.yml b/server/docker-compose.yml index 308d49a..01603f5 100644 --- a/server/docker-compose.yml +++ b/server/docker-compose.yml @@ -1,7 +1,8 @@ version: "2" services: server: - build: . + build: + context: . volumes: - ./:/server ports: diff --git a/server/index.js b/server/index.js index 95479fb..235f8d2 100644 --- a/server/index.js +++ b/server/index.js @@ -17,7 +17,7 @@ const app = express(); const firebase = admin.initializeApp({ credential: admin.credential.cert(credentials), - databaseURL: "https://react-firebase-85039.firebaseio.com" // TODO: Move this setting to `config.js`. + databaseURL: `https://${credentials.project_id}.firebaseio.com` // TODO: Move this setting to `config.js`. }, "server"); // Setting a few options to remove warnings on feature deprecations. diff --git a/server/package.json b/server/package.json index 1665ae8..499320c 100644 --- a/server/package.json +++ b/server/package.json @@ -8,6 +8,7 @@ }, "scripts": { "start": "node index.js", + "compose:build": "docker-compose build --build-arg STAGE=\"development\" server", "compose:start": "docker-compose up", "docker:build": "docker build -t knowledge-server .", "docker:run": "docker run -d --name knowledge-server -p 5000:5000 knowledge-server", diff --git a/server/sample.env b/server/sample.env index a18cf46..e82f30c 100644 --- a/server/sample.env +++ b/server/sample.env @@ -5,3 +5,4 @@ clientEmail="EMAIL" clientId="CLIENT_ID" clientCertUrl="CERT_URL" databaseUrl="DB_URL" +clientUrl="FRONTEND_CLIENT_URL"