UNPKG

vmagic

Version:

vMagic is a RESTFul framework for NodeJS applications.

50 lines (42 loc) 1.86 kB
/*eslint no-console: ["error", { allow: ["log"] }] */ "use strict"; import { createServer } from "https"; import { createServer as _createServer } from "http"; import { resolve, join } from "path"; import RequestHandler from "./Component/RequestHandler.js"; import AppDatasource from "./Component/DataSource/AppDatasource.js"; class Magic { constructor() { const srcPath = resolve(join("src")); this.application = { componentPath: resolve(join(srcPath, "Component")), configPath: resolve(join(srcPath, "Config")), controllersPath: resolve(join(srcPath, "Controller")), modelPath: resolve(join(srcPath, "Model")), }; } async initializeDatasource() { const coreConfigPath = `${this.application.configPath}/core.json`; const coreConfigModule = await import(coreConfigPath, { assert: { type: 'json' } }); this.core = coreConfigModule.default; // O JSON será importado como um objeto new AppDatasource(this.core.dataSources); } async start(address, port, options) { await this.initializeDatasource(); const requestHandler = new RequestHandler(this.application); await requestHandler.init(); if (options) { return createServer(options, function (request, response) { requestHandler.process(request, response); }).listen(port, address, () => console.log(`Server running at https://${address}:${port}/`)); } else { return _createServer(function (request, response) { requestHandler.process(request, response); }).listen(port, address, () => console.log(`Server running at http://${address}:${port}/`)); } } getApplication() { return this.application; } } export default Magic;