diff --git a/README.md b/README.md index 5505c7cf..15218a37 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ MONGO_URI="mongodb://database/accord" NODE_ENV="dev" PORT=3000 WEBSITE_URL="http://localhost:4200" +SESSION_SECRET="Please ⭐ this repository." ``` `backend/test/.env` @@ -87,4 +88,12 @@ NODE_ENV="dev" PORT=3001 ROOT_ENDPOINT="http://localhost:3001" WEBSITE_URL="http://localhost:4200" +SESSION_SECRET="Please ⭐ this repository." ``` + +## Troubleshooting + +### App does not connect to MongoDB on Windows? +- Ensure MongoDB is installed. +- If localhost does not work, use `mongodb://127.0.0.1:27017/accord`. + - https://stackoverflow.com/a/73139137/8304458 \ No newline at end of file diff --git a/backend/.env.example b/backend/.env.example index d14819da..2876ea93 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -3,4 +3,5 @@ EMAIL_PASSWORD="password" MONGO_URI="mongodb://localhost/accord" NODE_ENV="dev" PORT=3000 -WEBSITE_URL="http://localhost:4200" \ No newline at end of file +WEBSITE_URL="http://localhost:4200" +SESSION_SECRET="Please ⭐ this repository." \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index 144ed315..a06c490c 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -1,12 +1,12 @@ { "name": "@acrd/backend", - "version": "0.0.0", + "version": "0.6.0-pre-release", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@acrd/backend", - "version": "0.0.0", + "version": "0.6.0-pre-release", "dependencies": { "@acrd/ion": "github:acrdapp/ion", "@acrd/types": "file:../frontend/src/types", @@ -42,7 +42,8 @@ "socket.io": "^4.0.0", "striptags": "^3.2.0", "typescript": "^4.2.3", - "winston": "^3.3.3" + "winston": "^3.3.3", + "yaml": "^2.3.1" }, "devDependencies": { "@types/bad-words": "^3.0.1", @@ -84,6 +85,7 @@ } }, "../frontend/src/types": { + "name": "@acrd/types", "hasInstallScript": true, "dependencies": { "typescript": "^4.9.4" @@ -7585,6 +7587,14 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, + "node_modules/yaml": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.1.tgz", + "integrity": "sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==", + "engines": { + "node": ">= 14" + } + }, "node_modules/yargs": { "version": "16.2.0", "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", diff --git a/backend/package.json b/backend/package.json index 58e64501..6039b26a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -48,7 +48,8 @@ "socket.io": "^4.0.0", "striptags": "^3.2.0", "typescript": "^4.2.3", - "winston": "^3.3.3" + "winston": "^3.3.3", + "yaml": "^2.3.1" }, "devDependencies": { "@types/bad-words": "^3.0.1", @@ -88,4 +89,4 @@ "ts-node": "^10.4.0", "ts-node-dev": "^1.1.8" } -} \ No newline at end of file +} diff --git a/backend/src/app.ts b/backend/src/app.ts index b705d2b0..8d4ae17b 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -2,6 +2,10 @@ import { connect } from 'mongoose'; import { config } from 'dotenv'; config(); +import { parse } from 'yaml'; +import fs from 'fs'; +global['config'] = parse(fs.readFileSync('../config.yaml', 'utf-8')); + import './modules/deps'; import './modules/logger'; import { User } from './data/models/user'; @@ -13,7 +17,7 @@ connect(process.env.MONGO_URI, { useCreateIndex: true, serverSelectionTimeoutMS: 0, }).catch(error => log.error(error.message || 'Unable to connect to db', { uri: process.env.MONGO_URI })) - .then(async (con) => { + .then(async () => { log.info(`Connected to database.`, { uri: process.env.MONGO_URI }); await User.updateMany({ $set: { status: 'OFFLINE' } }) }); \ No newline at end of file diff --git a/backend/src/email/email.ts b/backend/src/email/email.ts index 0e01747d..bf13de9b 100644 --- a/backend/src/email/email.ts +++ b/backend/src/email/email.ts @@ -18,7 +18,7 @@ export class Email { }); this.email.verify((error) => (error) - ? log.error(error) + ? log.error(error?.message || 'Failed to login to email service') : log.info('Logged in to email service')); this.email.use('compile', pugEngine({ diff --git a/backend/src/modules/logger.ts b/backend/src/modules/logger.ts index 045c5efb..c9180e93 100644 --- a/backend/src/modules/logger.ts +++ b/backend/src/modules/logger.ts @@ -7,12 +7,12 @@ const colorsAndTime = format.combine( ); addColors({ - info: 'bold blue', - warn: 'bold yellow', - error: 'bold red', - debug: 'bold green', - verbose: 'bold grey', - silly: 'bold magenta', + info: global.config.logger.info, + warn: global.config.logger.warn, + error: global.config.logger.error, + debug: global.config.logger.debug, + verbose: global.config.logger.verbose, + silly: global.config.logger.silly, }); const logger = createLogger({ diff --git a/backend/src/rest/functions/apply-middleware.ts b/backend/src/rest/functions/apply-middleware.ts index 1281f089..3fbfb935 100644 --- a/backend/src/rest/functions/apply-middleware.ts +++ b/backend/src/rest/functions/apply-middleware.ts @@ -77,7 +77,7 @@ export default (app: Application) => { app.use(bodyParser.json()); app.use(passport.initialize()); app.use(expressSession({ - secret: 'keyboard cat', + secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: true, cookie: { secure: true } diff --git a/backend/test/.env.example b/backend/test/.env.example new file mode 100644 index 00000000..59a397e3 --- /dev/null +++ b/backend/test/.env.example @@ -0,0 +1,9 @@ +API_URL="http://localhost:3001/api" +EMAIL_ADDRESS="...@gmail.com" +EMAIL_PASSWORD="..." +MONGO_URI="mongodb://localhost/accord-test" +NODE_ENV="dev" +PORT=3001 +ROOT_ENDPOINT="http://localhost:3001" +WEBSITE_URL="http://localhost:4200" +SESSION_SECRET="Please ⭐ this repository." \ No newline at end of file diff --git a/backend/types/dotenv.d.ts b/backend/types/dotenv.d.ts index ec87816a..2f855e53 100644 --- a/backend/types/dotenv.d.ts +++ b/backend/types/dotenv.d.ts @@ -8,7 +8,8 @@ declare global { PORT: string; ROOT_ENDPOINT: string; WEBSITE_URL: string; + SESSION_SECRET: string; } } } -export {}; \ No newline at end of file +export { }; \ No newline at end of file diff --git a/backend/types/global.d.ts b/backend/types/global.d.ts index 223a034a..0be7c3c1 100644 --- a/backend/types/global.d.ts +++ b/backend/types/global.d.ts @@ -1,5 +1,17 @@ declare global { const log: import('winston').Logger; const deps: import('../src/modules/deps').Deps; + const config: ConfigYAML; + + interface ConfigYAML { + logger: { + info: string, + warn: string, + error: string, + debug: string, + verbose: string, + silly: string, + } + } } -export {} \ No newline at end of file +export { } \ No newline at end of file diff --git a/config.yaml b/config.yaml new file mode 100644 index 00000000..3319a1c5 --- /dev/null +++ b/config.yaml @@ -0,0 +1,9 @@ +# This config is public. Use .env for private and secure values. +# See https://www.npmjs.com/package/winston for custom console log formatting. +logger: + info: 'bold blue' + warn: 'bold yellow' + error: 'bold red' + debug: 'bold green' + verbose: 'bold grey' + silly: 'bold magenta' \ No newline at end of file