-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(main): Rework application main
- Loading branch information
Sander Koenders
committed
Sep 10, 2018
1 parent
7fa7a3e
commit 355a96d
Showing
1 changed file
with
23 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,37 @@ | ||
import * as express from 'express'; | ||
import Loader from './ioc/loader'; | ||
import Ioc from './ioc/ioc'; | ||
import * as path from 'path'; | ||
import { container } from './ioc/ioc'; | ||
import TYPES from './constants/types'; | ||
import HelloController from './modules/testModule/helloController'; | ||
|
||
class Main { | ||
private app: express.Application; | ||
private app: express.Application; | ||
private environment: Environment; | ||
private container: Ioc; | ||
|
||
public constructor(environment: any) { | ||
this.app = express(); | ||
public constructor(environment: Environment) { | ||
this.app = express(); | ||
this.environment = environment; | ||
this.container = new Ioc(path.resolve(__dirname, '..')); | ||
} | ||
|
||
this.initializeDependencyInjector(environment); | ||
} | ||
public async initialize() { | ||
// Dynamically load annotated classes inside the loadPaths context | ||
this.container.componentScan(this.environment.loadPaths); | ||
|
||
public onListening() { | ||
const helloController: any = container.getNamed('Music/HelloController', 'testModule'); | ||
// Bind dependencies that need manual defining | ||
this.container.bind<Environment>(TYPES.Environment).toConstantValue(this.environment); | ||
} | ||
|
||
helloController.hello('Sander'); | ||
} | ||
public onListening() { | ||
const helloController = this.container.getNamed<HelloController>('Music/HelloController', 'awesomeModule'); | ||
|
||
private initializeDependencyInjector(environment: any) { | ||
container.bind<Environment>(TYPES.Environment).toConstantValue(environment); | ||
helloController.hello('Sander'); | ||
} | ||
|
||
this.loadJsFiles(environment.loadPaths); | ||
} | ||
|
||
private loadJsFiles(loadPaths: string[]) { | ||
loadPaths = loadPaths.map(dir => path.join(__dirname, dir)); | ||
|
||
Loader.load(loadPaths); | ||
} | ||
|
||
public get App(): express.Application { | ||
return this.app; | ||
} | ||
public get App(): express.Application { | ||
return this.app; | ||
} | ||
} | ||
|
||
export = Main; |