Created main class instance
This commit is contained in:
90
src/common/classes/AuracleApi.ts
Normal file
90
src/common/classes/AuracleApi.ts
Normal file
@@ -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<ControllerBase>
|
||||
modules?: Array<any>
|
||||
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<any>) => {
|
||||
modules.forEach((module) => {
|
||||
this.app.use(module)
|
||||
})
|
||||
}
|
||||
|
||||
// private controller = (controllers: Array<ControllerBase>) => {
|
||||
// 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)
|
||||
})
|
||||
}
|
||||
}
|
||||
41
src/index.ts
41
src/index.ts
@@ -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);
|
||||
54
src/main.ts
Normal file
54
src/main.ts
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user