Refactor units to be generic; start bundles; start app index.ts
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import { Service } from '../../../di/src/decorator/Service.ts'
|
||||
import { Logging } from '../service/logging/Logging.ts'
|
||||
import Unit from './Unit.ts'
|
||||
import { container, make } from '../../../di/src/global.ts'
|
||||
import { DependencyKey } from '../../../di/src/type/DependencyKey.ts'
|
||||
import {Service} from '../../../di/src/decorator/Service.ts'
|
||||
import {Logging} from '../service/logging/Logging.ts'
|
||||
import LifecycleUnit from './Unit.ts'
|
||||
import {container, make} from '../../../di/src/global.ts'
|
||||
import {DependencyKey} from '../../../di/src/type/DependencyKey.ts'
|
||||
import RunLevelErrorHandler from '../error/RunLevelErrorHandler.ts'
|
||||
import {Status} from '../const/status.ts'
|
||||
import Instantiable from "../../../di/src/type/Instantiable.ts";
|
||||
import {Collection} from "../collection/Collection.ts";
|
||||
|
||||
@Service()
|
||||
export default class Application {
|
||||
protected instantiated_units: Collection<LifecycleUnit> = new Collection<LifecycleUnit>()
|
||||
|
||||
constructor(
|
||||
protected logger: Logging,
|
||||
protected rleh: RunLevelErrorHandler,
|
||||
protected units: Unit[],
|
||||
protected units: (Instantiable<LifecycleUnit>)[],
|
||||
) {}
|
||||
|
||||
make(token: DependencyKey) {
|
||||
@@ -22,7 +27,12 @@ export default class Application {
|
||||
}
|
||||
|
||||
async up() {
|
||||
|
||||
this.logger.info('Starting Daton...', true)
|
||||
for ( const unit_class of this.units ) {
|
||||
const unit = this.make(unit_class)
|
||||
this.instantiated_units.push(unit)
|
||||
await this.start_unit(unit)
|
||||
}
|
||||
}
|
||||
|
||||
async down() {
|
||||
@@ -31,7 +41,8 @@ export default class Application {
|
||||
|
||||
async run() {
|
||||
try {
|
||||
this.logger.info('Starting Daton...')
|
||||
await this.up()
|
||||
await this.down()
|
||||
} catch (e) {
|
||||
await this.app_error(e)
|
||||
}
|
||||
@@ -40,4 +51,19 @@ export default class Application {
|
||||
async app_error(e: Error) {
|
||||
this.rleh.handle(e)
|
||||
}
|
||||
|
||||
protected async start_unit(unit: LifecycleUnit) {
|
||||
try {
|
||||
unit.status = Status.Starting
|
||||
this.logger.info(`Starting ${unit.constructor.name}...`)
|
||||
await unit.up()
|
||||
this.logger.verbose(`Successfully started ${unit.constructor.name}`)
|
||||
unit.status = Status.Running
|
||||
} catch (e) {
|
||||
unit.status = Status.Error
|
||||
this.logger.error(`Error encountered while starting ${unit.constructor.name}. Will attempt to proceed.`)
|
||||
this.logger.debug(e.message)
|
||||
this.logger.verbose(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { Status, isStatus } from '../const/status.ts'
|
||||
import { Unit } from './decorators.ts'
|
||||
import { Collection } from '../collection/Collection.ts'
|
||||
import {container, make} from '../../../di/src/global.ts'
|
||||
import {DependencyKey} from "../../../di/src/type/DependencyKey.ts";
|
||||
import Instantiable, {isInstantiable} from "../../../di/src/type/Instantiable.ts";
|
||||
import {DependencyKey} from '../../../di/src/type/DependencyKey.ts'
|
||||
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
||||
|
||||
const isLifecycleUnit = (something: any): something is (typeof LifecycleUnit) => {
|
||||
return isInstantiable(something) && something.prototype instanceof LifecycleUnit
|
||||
|
||||
2
lib/src/module.ts
Normal file
2
lib/src/module.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { default as Scaffolding } from './unit/Scaffolding.ts'
|
||||
export { default as Application } from './lifecycle/Application.ts'
|
||||
@@ -1,6 +1,6 @@
|
||||
export default abstract class Cache {
|
||||
public abstract async fetch(key: string): Promise<any>;
|
||||
public abstract async put(key: string, value: any): Promise<void>;
|
||||
public abstract async put(key: string, value: string): Promise<void>;
|
||||
public abstract async has(key: string): Promise<boolean>;
|
||||
public abstract async drop(key: string): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Collection } from '../collection/Collection.ts'
|
||||
|
||||
export interface InMemCacheItem {
|
||||
key: string,
|
||||
item: any,
|
||||
item: string,
|
||||
}
|
||||
|
||||
export class InMemCache extends Cache {
|
||||
@@ -14,7 +14,7 @@ export class InMemCache extends Cache {
|
||||
if ( item ) return item.item
|
||||
}
|
||||
|
||||
public async put(key: string, item: any) {
|
||||
public async put(key: string, item: string) {
|
||||
const existing = this.items.firstWhere('key', '=', key)
|
||||
if ( existing ) existing.item = item
|
||||
else this.items.push({ key, item })
|
||||
|
||||
@@ -8,11 +8,11 @@ export interface CanonicalDefinition {
|
||||
imported: any,
|
||||
}
|
||||
|
||||
export class Canonical extends LifecycleUnit {
|
||||
export class Canonical<T> extends LifecycleUnit {
|
||||
protected base_path: string = '.'
|
||||
protected suffix: string = '.ts'
|
||||
protected canonical_item: string = ''
|
||||
protected _items: { [key: string]: any } = {}
|
||||
protected _items: { [key: string]: T } = {}
|
||||
|
||||
public get path(): string {
|
||||
return path.resolve(this.base_path)
|
||||
@@ -32,7 +32,7 @@ export class Canonical extends LifecycleUnit {
|
||||
this.make(Canon).register_resource(this.canonical_items, (key: string) => this.get(key))
|
||||
}
|
||||
|
||||
public async init_canonical_item(definition: CanonicalDefinition): Promise<any> {
|
||||
public async init_canonical_item(definition: CanonicalDefinition): Promise<T> {
|
||||
return definition.imported.default
|
||||
}
|
||||
|
||||
@@ -47,12 +47,7 @@ export class Canonical extends LifecycleUnit {
|
||||
return { canonical_name, original_name, imported }
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
const key_parts = key.split('.')
|
||||
let desc_value = this._items
|
||||
key_parts.forEach(part => {
|
||||
desc_value = desc_value[part]
|
||||
})
|
||||
return desc_value
|
||||
public get(key: string): T | undefined {
|
||||
return this._items[key]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {Canonical} from './Canonical.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
import {RecursiveCanonical} from './RecursiveCanonical.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Config extends Canonical {
|
||||
export default class Config extends RecursiveCanonical {
|
||||
protected base_path = './app/configs'
|
||||
protected suffix = '.config.ts'
|
||||
protected canonical_item = 'config'
|
||||
|
||||
@@ -4,7 +4,7 @@ import Controller from '../http/Controller.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Controllers extends InstantiableCanonical {
|
||||
export default class Controllers extends InstantiableCanonical<Controller> {
|
||||
protected base_path = './app/http/controllers'
|
||||
protected canonical_item = 'controller'
|
||||
protected suffix = '.controller.ts'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {Canonical, CanonicalDefinition} from './Canonical.ts'
|
||||
import {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
||||
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
||||
|
||||
export class InvalidCanonicalExportError extends Error {
|
||||
constructor(name: string) {
|
||||
@@ -7,8 +7,8 @@ export class InvalidCanonicalExportError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class InstantiableCanonical extends Canonical {
|
||||
public async init_canonical_item(def: CanonicalDefinition) {
|
||||
export class InstantiableCanonical<T> extends Canonical<Instantiable<T>> {
|
||||
public async init_canonical_item(def: CanonicalDefinition): Promise<Instantiable<T>> {
|
||||
if ( isInstantiable(def.imported.default) ) {
|
||||
return this.make(def.imported.default)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Middleware } from '../http/Middleware.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Middlewares extends InstantiableCanonical {
|
||||
export default class Middlewares extends InstantiableCanonical<Middleware> {
|
||||
protected base_path = './app/http/middleware'
|
||||
protected canonical_item = 'middleware'
|
||||
protected suffix = '.middleware.ts'
|
||||
|
||||
12
lib/src/unit/RecursiveCanonical.ts
Normal file
12
lib/src/unit/RecursiveCanonical.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {Canonical} from './Canonical.ts'
|
||||
|
||||
export class RecursiveCanonical extends Canonical<any> {
|
||||
public get(key: string): any | undefined {
|
||||
const parts = key.split('.')
|
||||
let current_value = this._items
|
||||
for ( const part of parts ) {
|
||||
current_value = current_value?.[part]
|
||||
}
|
||||
return current_value
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import { Container } from '../../../di/src/Container.ts'
|
||||
import { Inject } from '../../../di/src/decorator/Injection.ts'
|
||||
import CacheFactory from "../support/CacheFactory.ts";
|
||||
|
||||
const env = (name: string, fallback: any) => {
|
||||
const env = (name: string, fallback?: any) => {
|
||||
const scaffolding = make(Scaffolding)
|
||||
return scaffolding.env(name) ?? fallback
|
||||
}
|
||||
@@ -31,6 +31,9 @@ export default class Scaffolding extends LifecycleUnit {
|
||||
|
||||
public async up() {
|
||||
this.setup_logging()
|
||||
|
||||
this.logger.verbose('Adding the cache production factory to the container...')
|
||||
this.injector.register_factory(new CacheFactory())
|
||||
}
|
||||
|
||||
public setup_logging() {
|
||||
@@ -48,8 +51,5 @@ export default class Scaffolding extends LifecycleUnit {
|
||||
} catch (e) {}
|
||||
|
||||
this.logger.info('Logging initialized.', true)
|
||||
|
||||
this.logger.verbose('Adding the cache production factory to the container...')
|
||||
this.injector.register_factory(new CacheFactory())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user