LoRaWAN-Signal-Mapping/server/bootstrap/container.mjs

72 lines
1.9 KiB
JavaScript

"use strict";
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import a from 'awilix';
import Ansi from '../Helpers/Ansi.mjs';
import Log from '../Helpers/Log.mjs';
import TTNAppServer from '../ttn-app-server/TTNAppServer.mjs';
import MessageHandler from '../ttn-app-server/MessageHandler.mjs';
import DataProcessor from '../process-data/DataProcessor.mjs';
import DatasetFetcher from '../train-ai/DatasetFetcher.mjs';
import settings from './settings.mjs';
import database_init from '../bootstrap/database_init.mjs';
const root_dir = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
export default async function setup() {
const c = a.createContainer({
injectionMode: a.InjectionMode.PROXY
});
c.register({
root_dir: a.asValue(root_dir),
settings: a.asValue(settings),
ansi: a.asClass(Ansi).singleton(),
log: a.asClass(Log).singleton(),
database: a.asFunction(database_init).singleton(),
TTNAppServer: a.asClass(TTNAppServer),
MessageHandler: a.asClass(MessageHandler),
DataProcessor: a.asClass(DataProcessor),
DatasetFetcher: a.asClass(DatasetFetcher)
});
// Enable / disable colourising the output
c.cradle.ansi.enabled = settings.logging.colour;
c.cradle.ansi.escape_codes();
let repo_filenames = await fs.readdir(path.join(root_dir, "./Repos.SQLite"));
for(let repo_filename of repo_filenames) {
let class_root = await import(path.join(root_dir, `./Repos.SQLite/${repo_filename}`)),
class_name = class_root.default.prototype.constructor.name;
c.register(
class_name,
a.asClass(class_root.default).singleton(),
);
}
// console.log(c.registrations);
// c.loadModules([
// "./Repos.SQLite/*.mjs"
// // "./Repos.SQLite/ReadingRepo.mjs"
// ], {
// cwd: root_dir,
// resolverOptions: {
// register: a.asClass,
// lifetime: a.Lifetime.SINGLETON
// }
// });
return c;
}