database
This commit is contained in:
parent
a93f949020
commit
13ddf46255
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
auth-service/node_modules
|
||||
mail-service/node_modules
|
||||
1175
auth-service/package-lock.json
generated
Normal file
1175
auth-service/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
auth-service/package.json
Normal file
14
auth-service/package.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"scripts": {
|
||||
"start": "node src/index.js"
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"bcrypt": "^6.0.0",
|
||||
"dotenv": "^17.2.0",
|
||||
"express": "^5.1.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mysql2": "^3.14.2",
|
||||
"sequelize": "^6.37.7"
|
||||
}
|
||||
}
|
||||
27
auth-service/src/index.js
Normal file
27
auth-service/src/index.js
Normal file
@ -0,0 +1,27 @@
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
import express from 'express';
|
||||
import { sequelize } from './models/index.js';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Auth Service is running');
|
||||
});
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
await sequelize.sync({ alter: true });
|
||||
console.log('Database connected and models synced');
|
||||
} catch (err) {
|
||||
console.error('Unable to connect to the database:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Auth Service listening on port ${PORT}`);
|
||||
});
|
||||
30
auth-service/src/models/index.js
Normal file
30
auth-service/src/models/index.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { Sequelize } from 'sequelize';
|
||||
import UserModel from './user.js';
|
||||
import RoleModel from './role.js';
|
||||
import PermissionModel from './permission.js';
|
||||
import RolePermissionModel from './rolePermission.js';
|
||||
|
||||
const sequelize = new Sequelize(
|
||||
process.env.DB_NAME,
|
||||
process.env.DB_USER,
|
||||
process.env.DB_PASSWORD,
|
||||
{
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
dialect: 'mysql',
|
||||
logging: false,
|
||||
}
|
||||
);
|
||||
|
||||
const Role = RoleModel(sequelize);
|
||||
const Permission = PermissionModel(sequelize);
|
||||
const RolePermission = RolePermissionModel(sequelize);
|
||||
const User = UserModel(sequelize);
|
||||
|
||||
Role.hasMany(User, { foreignKey: 'role_id' });
|
||||
User.belongsTo(Role, { foreignKey: 'role_id' });
|
||||
|
||||
Role.belongsToMany(Permission, { through: RolePermission, foreignKey: 'role_id', otherKey: 'permission_id' });
|
||||
Permission.belongsToMany(Role, { through: RolePermission, foreignKey: 'permission_id', otherKey: 'role_id' });
|
||||
|
||||
export { sequelize, User, Role, Permission, RolePermission };
|
||||
10
auth-service/src/models/permission.js
Normal file
10
auth-service/src/models/permission.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
|
||||
export default (sequelize) => {
|
||||
const Permission = sequelize.define('Permission', {
|
||||
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
|
||||
code: { type: DataTypes.STRING, allowNull: false, unique: true },
|
||||
description: { type: DataTypes.STRING },
|
||||
}, { tableName: 'permissions', timestamps: false });
|
||||
return Permission;
|
||||
};
|
||||
10
auth-service/src/models/role.js
Normal file
10
auth-service/src/models/role.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
|
||||
export default (sequelize) => {
|
||||
const Role = sequelize.define('Role', {
|
||||
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
|
||||
name: { type: DataTypes.STRING, allowNull: false, unique: true },
|
||||
description: { type: DataTypes.STRING },
|
||||
}, { tableName: 'roles', timestamps: false });
|
||||
return Role;
|
||||
};
|
||||
10
auth-service/src/models/rolePermission.js
Normal file
10
auth-service/src/models/rolePermission.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
|
||||
export default (sequelize) => {
|
||||
const RolePermission = sequelize.define('RolePermission', {
|
||||
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
|
||||
role_id: { type: DataTypes.INTEGER, allowNull: false },
|
||||
permission_id: { type: DataTypes.INTEGER, allowNull: false },
|
||||
}, { tableName: 'role_permissions', timestamps: false });
|
||||
return RolePermission;
|
||||
};
|
||||
14
auth-service/src/models/user.js
Normal file
14
auth-service/src/models/user.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { DataTypes, Sequelize } from 'sequelize';
|
||||
|
||||
export default (sequelize) => {
|
||||
const User = sequelize.define('User', {
|
||||
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
|
||||
email: { type: DataTypes.STRING, allowNull: false, unique: true },
|
||||
name: { type: DataTypes.STRING, allowNull: false },
|
||||
password_hash: { type: DataTypes.STRING, allowNull: false },
|
||||
role_id: { type: DataTypes.INTEGER, allowNull: false },
|
||||
created_at: { type: DataTypes.DATE, defaultValue: Sequelize.NOW },
|
||||
last_login: { type: DataTypes.DATE },
|
||||
}, { tableName: 'users', timestamps: false });
|
||||
return User;
|
||||
};
|
||||
61
docker-compose.yml
Normal file
61
docker-compose.yml
Normal file
@ -0,0 +1,61 @@
|
||||
version: '3.8'
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:8.0
|
||||
container_name: mysql-auth
|
||||
restart: always
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: rootpassword
|
||||
MYSQL_DATABASE: authdb
|
||||
MYSQL_USER: authuser
|
||||
MYSQL_PASSWORD: authpassword
|
||||
ports:
|
||||
- '3310:3306'
|
||||
volumes:
|
||||
- mysql_data:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
auth-service:
|
||||
image: node:20
|
||||
container_name: auth-service
|
||||
restart: always
|
||||
environment:
|
||||
DB_HOST: mysql
|
||||
DB_PORT: 3306
|
||||
DB_NAME: authdb
|
||||
DB_USER: authuser
|
||||
DB_PASSWORD: authpassword
|
||||
JWT_SECRET: supersecretkey
|
||||
volumes:
|
||||
- ./auth-service:/usr/src/app
|
||||
working_dir: /usr/src/app
|
||||
command: bash -c "npm install && npm start"
|
||||
depends_on:
|
||||
mysql:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- '3001:3000'
|
||||
|
||||
mail-service:
|
||||
image: node:20
|
||||
container_name: mail-service
|
||||
restart: always
|
||||
volumes:
|
||||
- ./mail-service:/usr/src/app
|
||||
working_dir: /usr/src/app
|
||||
command: bash -c "npm install && npm start"
|
||||
environment:
|
||||
DB_HOST: mysql
|
||||
DB_PORT: 3306
|
||||
DB_NAME: authdb
|
||||
DB_USER: authuser
|
||||
DB_PASSWORD: authpassword
|
||||
ports:
|
||||
- '3002:3000'
|
||||
|
||||
volumes:
|
||||
mysql_data:
|
||||
Loading…
Reference in New Issue
Block a user