mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 10:54:07 +00:00
env structure, overall formatting check, sequelize config, minor util checks, minor validator aggregation change
This commit is contained in:
parent
1abcb3ac4e
commit
e2c544aee2
1
.env
1
.env
@ -2,3 +2,4 @@ CLIENT_ID = your_client_id_here
|
|||||||
CLIENT_SECRET = your_client_secret_here
|
CLIENT_SECRET = your_client_secret_here
|
||||||
SESSION_SECRET = 'your_session_secret_string_here'
|
SESSION_SECRET = 'your_session_secret_string_here'
|
||||||
PORT = 9001
|
PORT = 9001
|
||||||
|
TRUST_PROXY = 1
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
REDIRECT_URI = http://localhost:9001/api/auth/callback
|
REDIRECT_URI = http://localhost:9001/api/auth/callback
|
||||||
TRUST_PROXY = 1
|
DB_USER = your_database_username
|
||||||
PG_USER = your_postgres_username
|
DB_PASSWD = your_database_password
|
||||||
PG_PASSWD = your_postgres_password
|
DB_NAME = your_database_name
|
||||||
PG_DATABASE = postgres_database_name
|
DB_HOST = localhost
|
||||||
PG_HOST = localhost
|
DB_PORT = your_database_port
|
||||||
PG_PORT = postgres_instance_port
|
|
||||||
|
|||||||
@ -1,2 +1 @@
|
|||||||
REDIRECT_URI = https://domain.for.this.app/api/auth/callback
|
REDIRECT_URI = https://domain.for.this.app/api/auth/callback
|
||||||
TRUST_PROXY=1
|
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@ -70,9 +70,7 @@ typings/
|
|||||||
|
|
||||||
# dotenv environment variables file
|
# dotenv environment variables file
|
||||||
.env.local
|
.env.local
|
||||||
.env.development.local
|
.env.*.local
|
||||||
.env.production.local
|
|
||||||
.env.test.local
|
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
.cache
|
.cache
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
|
|
||||||
const validator = require("../validators");
|
const { validate } = require("../validators");
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
|
|
||||||
|
|||||||
@ -1,33 +1,28 @@
|
|||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
let connConfigs = {
|
const connConfigs = {
|
||||||
development: {
|
development: {
|
||||||
username: process.env.PG_USER,
|
username: process.env.DB_USER || 'postgres',
|
||||||
password: process.env.PG_PASSWD,
|
password: process.env.DB_PASSWD || '',
|
||||||
database: process.env.PG_DATABASE,
|
database: process.env.DB_NAME || 'postgres',
|
||||||
host: process.env.PG_HOST,
|
host: process.env.DB_HOST || '127.0.0.1',
|
||||||
port: process.env.PG_PORT,
|
port: process.env.DB_PORT || 5432,
|
||||||
},
|
},
|
||||||
test: {
|
staging: {
|
||||||
username: process.env.PG_USER,
|
use_env_variable: "DB_URL", // use connection string for non-dev env
|
||||||
password: process.env.PG_PASSWD,
|
|
||||||
database: process.env.PG_DATABASE,
|
|
||||||
host: process.env.PG_HOST,
|
|
||||||
port: process.env.PG_PORT,
|
|
||||||
},
|
},
|
||||||
production: {
|
production: {
|
||||||
username: process.env.PG_USER,
|
use_env_variable: "DB_URL", // use connection string for non-dev env
|
||||||
password: process.env.PG_PASSWD,
|
// dialectOptions: {
|
||||||
database: process.env.PG_DATABASE,
|
// ssl: true,
|
||||||
host: process.env.PG_HOST,
|
// },
|
||||||
port: process.env.PG_PORT,
|
}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// common config
|
// common config
|
||||||
for (const conf in connConfigs) {
|
for (const conf in connConfigs) {
|
||||||
connConfigs[conf]['logging'] = (msg) => logger.debug(msg);
|
connConfigs[conf]['logging'] = (msg) => logger.debug(msg);
|
||||||
connConfigs[conf]['dialect'] = 'postgres';
|
connConfigs[conf]['dialect'] = process.env.DB_DIALECT || 'postgres';
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = connConfigs;
|
module.exports = connConfigs;
|
||||||
@ -22,4 +22,4 @@ module.exports = {
|
|||||||
sessionName,
|
sessionName,
|
||||||
stateKey,
|
stateKey,
|
||||||
scopes
|
scopes
|
||||||
}
|
};
|
||||||
|
|||||||
19
index.js
19
index.js
@ -7,10 +7,11 @@ const session = require("express-session");
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const cookieParser = require('cookie-parser');
|
const cookieParser = require('cookie-parser');
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
|
|
||||||
const SQLiteStore = require("connect-sqlite3")(session);
|
const SQLiteStore = require("connect-sqlite3")(session);
|
||||||
const db = require("./models");
|
|
||||||
const { sessionName } = require('./constants');
|
const { sessionName } = require('./constants');
|
||||||
|
const db = require("./models");
|
||||||
|
|
||||||
const { isAuthenticated } = require('./middleware/authCheck');
|
const { isAuthenticated } = require('./middleware/authCheck');
|
||||||
|
|
||||||
const logger = require("./utils/logger")(module);
|
const logger = require("./utils/logger")(module);
|
||||||
@ -40,12 +41,10 @@ app.use(session({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(cookieParser());
|
|
||||||
|
|
||||||
// Configure helmet
|
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
app.disable('x-powered-by')
|
app.disable('x-powered-by');
|
||||||
|
|
||||||
|
app.use(cookieParser());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
||||||
@ -58,13 +57,15 @@ app.use("/api/playlists", isAuthenticated, require("./routes/playlists"));
|
|||||||
app.use("/api/operations", isAuthenticated, require("./routes/operations"));
|
app.use("/api/operations", isAuthenticated, require("./routes/operations"));
|
||||||
|
|
||||||
// Fallbacks
|
// Fallbacks
|
||||||
app.use((_req, res) => {
|
app.use((req, res) => {
|
||||||
return res.status(404).send(
|
res.status(404).send(
|
||||||
"Guess the <a href=\"https://github.com/20kaushik02/spotify-manager\">cat's</a> out of the bag!"
|
"Guess the <a href=\"https://github.com/20kaushik02/spotify-manager\">cat's</a> out of the bag!"
|
||||||
);
|
);
|
||||||
|
logger.info("Unrecognized URL", { url: req.url });
|
||||||
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 5000;
|
||||||
|
|
||||||
const server = app.listen(port, () => {
|
const server = app.listen(port, () => {
|
||||||
logger.info(`App Listening on port ${port}`);
|
logger.info(`App Listening on port ${port}`);
|
||||||
|
|||||||
@ -2,14 +2,12 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const Sequelize = require("sequelize");
|
const Sequelize = require("sequelize");
|
||||||
const typedefs = require("../typedefs");
|
|
||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
const env = process.env.NODE_ENV || "development";
|
const env = process.env.NODE_ENV || "development";
|
||||||
const config = require(__dirname + "/../config/sequelize.js")[env];
|
const config = require(__dirname + "/../config/sequelize.js")[env];
|
||||||
const db = {};
|
const db = {};
|
||||||
|
|
||||||
/** @type {typedefs.Sequelize} */
|
|
||||||
let sequelize;
|
let sequelize;
|
||||||
if (config.use_env_variable) {
|
if (config.use_env_variable) {
|
||||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"start": "node index.js",
|
||||||
"dev": "cross-env NODE_ENV=development nodemon index.js"
|
"dev": "cross-env NODE_ENV=development nodemon --exitcrash index.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@ -5,12 +5,6 @@
|
|||||||
* @typedef {import('express').Response} Res
|
* @typedef {import('express').Response} Res
|
||||||
* @typedef {import('express').NextFunction} Next
|
* @typedef {import('express').NextFunction} Next
|
||||||
*
|
*
|
||||||
* @typedef {import("sequelize").Sequelize} Sequelize
|
|
||||||
* @typedef {import("sequelize").Model} Model
|
|
||||||
* @typedef {import("sequelize").QueryInterface} QueryInterface
|
|
||||||
*
|
|
||||||
* @typedef {import('winston').Logger} Logger
|
|
||||||
*
|
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* type: string,
|
* type: string,
|
||||||
* is_local: boolean,
|
* is_local: boolean,
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Returns a single string of the values of all keys in the given JSON object, even nested ones.
|
* Stringifies only values of a JSON object, including nested ones
|
||||||
*
|
*
|
||||||
* @param {*} obj
|
* @param {any} obj JSON object
|
||||||
|
* @param {string} delimiter Delimiter of final string
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
const getNestedValuesString = (obj) => {
|
const getNestedValuesString = (obj, delimiter = ', ') => {
|
||||||
let values = [];
|
let values = [];
|
||||||
for (key in obj) {
|
for (key in obj) {
|
||||||
if (typeof obj[key] !== "object") {
|
if (typeof obj[key] !== "object") {
|
||||||
@ -14,7 +15,7 @@ const getNestedValuesString = (obj) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return values.join();
|
return values.join(delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const { createLogger, transports, config, format } = require('winston');
|
const { createLogger, transports, config, format } = require('winston');
|
||||||
const { colorize, combine, label, timestamp, printf, errors } = format;
|
const { combine, label, timestamp, printf, errors } = format;
|
||||||
|
|
||||||
const typedefs = require("../typedefs");
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
@ -36,10 +36,9 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
|||||||
/**
|
/**
|
||||||
* Creates a curried function, and call it with the module in use to get logs with filename
|
* Creates a curried function, and call it with the module in use to get logs with filename
|
||||||
* @param {typedefs.Module} callingModule The module from which the logger is called
|
* @param {typedefs.Module} callingModule The module from which the logger is called
|
||||||
* @returns {typedefs.Logger}
|
|
||||||
*/
|
*/
|
||||||
const logger = (callingModule) => {
|
const curriedLogger = (callingModule) => {
|
||||||
let tmpLogger = createLogger({
|
let winstonLogger = createLogger({
|
||||||
levels: config.npm.levels,
|
levels: config.npm.levels,
|
||||||
format: combine(
|
format: combine(
|
||||||
errors({ stack: true }),
|
errors({ stack: true }),
|
||||||
@ -48,7 +47,7 @@ const logger = (callingModule) => {
|
|||||||
logFormat,
|
logFormat,
|
||||||
),
|
),
|
||||||
transports: [
|
transports: [
|
||||||
new transports.Console({ level: 'debug' }),
|
new transports.Console({ level: 'info' }),
|
||||||
new transports.File({
|
new transports.File({
|
||||||
filename: __dirname + '/../logs/debug.log',
|
filename: __dirname + '/../logs/debug.log',
|
||||||
level: 'debug',
|
level: 'debug',
|
||||||
@ -57,12 +56,12 @@ const logger = (callingModule) => {
|
|||||||
new transports.File({
|
new transports.File({
|
||||||
filename: __dirname + '/../logs/error.log',
|
filename: __dirname + '/../logs/error.log',
|
||||||
level: 'error',
|
level: 'error',
|
||||||
maxsize: 10485760,
|
maxsize: 1048576,
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
tmpLogger.on('error', (error) => tmpLogger.crit("Error inside logger", { error }));
|
winstonLogger.on('error', (error) => winstonLogger.error("Error inside logger", { error }));
|
||||||
return tmpLogger;
|
return winstonLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = logger;
|
module.exports = curriedLogger;
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
const { validationResult } = require("express-validator");
|
const { validationResult } = require("express-validator");
|
||||||
|
|
||||||
const typedefs = require("../typedefs");
|
|
||||||
const { getNestedValuesString } = require("../utils/jsonTransformer");
|
const { getNestedValuesString } = require("../utils/jsonTransformer");
|
||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator
|
* Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator
|
||||||
*
|
*
|
||||||
@ -16,10 +17,21 @@ const validate = (req, res, next) => {
|
|||||||
if (errors.isEmpty()) {
|
if (errors.isEmpty()) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
const extractedErrors = []
|
|
||||||
errors.array().map(err => extractedErrors.push({
|
const extractedErrors = [];
|
||||||
|
errors.array().forEach(err => {
|
||||||
|
if (err.type === 'alternative') {
|
||||||
|
err.nestedErrors.forEach(nestedErr => {
|
||||||
|
extractedErrors.push({
|
||||||
|
[nestedErr.path]: nestedErr.msg
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else if (err.type === 'field') {
|
||||||
|
extractedErrors.push({
|
||||||
[err.path]: err.msg
|
[err.path]: err.msg
|
||||||
}));
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: getNestedValuesString(extractedErrors),
|
message: getNestedValuesString(extractedErrors),
|
||||||
@ -31,4 +43,4 @@ const validate = (req, res, next) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validate
|
validate
|
||||||
}
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user