Skip to content

Commit

Permalink
Use .env config file according to NODE_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
itaisteinherz committed Feb 12, 2019
1 parent b3c8a0f commit d9634ce
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
yarn.lock
node_modules
.next
.env
*.env
!sample.env
4 changes: 3 additions & 1 deletion client/firebase/firebase-api.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 4 additions & 1 deletion client/http.js
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
16 changes: 15 additions & 1 deletion client/next.config.js
Original file line number Diff line number Diff line change
@@ -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"
}
});
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ RUN npm install --production

EXPOSE 5000

ARG STAGE="production"
ENV NODE_ENV=$STAGE

CMD [ "npm", "start" ]
8 changes: 4 additions & 4 deletions server/cloud/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
});

Expand Down
9 changes: 7 additions & 2 deletions server/components/tags/tag-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
13 changes: 10 additions & 3 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -18,5 +25,5 @@ module.exports = {
client_x509_cert_url: clientCertUrl
},
databaseUrl,
clientUrl: "http://knowledge2019.herokuapp.com"
clientUrl
};
3 changes: 2 additions & 1 deletion server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
version: "2"
services:
server:
build: .
build:
context: .
volumes:
- ./:/server
ports:
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions server/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ clientEmail="EMAIL"
clientId="CLIENT_ID"
clientCertUrl="CERT_URL"
databaseUrl="DB_URL"
clientUrl="FRONTEND_CLIENT_URL"

0 comments on commit d9634ce

Please sign in to comment.