mirror of
https://github.com/andwati/order-service.git
synced 2026-12-02 13:38:46 +00:00
feat: add environment configuration and required variables
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
NODE_ENV=
|
||||||
|
PORT=
|
||||||
|
MONGO_URI=
|
||||||
|
JWT_SECRET=
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
"name": "order-service",
|
"name": "order-service",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import dotenv from "dotenv";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
function requireEnv(key: string): string {
|
||||||
|
const value = process.env[key];
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
throw new Error(`Environment variable ${key} is required but not set.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const env = {
|
||||||
|
nodeEnv: process.env.NODE_ENV ?? "development",
|
||||||
|
port: Number(process.env.PORT) ?? 3000,
|
||||||
|
|
||||||
|
mongoUri: requireEnv("MONGO_URI"),
|
||||||
|
|
||||||
|
jwtSecret: {
|
||||||
|
secret: requireEnv("JWT_SECRET"),
|
||||||
|
expiresIn: process.env.JWT_EXPIRES_IN ?? "1h",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user