Spaces:
Sleeping
Sleeping
Commit
·
74e470a
1
Parent(s):
7933ed3
refactor: base and config
Browse files- package-lock.json +0 -51
- package.json +0 -1
- src/configs/config.ts +17 -0
- src/configs/database.ts +11 -18
- src/configs/env.ts +25 -0
- src/index.ts +10 -9
package-lock.json
CHANGED
@@ -10,7 +10,6 @@
|
|
10 |
"license": "ISC",
|
11 |
"dependencies": {
|
12 |
"bcrypt": "^5.1.1",
|
13 |
-
"body-parser": "^1.20.2",
|
14 |
"cors": "^2.8.5",
|
15 |
"dotenv": "^16.3.1",
|
16 |
"express": "^4.18.2",
|
@@ -395,42 +394,6 @@
|
|
395 |
"node": ">=8"
|
396 |
}
|
397 |
},
|
398 |
-
"node_modules/body-parser": {
|
399 |
-
"version": "1.20.2",
|
400 |
-
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
|
401 |
-
"integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==",
|
402 |
-
"dependencies": {
|
403 |
-
"bytes": "3.1.2",
|
404 |
-
"content-type": "~1.0.5",
|
405 |
-
"debug": "2.6.9",
|
406 |
-
"depd": "2.0.0",
|
407 |
-
"destroy": "1.2.0",
|
408 |
-
"http-errors": "2.0.0",
|
409 |
-
"iconv-lite": "0.4.24",
|
410 |
-
"on-finished": "2.4.1",
|
411 |
-
"qs": "6.11.0",
|
412 |
-
"raw-body": "2.5.2",
|
413 |
-
"type-is": "~1.6.18",
|
414 |
-
"unpipe": "1.0.0"
|
415 |
-
},
|
416 |
-
"engines": {
|
417 |
-
"node": ">= 0.8",
|
418 |
-
"npm": "1.2.8000 || >= 1.4.16"
|
419 |
-
}
|
420 |
-
},
|
421 |
-
"node_modules/body-parser/node_modules/debug": {
|
422 |
-
"version": "2.6.9",
|
423 |
-
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
424 |
-
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
|
425 |
-
"dependencies": {
|
426 |
-
"ms": "2.0.0"
|
427 |
-
}
|
428 |
-
},
|
429 |
-
"node_modules/body-parser/node_modules/ms": {
|
430 |
-
"version": "2.0.0",
|
431 |
-
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
432 |
-
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
|
433 |
-
},
|
434 |
"node_modules/brace-expansion": {
|
435 |
"version": "1.1.11",
|
436 |
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
@@ -1753,20 +1716,6 @@
|
|
1753 |
"node": ">= 0.6"
|
1754 |
}
|
1755 |
},
|
1756 |
-
"node_modules/raw-body": {
|
1757 |
-
"version": "2.5.2",
|
1758 |
-
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz",
|
1759 |
-
"integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==",
|
1760 |
-
"dependencies": {
|
1761 |
-
"bytes": "3.1.2",
|
1762 |
-
"http-errors": "2.0.0",
|
1763 |
-
"iconv-lite": "0.4.24",
|
1764 |
-
"unpipe": "1.0.0"
|
1765 |
-
},
|
1766 |
-
"engines": {
|
1767 |
-
"node": ">= 0.8"
|
1768 |
-
}
|
1769 |
-
},
|
1770 |
"node_modules/readable-stream": {
|
1771 |
"version": "3.6.2",
|
1772 |
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
|
|
|
10 |
"license": "ISC",
|
11 |
"dependencies": {
|
12 |
"bcrypt": "^5.1.1",
|
|
|
13 |
"cors": "^2.8.5",
|
14 |
"dotenv": "^16.3.1",
|
15 |
"express": "^4.18.2",
|
|
|
394 |
"node": ">=8"
|
395 |
}
|
396 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
"node_modules/brace-expansion": {
|
398 |
"version": "1.1.11",
|
399 |
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
|
|
1716 |
"node": ">= 0.6"
|
1717 |
}
|
1718 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1719 |
"node_modules/readable-stream": {
|
1720 |
"version": "3.6.2",
|
1721 |
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
|
package.json
CHANGED
@@ -19,7 +19,6 @@
|
|
19 |
},
|
20 |
"dependencies": {
|
21 |
"bcrypt": "^5.1.1",
|
22 |
-
"body-parser": "^1.20.2",
|
23 |
"cors": "^2.8.5",
|
24 |
"dotenv": "^16.3.1",
|
25 |
"express": "^4.18.2",
|
|
|
19 |
},
|
20 |
"dependencies": {
|
21 |
"bcrypt": "^5.1.1",
|
|
|
22 |
"cors": "^2.8.5",
|
23 |
"dotenv": "^16.3.1",
|
24 |
"express": "^4.18.2",
|
src/configs/config.ts
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Env } from "./env";
|
2 |
+
import dotenv from "dotenv";
|
3 |
+
dotenv.config();
|
4 |
+
|
5 |
+
export interface Config {
|
6 |
+
port: number;
|
7 |
+
db: {
|
8 |
+
uri: string;
|
9 |
+
};
|
10 |
+
}
|
11 |
+
|
12 |
+
export const config: Config = {
|
13 |
+
port: Env.get("PORT", 4000).toNumber(),
|
14 |
+
db: {
|
15 |
+
uri: Env.get("DB_URI").toString(),
|
16 |
+
},
|
17 |
+
};
|
src/configs/database.ts
CHANGED
@@ -1,20 +1,13 @@
|
|
1 |
-
import mongoose from
|
|
|
2 |
|
3 |
-
const
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
};
|
7 |
-
|
8 |
-
console.log("CURRENT_ENV", process.env.CURRENT_ENV);
|
9 |
-
const selectedEnv = process.env.CURRENT_ENV || 'development';
|
10 |
-
let uri = uriMap[selectedEnv];
|
11 |
-
console.log(uri);
|
12 |
-
|
13 |
-
export const connection = async () => {
|
14 |
-
return mongoose.connect(uri)
|
15 |
-
.then(() => {
|
16 |
-
console.log(`Connected to MongoDB database successfully on ${selectedEnv} environment!`);
|
17 |
-
}).catch((err) => {
|
18 |
-
console.log("MongoDB Error: ", err);
|
19 |
-
})
|
20 |
-
}
|
|
|
1 |
+
import mongoose from "mongoose";
|
2 |
+
import { config } from "./config";
|
3 |
|
4 |
+
export const connectDatabase = async () => {
|
5 |
+
return mongoose
|
6 |
+
.connect(config.db.uri)
|
7 |
+
.then(() => {
|
8 |
+
console.log(`Connected to MongoDB database successfully!`);
|
9 |
+
})
|
10 |
+
.catch((err) => {
|
11 |
+
console.error("MongoDB Error: ", err);
|
12 |
+
});
|
13 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/configs/env.ts
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export class EnvValue {
|
2 |
+
constructor(public value: string | number | boolean) {}
|
3 |
+
|
4 |
+
toString(): string {
|
5 |
+
return String(this.value);
|
6 |
+
}
|
7 |
+
toNumber(): number {
|
8 |
+
return Number(this.value);
|
9 |
+
}
|
10 |
+
toBoolean(): boolean {
|
11 |
+
return this.value === "true";
|
12 |
+
}
|
13 |
+
}
|
14 |
+
|
15 |
+
export class Env {
|
16 |
+
static get(key: string, defaultValue?: string | number | boolean): EnvValue {
|
17 |
+
const value = process.env[key] || defaultValue;
|
18 |
+
|
19 |
+
if (!value) {
|
20 |
+
throw new Error(`Environment variable ${key} not found`);
|
21 |
+
}
|
22 |
+
|
23 |
+
return new EnvValue(value);
|
24 |
+
}
|
25 |
+
}
|
src/index.ts
CHANGED
@@ -1,26 +1,27 @@
|
|
1 |
-
import dotenv from "dotenv";
|
2 |
-
dotenv.config();
|
3 |
import express from "express";
|
4 |
-
import bodyParser from "body-parser";
|
5 |
import cors from "cors";
|
6 |
import { routes } from "./index.route";
|
7 |
-
import {
|
|
|
8 |
|
9 |
const main = async () => {
|
10 |
// set up database connection
|
11 |
-
|
12 |
|
13 |
// set up express server
|
14 |
const app = express();
|
|
|
|
|
15 |
app.use(cors());
|
|
|
16 |
app.use(express.json());
|
|
|
|
|
17 |
app.use(routes);
|
18 |
-
app.use(bodyParser.json());
|
19 |
-
app.use(bodyParser.urlencoded({ extended: false }));
|
20 |
|
21 |
// start server
|
22 |
-
app.listen(
|
23 |
-
console.log(`Server is up and running on port ${
|
24 |
});
|
25 |
};
|
26 |
|
|
|
|
|
|
|
1 |
import express from "express";
|
|
|
2 |
import cors from "cors";
|
3 |
import { routes } from "./index.route";
|
4 |
+
import { connectDatabase } from "./configs/database";
|
5 |
+
import { config } from "./configs/config";
|
6 |
|
7 |
const main = async () => {
|
8 |
// set up database connection
|
9 |
+
connectDatabase();
|
10 |
|
11 |
// set up express server
|
12 |
const app = express();
|
13 |
+
|
14 |
+
// set up middlewares
|
15 |
app.use(cors());
|
16 |
+
app.use(express.urlencoded({ extended: true }));
|
17 |
app.use(express.json());
|
18 |
+
|
19 |
+
// set up routes
|
20 |
app.use(routes);
|
|
|
|
|
21 |
|
22 |
// start server
|
23 |
+
app.listen(config.port, () => {
|
24 |
+
console.log(`Server is up and running on port ${config.port}!`);
|
25 |
});
|
26 |
};
|
27 |
|