Skip to content

Commit

Permalink
Add .env file to server to avoid hardcoding values
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicDX342 committed Apr 14, 2024
1 parent 5a0cca9 commit b3c5064
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
4 changes: 4 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.1",
"js-yaml": "^4.1.0",
"mysql2": "^3.9.2",
Expand Down
8 changes: 8 additions & 0 deletions server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 28 additions & 18 deletions server/src/models/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
require('dotenv').config();
const Sequelize = require('sequelize');
const UserModel = require('./user');

const sequelize = new Sequelize('cs304_project', 'root', '123qweasd', {
host: 'localhost',
dialect: 'mysql',
logging: (msg) => {
if (msg.startsWith('Executing')) {
console.log(msg);
}
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
host: process.env.DB_HOST,
dialect: 'mysql',
logging: (msg) => {
if (msg.startsWith('Executing')) {
console.log(msg);
}
},
},
});
sequelize
.authenticate()
.then(() =>
console.log('Connection to database has been established successfully.'),
)
.catch((err) => console.error('Unable to connect to the database:', err));
const User = UserModel(sequelize, Sequelize);
);

const initializeDatabase = async () => {
try {
await sequelize.authenticate();
console.log('Connection to database has been established successfully.');
await sequelize.sync({force: false});
console.log(`Database & tables created!`);
} catch (err) {
console.error('Unable to connect to the database:', err);
}
};

sequelize.sync({force: false}).then(() => {
console.log(`Database & tables created!`);
});
initializeDatabase();

const User = UserModel(sequelize, Sequelize);

module.exports = {
User,
Expand Down
1 change: 0 additions & 1 deletion server/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ const router = express.Router();
router.use('/users', require('./users'));

module.exports = router;
//

0 comments on commit b3c5064

Please sign in to comment.