handle errors & connection & createUser
This commit is contained in:
Generated
+925
-31
File diff suppressed because it is too large
Load Diff
@@ -12,9 +12,11 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/dockerode": "^3.3.14",
|
||||
"@types/express": "^4.17.14",
|
||||
"@types/jsonwebtoken": "^9.0.0",
|
||||
"@types/node": "^18.11.11",
|
||||
"nodemon": "^2.0.20",
|
||||
"ts-node": "^10.9.1",
|
||||
@@ -23,9 +25,11 @@
|
||||
"typescript": "^4.9.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.1.0",
|
||||
"cors": "^2.8.5",
|
||||
"dockerode": "^3.3.4",
|
||||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"pg": "^8.8.0",
|
||||
"pg-hstore": "^2.3.4",
|
||||
"sequelize": "^6.26.0"
|
||||
|
||||
+3
-1
@@ -7,7 +7,9 @@ export const config = {
|
||||
DATABASE: "docker",
|
||||
PORT: 5432,
|
||||
HOST: "localhost"
|
||||
}
|
||||
},
|
||||
jwtSecret: "RUqz!Nwo4Q6t4&Im^2@54DZ@l6T",
|
||||
saltRounds: 10
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ const connection = new Sequelize(config.database.DATABASE, config.database.USERN
|
||||
dialect: 'postgres'
|
||||
});
|
||||
|
||||
|
||||
const tryConnection = async () => {
|
||||
try {
|
||||
await connection.authenticate();
|
||||
|
||||
+6
-1
@@ -6,6 +6,8 @@ import { UnknownRoutesHandler } from '~/middlewares/unknownRoutes.handler'
|
||||
import {UsersController} from "~/resources/users/users.controller";
|
||||
import {ContainersController} from "~/resources/containers/containers.controller";
|
||||
import {ImagesController} from "~/resources/images/images.controller";
|
||||
import dbInit from "~/database/init";
|
||||
import {AuthController} from "~/resources/auth/auth.controller";
|
||||
|
||||
|
||||
const app = express()
|
||||
@@ -14,11 +16,12 @@ const app = express()
|
||||
app.use(express.json())
|
||||
|
||||
app.use(cors())
|
||||
|
||||
app.use(ExceptionsHandler)
|
||||
|
||||
app.use('/users', UsersController)
|
||||
app.use('/containers', ContainersController)
|
||||
app.use('/images', ImagesController)
|
||||
app.use('/auth', AuthController)
|
||||
|
||||
app.get('/', (req, res) => res.send('API DOCKER-android'))
|
||||
|
||||
@@ -30,5 +33,7 @@ app.all('*', UnknownRoutesHandler)
|
||||
|
||||
app.use(ExceptionsHandler)
|
||||
|
||||
dbInit()
|
||||
|
||||
|
||||
app.listen(config.API_PORT, () => console.log("Server running on port " + config.API_PORT))
|
||||
|
||||
@@ -14,5 +14,5 @@ export const ExceptionsHandler = (err: any, req: Request, res: Response, next: N
|
||||
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: 'Erreur interne' })
|
||||
.json({ error: 'INTERNAL ERROR' })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import {Router} from 'express'
|
||||
import {BadRequestException, HttpException} from "~/utils/exception";
|
||||
import {AuthService} from "~/resources/auth/auth.service";
|
||||
|
||||
const AuthController = Router()
|
||||
|
||||
const service = new AuthService()
|
||||
|
||||
|
||||
AuthController.post('/', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
if (!req.body.username || !req.body.password)
|
||||
throw new BadRequestException("PASSWORD OR USERNAME IS MISSING");
|
||||
|
||||
const token = await service.auth(req.body.username, req.body.password)
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({token});
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
export {AuthController}
|
||||
@@ -0,0 +1,22 @@
|
||||
import UserModel from "~/resources/users/user.model";
|
||||
import {BadRequestException, NotFoundException} from "~/utils/exception";
|
||||
import bcrypt from 'bcrypt';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import {config} from "~/config";
|
||||
|
||||
export class AuthService {
|
||||
async auth(username: string, password: string) {
|
||||
const user = await UserModel.findOne({where: {username: username}});
|
||||
|
||||
if (!user)
|
||||
throw new NotFoundException("USER NOT FOUND");
|
||||
|
||||
const passwordIsCorrect = await bcrypt.compare(password, user.password)
|
||||
|
||||
if (!passwordIsCorrect)
|
||||
throw new BadRequestException("PASSWORD INCORRECT")
|
||||
|
||||
|
||||
return jwt.sign( {id: user.id, username: user.username, role: user.role}, config.jwtSecret)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Router } from 'express'
|
||||
import {Router} from 'express'
|
||||
import {NotFoundException} from "~/utils/exception";
|
||||
import {ContainersService} from "~/resources/containers/containers.service";
|
||||
|
||||
@@ -7,46 +7,85 @@ const ContainersController = Router()
|
||||
const service = new ContainersService()
|
||||
|
||||
|
||||
ContainersController.get('/', async (req, res) => {
|
||||
return res
|
||||
.status(200)
|
||||
.json(await service.findAll())
|
||||
ContainersController.get('/', async (req, res, next) => {
|
||||
try {
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(await service.findAll())
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
ContainersController.get('/:id', async (req, res) => {
|
||||
const container = await service.findOne(req.params.id)
|
||||
ContainersController.get('/:id', async (req, res, next) => {
|
||||
|
||||
if (!container) {
|
||||
throw new NotFoundException('Container Not Found')
|
||||
try {
|
||||
|
||||
const container = await service.findOne(req.params.id)
|
||||
|
||||
if (!container) {
|
||||
throw new NotFoundException('Container Not Found')
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(container)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/stop', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.stop(req.params.id)
|
||||
return res.status(201)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
ContainersController.delete('/:id', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.remove(req.params.id)
|
||||
return res.status(204)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/restart', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.restart(req.params.id)
|
||||
return res.status(201)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(container)
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/stop', async (req, res) => {
|
||||
await service.stop(req.params.id)
|
||||
ContainersController.post('/:id/start', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.start(req.params.id)
|
||||
return res.status(201)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
return res.status(201)
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/remove', async (req, res) => {
|
||||
await service.remove(req.params.id)
|
||||
|
||||
return res.status(204)
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/restart', async (req, res) => {
|
||||
await service.restart(req.params.id)
|
||||
|
||||
return res.status(201)
|
||||
})
|
||||
|
||||
ContainersController.post('/:id/start', async (req, res) => {
|
||||
await service.start(req.params.id)
|
||||
|
||||
return res.status(201)
|
||||
})
|
||||
|
||||
export { ContainersController }
|
||||
export {ContainersController}
|
||||
|
||||
@@ -2,7 +2,7 @@ import Docker from 'dockerode'
|
||||
export class ContainersService {
|
||||
async findAll() {
|
||||
const docker = new Docker();
|
||||
return await docker.listContainers();
|
||||
return await docker.listContainers({'all': true});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
|
||||
@@ -6,33 +6,63 @@ const ImagesController = Router()
|
||||
|
||||
const service = new ImagesService()
|
||||
|
||||
ImagesController.get('/', async (req, res) => {
|
||||
return res
|
||||
.status(200)
|
||||
.json(await service.findAll())
|
||||
})
|
||||
ImagesController.get('/', async (req, res, next) => {
|
||||
|
||||
ImagesController.get('/:id', async (req, res) => {
|
||||
const container = await service.findOne(req.params.id)
|
||||
try {
|
||||
|
||||
if (!container) {
|
||||
throw new NotFoundException('Container Not Found')
|
||||
return res
|
||||
.status(200)
|
||||
.json(await service.findAll())
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(container)
|
||||
})
|
||||
|
||||
ImagesController.delete('/prune', async (req, res) => {
|
||||
await service.prune()
|
||||
return res.status(204)
|
||||
ImagesController.get('/:id', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
const container = await service.findOne(req.params.id)
|
||||
|
||||
if (!container) {
|
||||
throw new NotFoundException('Container Not Found')
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(container)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
ImagesController.delete('/:id', async (req, res) => {
|
||||
await service.remove(req.params.id)
|
||||
|
||||
return res.status(204)
|
||||
ImagesController.delete('/prune', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.prune()
|
||||
return res.status(204)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
ImagesController.delete('/:id', async (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
await service.remove(req.params.id)
|
||||
return res.status(204)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
export { ImagesController }
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import {UserInput, UserOutput} from "~/resources/users/user.interface";
|
||||
import UserModel from "~/resources/users/user.model";
|
||||
import {NotFoundException} from "~/utils/exception";
|
||||
|
||||
class UserRepository {
|
||||
async create (payload: UserInput): Promise<UserOutput> {
|
||||
return await UserModel.create(payload)
|
||||
}
|
||||
|
||||
async findAll(): Promise<UserOutput[]> {
|
||||
return await UserModel.findAll();
|
||||
}
|
||||
|
||||
async findOneById(id: number): Promise<UserModel | null> {
|
||||
const user = UserModel.findByPk(id);
|
||||
|
||||
if (!user)
|
||||
throw new NotFoundException("User with id " + id + " not found")
|
||||
return await UserModel.findOne({where: {id: id}});
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,65 @@
|
||||
import { Router } from 'express'
|
||||
import {Router} from 'express'
|
||||
import {UsersService} from "~/resources/users/users.service";
|
||||
import {BadRequestException, NotFoundException} from "~/utils/exception";
|
||||
import {BadRequestException, HttpException, NotFoundException} from "~/utils/exception";
|
||||
|
||||
const UsersController = Router()
|
||||
|
||||
const service = new UsersService()
|
||||
|
||||
|
||||
UsersController.get('/', (req, res) => {
|
||||
return res
|
||||
.status(200)
|
||||
.json(service.findAll())
|
||||
UsersController.get('/', (req, res, next) => {
|
||||
|
||||
try {
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(service.findAll())
|
||||
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
UsersController.get('/:id', (req, res) => {
|
||||
const id = Number(req.params.id)
|
||||
UsersController.get('/:id', (req, res, next) => {
|
||||
try {
|
||||
|
||||
if (!Number.isInteger(id)) {
|
||||
throw new BadRequestException('ID non valide')
|
||||
const id = Number(req.params.id)
|
||||
|
||||
if (!Number.isInteger(id)) {
|
||||
throw new BadRequestException('ID non valide')
|
||||
}
|
||||
|
||||
const pet = service.findOne(id)
|
||||
|
||||
if (!pet) {
|
||||
throw new NotFoundException('Animal introuvable')
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(pet)
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
|
||||
const pet = service.findOne(id)
|
||||
|
||||
if (!pet) {
|
||||
throw new NotFoundException('Animal introuvable')
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(pet)
|
||||
})
|
||||
|
||||
export { UsersController }
|
||||
UsersController.post('/', async (req, res, next) => {
|
||||
try {
|
||||
const password = req.body.password;
|
||||
const username = req.body.username;
|
||||
const role = req.body.role;
|
||||
|
||||
if (!password || !username || !role || !["ADMIN", "USER", "READER"].includes(role))
|
||||
throw new BadRequestException("BODY IS INCORRECT");
|
||||
|
||||
return res
|
||||
.status(201)
|
||||
.json(await service.create(username, password, role))
|
||||
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
export {UsersController}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import UserModel from "~/resources/users/user.model";
|
||||
import {UserOutput} from "~/resources/users/user.interface";
|
||||
import {NotFoundException} from "~/utils/exception";
|
||||
import {BadRequestException, NotFoundException} from "~/utils/exception";
|
||||
import bcrypt from "bcrypt";
|
||||
import {config} from "~/config";
|
||||
import * as console from "console";
|
||||
|
||||
export class UsersService {
|
||||
async findAll(): Promise<UserModel[]> {
|
||||
@@ -15,4 +17,14 @@ export class UsersService {
|
||||
return user;
|
||||
}
|
||||
|
||||
async create(username: string, password: string, role: "ADMIN" | "USER" | "READER") {
|
||||
|
||||
const existingUser = await UserModel.findOne({where: {username: username}})
|
||||
console.log(existingUser)
|
||||
if (existingUser)
|
||||
throw new BadRequestException("USER ALREADY EXIST")
|
||||
const hash = await bcrypt.hash(password, config.saltRounds);
|
||||
|
||||
return await UserModel.create({username: username, password: hash, role: role});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import {ApiException} from "~~/types/exception";
|
||||
|
||||
class Exception implements ApiException {
|
||||
export class HttpException implements ApiException {
|
||||
constructor(readonly error: any, readonly status: number) {}
|
||||
}
|
||||
|
||||
|
||||
export class NotFoundException extends Exception {
|
||||
export class NotFoundException extends HttpException {
|
||||
|
||||
constructor(error: any) {
|
||||
super(error, 404)
|
||||
}
|
||||
}
|
||||
|
||||
export class BadRequestException extends Exception {
|
||||
export class BadRequestException extends HttpException {
|
||||
|
||||
constructor(error: any) {
|
||||
super(error, 400)
|
||||
|
||||
Reference in New Issue
Block a user