feat(api): validate environment variables before use (#49613)

* feat(api): validate environment variables before use

This is similar in concept to ensure-env, but a little simpler since
there is no need to put the data in a file before the api can consume
it.

* refactor: combine the two env files
This commit is contained in:
Oliver Eyton-Williams
2023-03-07 19:03:46 +01:00
committed by GitHub
parent 0393910a24
commit 5f59b2b8c7
3 changed files with 43 additions and 9 deletions
+3 -3
View File
@@ -2,12 +2,12 @@ import fastifyPlugin from 'fastify-plugin';
import fastifyMongo from '@fastify/mongodb';
import { FastifyInstance } from 'fastify';
const URI = process.env.MONGOHQ_URL || 'mongodb://localhost:27017/freecodecamp';
import { MONGOHQ_URL } from '../utils/env';
async function connect(fastify: FastifyInstance) {
fastify.log.info(`Connecting to : ${URI}`);
fastify.log.info(`Connecting to Mongodb`);
await fastify.register(fastifyMongo, {
url: URI
url: MONGOHQ_URL
});
}
+5 -6
View File
@@ -1,5 +1,3 @@
import { config } from 'dotenv';
config({ path: '../.env' });
import fastifyAuth0 from 'fastify-auth0-verify';
import Fastify from 'fastify';
import middie from '@fastify/middie';
@@ -8,9 +6,10 @@ import jwtAuthz from './plugins/fastify-jwt-authz';
import { testRoutes } from './routes/test';
import { dbConnector } from './db';
import { auth0Verify, testMiddleware } from './middleware';
import { AUTH0_AUDIENCE, AUTH0_DOMAIN, NODE_ENV, PORT } from './utils/env';
const fastify = Fastify({
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
logger: { level: NODE_ENV === 'development' ? 'debug' : 'fatal' }
});
fastify.get('/', async (_request, _reply) => {
@@ -23,8 +22,8 @@ const start = async () => {
// Auth0 plugin
void fastify.register(fastifyAuth0, {
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE
domain: AUTH0_DOMAIN,
audience: AUTH0_AUDIENCE
});
void fastify.register(jwtAuthz);
@@ -37,7 +36,7 @@ const start = async () => {
void fastify.register(testRoutes);
try {
const port = Number(process.env.PORT) || 3000;
const port = Number(PORT);
fastify.log.info(`Starting server on port ${port}`);
await fastify.listen({ port });
} catch (err) {
+35
View File
@@ -0,0 +1,35 @@
import assert from 'node:assert';
import path from 'node:path';
import { config } from 'dotenv';
const envPath = path.resolve(__dirname, '../../.env');
const { error } = config({ path: envPath });
if (error) {
console.warn(`
----------------------------------------------------
Warning: .env file not found.
----------------------------------------------------
Please copy sample.env to .env
You can ignore this warning if using a different way
to setup this environment.
----------------------------------------------------
`);
}
assert.ok(process.env.NODE_ENV);
assert.ok(process.env.AUTH0_DOMAIN);
assert.ok(process.env.AUTH0_AUDIENCE);
if (process.env.NODE_ENV !== 'development') {
assert.ok(process.env.PORT);
assert.ok(process.env.MONGOHQ_URL);
}
export const MONGOHQ_URL =
process.env.MONGOHQ_URL || 'mongodb://localhost:27017/freecodecamp';
export const NODE_ENV = process.env.NODE_ENV;
export const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN;
export const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE;
export const PORT = process.env.PORT || '3000';