You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.8 KiB

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 {container, 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'
import { path } from '../external/std.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)
const scaffolding = container.get_existing_instance(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 {
public base_dir!: string
constructor(
protected logger: Logging,
protected utility: Utility,
@Inject('injector') protected injector: Container,
base_script: string
) {
super()
this.base_dir = path.dirname(base_script)//.replace('file:///', '/')
}
/**
* 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()
this.logger.info(`Base directory: ${this.base_dir}`)
}
/**
* 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())
}
}