diff --git a/src/app.ts b/src/app.ts index 0acfe65..953f3b6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,21 +7,17 @@ import bodyparser from "body-parser"; import 'reflect-metadata'; import routerMain from './routes/index'; // const loaders = require('./loaders'); -import * as loaders from './loaders/index'; +import loaders from './loaders/index'; dotenvSafe.config(); -const app = express(); +let app = express(); const PORT = process.env.APP_PORT || 4010; -createConnection() - .then((connection) => {}) - .catch((err) => { - consola.error(err); - }) - .finally(() => { - // await loaders.init(app); - app.use('/api/', routerMain); - app.listen(PORT, () => { - consola.success(`⚡️[server]: Server is running at http://localhost:${PORT}`); - }); +async function main() { + await loaders(app); + app.listen(PORT, () => { + consola.success(`⚡️[server]: Server is running at http://localhost:${PORT}`); }); +} + +main() \ No newline at end of file diff --git a/src/loaders/database.ts b/src/loaders/database.ts new file mode 100644 index 0000000..1ca6f46 --- /dev/null +++ b/src/loaders/database.ts @@ -0,0 +1,5 @@ +import {createConnection} from "typeorm"; + +export default async ()=> { + return await createConnection(); +}; \ No newline at end of file diff --git a/src/loaders/express.ts b/src/loaders/express.ts new file mode 100644 index 0000000..cb36dfc --- /dev/null +++ b/src/loaders/express.ts @@ -0,0 +1,14 @@ +import {Application} from "express"; +import bodyParser from 'body-parser'; +import cors from 'cors'; + +export default async (app: Application) => { + app.get('/status', (req, res) => res.status(200).end()); + app.enable('trust proxy'); + + app.use(cors()); + // app.use(bodyParser.urlencoded({ extended: false })); + + // more middlewares + return app; +}; diff --git a/src/loaders/index.ts b/src/loaders/index.ts index 4f263d2..56e2286 100644 --- a/src/loaders/index.ts +++ b/src/loaders/index.ts @@ -1,14 +1,11 @@ -import express from 'express'; -import bodyParser from 'body-parser'; -import cors from 'cors'; +import expressLoader from "./express"; +import routeLoader from "./routes"; +import databaseLoader from "./database"; +import {Application} from "express"; -export default async (app) => { - app.get('/status', (req, res) => res.status(200).end()); - app.enable('trust proxy'); - - app.use(cors()); - // app.use(bodyParser.urlencoded({ extended: false })); - - // more middlewares - return app; +export default async (app: Application) => { + await expressLoader(app); + await routeLoader(app); + databaseLoader(); + return app; }; diff --git a/src/loaders/routes.ts b/src/loaders/routes.ts new file mode 100644 index 0000000..1d9309c --- /dev/null +++ b/src/loaders/routes.ts @@ -0,0 +1,7 @@ +import {Application} from "express"; +import routerMain from "../routes/index"; + +export default async (app: Application) => { + app.use('/api/', routerMain); + return app; +};