import LifecycleUnit from '../lifecycle/Unit.ts' import { Unit } from '../lifecycle/decorators.ts' import { Logging } from '../service/logging/Logging.ts' import StandardLogger from '../service/logging/StandardLogger.ts' import { isLoggingLevel } from '../service/logging/types.ts' import Utility from '../service/utility/Utility.ts' import { make } from '../../../di/src/global.ts' import 'https://deno.land/x/dotenv/load.ts' import { Container } from '../../../di/src/Container.ts' import { Inject } from '../../../di/src/decorator/Injection.ts' import CacheFactory from '../support/CacheFactory.ts' /** * Simple helper for loading ENV values with fallback. * @param {string} name - the environment variable name * @param fallback * @return any */ const env = (name: string, fallback?: any) => { const scaffolding = make(Scaffolding) return scaffolding.env(name) ?? fallback } export { env } /** * Unit service responsible for getting basic essential scaffolding necessary for Daton. * @extends LifecycleUnit */ @Unit() export default class Scaffolding extends LifecycleUnit { constructor( protected logger: Logging, protected utility: Utility, @Inject('injector') protected injector: Container, ) { super() } /** * Helper method for fetching environment variables. * @param {string} name * @return any */ public env(name: string) { return this.utility.infer(Deno.env.get(name) ?? '') } public async up() { this.setup_logging() this.register_factories() } /** * Bootstrap the logging service, and set the appropriate logging level. */ public setup_logging() { StandardLogger.register() try { this.logger.verbose('Attempting to load logging level from the environment') const env_level = this.env('DATON_LOGGING_LEVEL') this.logger.verbose(`Read logging level: ${env_level}`) if ( isLoggingLevel(env_level) ) { this.logger.verbose('Logging level is valid.') this.logger.level = env_level this.logger.debug(`Set logging level from environment: ${env_level}`) } } catch (e) {} this.logger.info('Logging initialized.', true) } /** * Register the necessary core factories with the IoC container. */ public register_factories() { this.logger.verbose('Adding the cache production factory to the container...') this.injector.register_factory(new CacheFactory()) } }