added linter

This commit is contained in:
Strife 2025-05-11 15:25:40 +05:00
parent 1f85ebc3d9
commit cc0d825334
8 changed files with 1560 additions and 149 deletions

14
.eslintrc.json Normal file
View File

@ -0,0 +1,14 @@
{
"extends": "google",
"env": {
"browser": true,
"node": true,
"es2021": true
},
"parserOptions": {
"sourceType": "module" // Это позволяет использовать import/export
},
"rules": {
"linebreak-style": "off"
}
}

20
db.js
View File

@ -2,16 +2,16 @@ import knex from 'knex';
const initializeDb = () => {
const dbConfig = {
client: 'pg',
connection: {
host: '192.168.0.10',
user: 'userName',
password: 'password',
database: process.env.DB_NAME || 'dbName'
},
migrations: {
directory: './migrations'
}
client: 'pg',
connection: {
host: '192.168.0.10',
user: 'userName',
password: 'password',
database: process.env.DB_NAME || 'dbName',
},
migrations: {
directory: './migrations',
},
};
return knex(dbConfig);

View File

@ -1,12 +1,12 @@
import express from 'express';
import cors from "cors";
import cors from 'cors';
import bodyParser from 'body-parser';
import {auth} from "./routes/auth/auth.js";
import {checkSession} from "./routes/auth/checkSession.js"
import {ping} from "./routes/test/ping.js";
import {auth} from './routes/auth/auth.js';
import {checkSession} from './routes/auth/checkSession.js';
import {ping} from './routes/test/ping.js';
const authIgnorePaths = ['/ping']
const authIgnorePaths = ['/ping'];
const app = express();
@ -22,8 +22,8 @@ const checkAuth = async (req, res, next) => {
return res.status(401).json({error: 'Unauthorized'});
}
const session = await checkSession(authToken)
if(session){
const session = await checkSession(authToken);
if (session) {
return next();
}
@ -37,57 +37,57 @@ app.use(checkAuth); // Применяем middleware функцию для вс
// Проверка сессии
app.get(['/ping'], async (req, res) => {
try{
const result = await ping(req)
try {
const result = await ping(req);
res.status(200).json(result);
}catch (e) {
console.log('Ошибка')
res.status(500).json({error: e})
} catch (e) {
console.log('Ошибка');
res.status(500).json({error: e});
}
});
// Авторизация
app.post(['/auth'], async (req, res) => {
try{
const result = await auth(req.body)
try {
const result = await auth(req.body);
if(result === 'Unauthorized'){
if (result === 'Unauthorized') {
res.status(401);
}
res.status(200).json(result);
}catch (e) {
console.log('Ошибка')
res.status(500).json({error: e})
} catch (e) {
console.log('Ошибка');
res.status(500).json({error: e});
}
});
// Проверка сессии
app.get(['/checkSession'], async (req, res) => {
try{
const authToken = req.headers.authorization ?? null
try {
const authToken = req.headers.authorization ?? null;
const result = await checkSession(authToken)
const result = await checkSession(authToken);
res.status(200).json(result);
}catch (e) {
console.log('Ошибка')
res.status(500).json({error: e})
} catch (e) {
console.log('Ошибка');
res.status(500).json({error: e});
}
});
//todo remove
// todo remove
// Функция для проверки событий
// async function runEvents() {
// await Promise.all([
// suspiciousSearch(),
// checkMaxConsumersCount()
// ])
// await Promise.all([
// suspiciousSearch(),
// checkMaxConsumersCount()
// ])
// }
//интервал проверки событий
// интервал проверки событий
// setInterval(runEvents, 180*1000); //180 секунд
// Запуск сервера

1550
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,8 @@
"main": "index.js",
"scripts": {
"startOld": "node index.js",
"start": "nodemon index.js"
"dev": "nodemon index.js",
"lint": "npx eslint ."
},
"author": "",
"license": "ISC",
@ -18,5 +19,9 @@
"knex": "^3.1.0",
"pg": "^8.12.0",
"uuid": "^11.0.5"
},
"devDependencies": {
"eslint": "^8.57.1",
"eslint-config-google": "^0.14.0"
}
}

View File

@ -2,30 +2,29 @@ import {db} from '../../db.js';
const auth = async (body) => {
try {
const {login, password} = await body
const {login, password} = await body;
const [user] =
await db('users').select(['id'])
.whereNull('deletedAt')
.andWhere('login', '=', login)
.andWhere('password', '=', password)
.whereNull('deletedAt')
.andWhere('login', '=', login)
.andWhere('password', '=', password);
if(!!user){
const {id} = user
if (!!user) {
const {id} = user;
await db('users')
.update('sessionToken', token)
.where('id', '=', id)
.update('sessionToken', token)
.where('id', '=', id);
return {
token
}
}else{
return 'Unauthorized'
token,
};
} else {
return 'Unauthorized';
}
} catch (e) {
return 'error' + e
return 'error' + e;
}
}
};
export {auth};

View File

@ -3,14 +3,14 @@ import {db} from '../../db.js';
const checkSession = async (token) => {
try {
const [sessionIsValid] = await db('users')
.select('id')
.whereNull('deletedAt')
.andWhere('sessionToken', '=', token);
.select('id')
.whereNull('deletedAt')
.andWhere('sessionToken', '=', token);
return !!sessionIsValid
return !!sessionIsValid;
} catch (e) {
return 'error' + e
return 'error' + e;
}
}
};
export {checkSession};

View File

@ -1,13 +1,12 @@
const ping = async (data) => {
const {query: params, body} = data
const {query: params, body} = data;
try {
console.log('body: ', body)
console.log('params: ', params)
return 'pong'
} catch (e) {
return 'error' + e
console.log('body: ', body);
console.log('params: ', params);
return 'pong';
} catch (e) {
return 'error' + e;
}
}
};
export {ping};