mongo docker script

This commit is contained in:
Primakov Alexandr Alexandrovich 2024-12-04 19:25:53 +03:00
parent 4c9afb83b5
commit ed9eb93013
7 changed files with 2933 additions and 35 deletions

View File

@ -1 +1,2 @@
PORT=3003 PORT=3003
JWT_SECRET="superSecretString"

10
d-script/up-mongo.sh Normal file
View File

@ -0,0 +1,10 @@
docker \
run \
-d \
--rm \
--name bh-mongo \
-e MONGO_INITDB_ROOT_USERNAME=qqq \
-e MONGO_INITDB_ROOT_PASSWORD=qqq \
-p 27017:27017 \
-v bh-mongo-volume:/data/db \
mongo:8.0.3;

2826
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,10 @@
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "^5.0.1", "express": "^5.0.1",
"express-json-validator-middleware": "^3.0.1", "express-json-validator-middleware": "^3.0.1",
"install": "^0.13.0",
"jsonwebtoken": "^9.0.2",
"npm": "^10.9.1",
"pbkdf2-password": "^1.2.1",
"ts-node": "^10.9.2" "ts-node": "^10.9.2"
}, },
"devDependencies": { "devDependencies": {

1
src/model/errors.ts Normal file
View File

@ -0,0 +1 @@
export class NotPayedError extends Error {}

View File

@ -1,40 +1,111 @@
import { Router } from "express"; import { Router } from "express";
import { Validator } from "express-json-validator-middleware"; import { Validator } from "express-json-validator-middleware";
import bkfd2Password from "pbkdf2-password";
import { promisify } from 'node:util'
import jwt from 'jsonwebtoken'
import { NotPayedError } from "../../model/errors";
const hasher = bkfd2Password();
// const asyncHasher = promisify(hasher);
export const router = Router(); export const router = Router();
const { validate } = new Validator({ }); const { validate } = new Validator({});
const user = { const user = {
type: "object", type: "object",
required: ["name"], required: ["name"],
properties: { properties: {
name: { name: {
type: "string" type: "string",
}
}, },
}; },
};
let users = [ let users = [
{ id: 1, name: 'John' }, { id: 1, login: "John", password: '123', salt: 'salt' },
{ id: 2, name: 'Jane' },
]; ];
const addUser = (user) => new Promise<void>((res, rej) => { const addUser = (user) =>
new Promise<void>((res, rej) => {
setTimeout(() => { setTimeout(() => {
// rej(new Error('user is not a spoon')); // rej(new Error('user is not a spoon'));
users.push(user); users.push(user);
res(); res();
}, 100) }, 100);
}) });
router.get('/', (req, res) => void res.send(users)); router.get("/", (req, res) => void res.send(users));
router.post('/', async (req, res) => { router.post("/", async (req, res) => {
await addUser(req.body); await addUser(req.body);
res.send(users); res.send(users);
});
router.delete("/:id", (req, res, next) => {
users = users.filter((user) => user.id !== Number(req.params.id));
res.send(users);
});
global.id = 1;
router.post('/sign-in', (req, res, next) => {
const { login, password } = req.body;
const user = users.find(u => u.login === login);
if (!user) {
res.status(404).send({ message: 'User not found' })
return
}
hasher({ password, salt: user.salt }, function(err, pass, salt, hash) {
if (err) {
next(err);
return
}
if (hash !== user.password) {
res.status(401).send({ message: 'Wrong password' })
return
}
const token = jwt.sign({ ...user, role: 'admin', iat: Math.floor(Date.now() / 1000) + 30000 }, process.env.JWT_SECRET)
res.send({ login, token })
})
}) })
router.delete('/:id', validate(''), (req, res, next) => { router.get('/list', (req, res, next) => {
users = users.filter(user => user.id !== Number(req.params.id)); const token = req.headers['authorization']?.replace('Bearer ', '');
res.send(users);
const data = jwt.verify(token, process.env.JWT_SECRET)
if (data.role !== 'admin') {
throw new Error('Sorry Not 4 you')
}
next()
}, (req, res, next) => {
res.send(users)
})
router.post('/sign-up', (req, res, next) => {
const { login, password } = req.body;
hasher({ password }, function(err, pass, salt, hash) {
if (err) {
next(err);
return;
}
users.push({
id: global.id++,
login,
password: hash,
salt
})
res.send({ login, users })
})
}) })

View File

@ -1,3 +1,10 @@
export const handleError = (error, req, res, next) => { import { NotPayedError } from "../model/errors"
res.send({ error: error.message })
export const handleError = (error, req, res, _next) => {
if (error instanceof NotPayedError) {
res.status(402).send({ error: 'Надо больше золота' })
return
}
res.status(500).send({ error: error.message })
} }