2020-06-17 14:48:01 +00:00
|
|
|
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'
|
2020-07-06 14:53:03 +00:00
|
|
|
import { isLoggingLevel } from '../service/logging/types.ts'
|
|
|
|
import Utility from '../service/utility/Utility.ts'
|
2020-09-05 15:12:33 +00:00
|
|
|
import {container, make} from '../../../di/src/global.ts'
|
2020-07-06 14:53:03 +00:00
|
|
|
import 'https://deno.land/x/dotenv/load.ts'
|
|
|
|
import { Container } from '../../../di/src/Container.ts'
|
|
|
|
import { Inject } from '../../../di/src/decorator/Injection.ts'
|
2020-07-24 14:53:30 +00:00
|
|
|
import CacheFactory from '../support/CacheFactory.ts'
|
2020-09-04 14:40:16 +00:00
|
|
|
import { path } from '../external/std.ts'
|
2020-07-06 14:53:03 +00:00
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Simple helper for loading ENV values with fallback.
|
|
|
|
* @param {string} name - the environment variable name
|
|
|
|
* @param fallback
|
|
|
|
* @return any
|
|
|
|
*/
|
2020-07-21 03:54:25 +00:00
|
|
|
const env = (name: string, fallback?: any) => {
|
2020-09-05 15:22:18 +00:00
|
|
|
const scaffolding = make(Scaffolding)
|
2020-07-06 14:53:03 +00:00
|
|
|
return scaffolding.env(name) ?? fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
export { env }
|
2020-06-17 14:48:01 +00:00
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Unit service responsible for getting basic essential scaffolding necessary for Daton.
|
|
|
|
* @extends LifecycleUnit
|
|
|
|
*/
|
2020-06-17 14:48:01 +00:00
|
|
|
@Unit()
|
|
|
|
export default class Scaffolding extends LifecycleUnit {
|
2020-09-04 14:40:16 +00:00
|
|
|
public base_dir!: string
|
|
|
|
|
2020-06-17 14:48:01 +00:00
|
|
|
constructor(
|
2020-07-06 14:53:03 +00:00
|
|
|
protected logger: Logging,
|
|
|
|
protected utility: Utility,
|
|
|
|
@Inject('injector') protected injector: Container,
|
2020-09-05 15:04:27 +00:00
|
|
|
base_script: string
|
2020-09-04 14:40:16 +00:00
|
|
|
) {
|
|
|
|
super()
|
2020-09-05 14:28:24 +00:00
|
|
|
this.base_dir = path.dirname(base_script)//.replace('file:///', '/')
|
2020-09-04 14:40:16 +00:00
|
|
|
}
|
2020-07-06 14:53:03 +00:00
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Helper method for fetching environment variables.
|
|
|
|
* @param {string} name
|
|
|
|
* @return any
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public env(name: string) {
|
|
|
|
return this.utility.infer(Deno.env.get(name) ?? '')
|
2020-06-17 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:53:03 +00:00
|
|
|
public async up() {
|
|
|
|
this.setup_logging()
|
2020-07-24 14:53:30 +00:00
|
|
|
this.register_factories()
|
2020-09-04 14:40:16 +00:00
|
|
|
|
|
|
|
this.logger.info(`Base directory: ${this.base_dir}`)
|
2020-06-17 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Bootstrap the logging service, and set the appropriate logging level.
|
|
|
|
*/
|
2020-06-17 14:48:01 +00:00
|
|
|
public setup_logging() {
|
|
|
|
StandardLogger.register()
|
2020-07-06 14:53:03 +00:00
|
|
|
|
|
|
|
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) {}
|
|
|
|
|
2020-06-17 14:48:01 +00:00
|
|
|
this.logger.info('Logging initialized.', true)
|
|
|
|
}
|
2020-07-24 14:53:30 +00:00
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Register the necessary core factories with the IoC container.
|
|
|
|
*/
|
2020-07-24 14:53:30 +00:00
|
|
|
public register_factories() {
|
|
|
|
this.logger.verbose('Adding the cache production factory to the container...')
|
|
|
|
this.injector.register_factory(new CacheFactory())
|
|
|
|
}
|
2020-06-17 14:48:01 +00:00
|
|
|
}
|