diff --git a/src/common/classes/AuracleApi.ts b/src/common/classes/AuracleApi.ts new file mode 100644 index 0000000..eba6092 --- /dev/null +++ b/src/common/classes/AuracleApi.ts @@ -0,0 +1,90 @@ +import express, { Application } from 'express' +import { Sequelize } from 'sequelize/types' + +// import ControllerBase from './class/controller.base' + +export class AuracleApi { + public app: Application + public port: number + public base_url = '/api/v1/' + + private database?: Sequelize; + + constructor(init: { + port: number + middlewares: any + // controllers: Array + modules?: Array + database?: Sequelize + }) { + this.app = express() + this.port = init.port + this.database = init.database + + if (init.modules) { + this.modules(init.modules) + } + + if (init.middlewares) { + this.middlewares(init.middlewares) + } + + // if (init.controllers) { + // this.controller(init.controllers) + // } + + if (init.database) { + this.db_connect(init.database) + } + } + + private modules = (modules: Array) => { + modules.forEach((module) => { + this.app.use(module) + }) + } + + // private controller = (controllers: Array) => { + // controllers.forEach((controller) => { + // this.app.use(this.base_url + controller.path, controller.router) + // console.log(`${controller.name} chargé`) + // }) + // } + + private middlewares = (middleWares: { + forEach: (arg0: (middleWare: any) => void) => void; + }) => { + middleWares.forEach((middleWare) => { + this.app.use(middleWare) + }) + } + + private db_connect = async (db_driver: Sequelize) => { + try { + await db_driver.authenticate(); + console.info('Connection has been established successfully.') + } catch (err) { + console.error('Unable to connect to the database') + console.error(err) + } + } + + public listen = () => { + this.app.listen(this.port, () => { + console.log(`App listening on port ${this.port}`) + }) + } + + public close = async () => { + console.log('Closing the app...') + + console.log('Closing database...') + this.database.close().then(() => { + console.log('App was successfully closed.') + }) + .catch((err) => { + console.error('Encountered an error while closing !') + console.error(err) + }) + } +} diff --git a/src/index.ts b/src/index.ts deleted file mode 100644 index aed8537..0000000 --- a/src/index.ts +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -// MODULES -const express = require('express'); -const bodyParser = require('body-parser'); -const helmet = require('helmet'); -const morgan = require('morgan'); -const cors = require('cors'); // module to format the json response -require('dotenv').config(); - -// CONSTANTS -const port = 2814; - -// Import routes -const routes = require('./routes'); - -// Builds app w/ express -let app = express(); -app.use(bodyParser.json({ limit: '10kb' })); -app.use(cors({ - origin: [ - "http://localhost:8080", - ], - credentials: true, -})); -app.use(morgan('dev')); -app.use(helmet()); - -// Server -app.listen(port, () => console.log(`App listening on port ${port}`)); - -// Entry route -app.use('/api/v1/', routes.auth); - -// Routing -app.use('/api/v1/spells', routes.spells); -app.use('/api/v1/schools', routes.schools); -app.use('/api/v1/meta_schools', routes.meta_schools); -app.use('/api/v1/variables', routes.variables); -app.use('/api/v1/ingredients', routes.ingredients); -app.use('/api/v1/users', routes.users); \ No newline at end of file diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..9c2363f --- /dev/null +++ b/src/main.ts @@ -0,0 +1,54 @@ +import * as dotenv from 'dotenv'; +dotenv.config(); + +// Dependencies +import morgan from 'morgan'; +import helmet from 'helmet'; + +import express from 'express' + +import { AuracleApi } from './common/classes/AuracleApi' + +const port = Number(process.env.API_PORT); + +const app = new AuracleApi({ + port: port, + middlewares: [], + modules: [ + express.json({ limit: '10kb' }), + morgan('dev'), + helmet() + ], +}); + +app.listen() + +process.on('SIGINT', () => app.close()); + +// const routes = require('./routes'); + +// Builds app w/ express +// let app = express(); +// app.use(bodyParser.json({ limit: '10kb' })); +// app.use(cors({ +// origin: [ +// "http://localhost:8080", +// ], +// credentials: true, +// })); +// app.use(morgan('dev')); +// app.use(helmet()); + +// // Server +// app.listen(port, () => console.log(`App listening on port ${port}`)); + +// // Entry route +// app.use('/api/v1/', routes.auth); + +// // Routing +// app.use('/api/v1/spells', routes.spells); +// app.use('/api/v1/schools', routes.schools); +// app.use('/api/v1/meta_schools', routes.meta_schools); +// app.use('/api/v1/variables', routes.variables); +// app.use('/api/v1/ingredients', routes.ingredients); +// app.use('/api/v1/users', routes.users); \ No newline at end of file