Skip to content

Commit

Permalink
env-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 17, 2020
1 parent d86acc9 commit 947ecac
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 8 deletions.
5 changes: 5 additions & 0 deletions server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/lireddit2
REDIS_URL=127.0.0.1:6379
PORT=4000
SESSION_SECRET=qowiueojwojfalksdjoqiwueo
CORS_ORIGIN=http://localhost:3000
5 changes: 5 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DATABASE_URL=
REDIS_URL=
PORT=
SESSION_SECRET=
CORS_ORIGIN=
2 changes: 2 additions & 0 deletions server/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SESSION_SECRET=qwoiuweqoiuwqeoiusdalkjwqoi
CORS_ORIGIN=http://localhost:3000
4 changes: 3 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist
node_modules
node_modules
# you should add that
# .env
1 change: 1 addition & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ COPY yarn.lock ./
RUN yarn

COPY . .
COPY .env.production .env

RUN yarn build

Expand Down
3 changes: 3 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"gen-env": "gen-env-types .env -o src/env.d.ts -e .",
"build": "tsc",
"watch": "tsc -w",
"dev": "nodemon dist/index.js",
Expand All @@ -24,6 +25,7 @@
"@types/nodemailer": "^6.4.0",
"@types/redis": "^2.8.25",
"@types/uuid": "^8.0.1",
"gen-env-types": "^1.0.3",
"nodemon": "^2.0.4",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
Expand All @@ -34,6 +36,7 @@
"connect-redis": "^5.0.0",
"cors": "^2.8.5",
"dataloader": "^2.0.0",
"dotenv-safe": "^8.2.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
"graphql": "^15.3.0",
Expand Down
9 changes: 9 additions & 0 deletions server/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare namespace NodeJS {
export interface ProcessEnv {
DATABASE_URL: string;
REDIS_URL: string;
PORT: string;
SESSION_SECRET: string;
CORS_ORIGIN: string;
}
}
14 changes: 7 additions & 7 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "reflect-metadata";
import "dotenv-safe/config";
import { __prod__, COOKIE_NAME } from "./constants";
import express from "express";
import { ApolloServer } from "apollo-server-express";
Expand All @@ -21,9 +22,7 @@ import { createUpdootLoader } from "./utils/createUpdootLoader";
const main = async () => {
const conn = await createConnection({
type: "postgres",
database: "lireddit2",
username: "postgres",
password: "postgres",
url: process.env.DATABASE_URL,
logging: true,
synchronize: true,
migrations: [path.join(__dirname, "./migrations/*")],
Expand All @@ -36,10 +35,10 @@ const main = async () => {
const app = express();

const RedisStore = connectRedis(session);
const redis = new Redis();
const redis = new Redis(process.env.REDIS_URL);
app.use(
cors({
origin: "http://localhost:3000",
origin: process.env.CORS_ORIGIN,
credentials: true,
})
);
Expand All @@ -55,9 +54,10 @@ const main = async () => {
httpOnly: true,
sameSite: "lax", // csrf
secure: __prod__, // cookie only works in https
domain: __prod__ ? ".codeponder.com" : undefined,
},
saveUninitialized: false,
secret: "qowiueojwojfalksdjoqiwueo",
secret: process.env.SESSION_SECRET,
resave: false,
})
);
Expand All @@ -81,7 +81,7 @@ const main = async () => {
cors: false,
});

app.listen(4000, () => {
app.listen(parseInt(process.env.PORT), () => {
console.log("server started on localhost:4000");
});
};
Expand Down
27 changes: 27 additions & 0 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ chalk@^3.0.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chokidar@^3.2.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.4.2.tgz#38dc8e658dec3809741eb3ef7bb0a47fe424232d"
Expand Down Expand Up @@ -1095,11 +1103,23 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv-safe@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv-safe/-/dotenv-safe-8.2.0.tgz#8d548c7318a62c09a66c4dc8c31864cc007c78ba"
integrity sha512-uWwWWdUQkSs5a3mySDB22UtNwyEYi0JtEQu+vDzIqr9OjbDdC2Ip13PnSpi/fctqlYmzkxCeabiyCAOROuAIaA==
dependencies:
dotenv "^8.2.0"

dotenv@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064"
integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w==

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

duplexer3@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
Expand Down Expand Up @@ -1343,6 +1363,13 @@ gauge@~2.7.3:
strip-ansi "^3.0.1"
wide-align "^1.1.0"

gen-env-types@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/gen-env-types/-/gen-env-types-1.0.3.tgz#f19a0bcf6afcb09131395bce5446997e02570219"
integrity sha512-Qxwasw54sn9EY8r34DTej92pWG78agRxRMc9rPSy+sRJLMWTxItoCC4fS2iC0fYCdn1E7auGMjHEk7HwJ3KSYA==
dependencies:
chalk "^4.0.0"

get-caller-file@^2.0.1:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
Expand Down

0 comments on commit 947ecac

Please sign in to comment.