Skip to content

Commit 21ae846

Browse files
author
hirsch88
committed
add more bootstrap magic
1 parent 1e3f908 commit 21ae846

File tree

10 files changed

+67
-209
lines changed

10 files changed

+67
-209
lines changed

src/api/controllers/UserController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { injectable, inject, named } from 'inversify';
22
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response, Request } from 'inversify-express-utils';
33
import { my } from 'my-express';
4-
import { UserService } from '../services/UsersService';
4+
import { UserService } from '../services/UserService';
55
import { Types } from '../../constants/Types';
66
import { Service } from '../../constants/Targets';
77
import { authenticate, populateUser } from '../middlewares';

src/api/middlewares/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ioc } from '../../core/IoC';
1010
import { Log } from '../../core/log';
1111
import { Types } from '../../constants/Types';
1212
import { Service } from '../../constants/Targets';
13-
import { UserService } from '../services/UsersService';
13+
import { UserService } from '../services/UserService';
1414

1515
import { authenticate as Authenticate } from './authenticate';
1616
import { populateUser as PopulateUser } from './populateUser';

src/api/middlewares/populateUser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as request from 'request';
22
import { Container } from 'inversify';
33
import { my } from 'my-express';
44
import { Log } from '../../core/log';
5-
import { UserService } from '../services/UsersService';
5+
import { UserService } from '../services/UserService';
66

77
/**
88
* populateUser middleware

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Log } from './core/log';
2020

2121

2222
bootstrap.configureExpress((app: express.Application) => app
23+
2324
// Report real time server metrics for Express-based node servers
2425
.use(monitor())
2526

src/container.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,11 @@
66
* will then be bonded to the express structure with their defined routes.
77
*/
88

9-
import { interfaces } from 'inversify-express-utils';
109
import { Container } from 'inversify';
11-
import { Types } from './constants/Types';
12-
import { Core, Model, Controller, Service, Repository } from './constants/Targets';
13-
14-
/**
15-
* Core Features
16-
*/
17-
import { events, EventEmitter } from './core/api/events';
18-
import { Log } from './core/log';
1910
import { ioc } from './core/IoC';
2011

21-
/**
22-
* User Resource
23-
*/
24-
import { UserController } from './api/controllers/UserController';
25-
import { UserService } from './api/services/UsersService';
26-
import { UserRepository } from './api/repositories/UserRepository';
27-
import { User } from './api/models/User';
28-
2912
ioc.configure((container: Container) => {
30-
console.log('--> CONTAINER should be last');
31-
32-
/**
33-
* Core
34-
*/
35-
// container.bind<EventEmitter>(Types.Core).toConstantValue(events).whenTargetNamed(Core.Events);
36-
// container.bind<typeof Log>(Types.Core).toConstantValue(Log).whenTargetNamed(Core.Log);
37-
38-
/**
39-
* Model
40-
*/
41-
// container.bind<any>(Types.Model).toConstantValue(User).whenTargetNamed(Model.User);
42-
43-
/**
44-
* Controllers
45-
*/
46-
container.bind<interfaces.Controller>(Types.Controller).to(UserController).whenTargetNamed(Controller.UserController);
47-
48-
/**
49-
* Services
50-
*/
51-
container.bind<UserService>(Types.Service).to(UserService).whenTargetNamed(Service.UserService);
5213

53-
/**
54-
* Repositories
55-
*/
56-
container.bind<UserRepository>(Types.Repository).to(UserRepository).whenTargetNamed(Repository.UserRepository);
5714

5815
return container;
5916
});

src/core/Bootstrap.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ export class Bootstrap {
7272

7373
private async bindIoC(): Promise<void> {
7474
log.info('Binding IoC modules...');
75-
try {
76-
await ioc.bindModules();
77-
} catch (error) {
78-
// TODO
79-
console.error(error);
80-
}
75+
await ioc.bindModules();
8176
}
8277

8378
private setupIoC(): void {

src/core/IoC.ts

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import { Core, Controller, Model, Service, Repository } from '../constants/Targe
1515
import { events, EventEmitter } from './api/events';
1616
import { Log } from './log';
1717

18-
19-
import { User } from '../api/models/User';
18+
const log = new Log('core:IoC');
2019

2120

2221
class IoC {
@@ -39,7 +38,9 @@ class IoC {
3938
public async bindModules(): Promise<void> {
4039
this.bindCore();
4140
await this.bindModels();
42-
// this.bindControllers();
41+
await this.bindRepositories();
42+
await this.bindServices();
43+
await this.bindControllers();
4344

4445
this.container = this.customConfiguration(this.container);
4546
}
@@ -49,36 +50,68 @@ class IoC {
4950
this.container.bind<EventEmitter>(Types.Core).toConstantValue(events).whenTargetNamed(Core.Events);
5051
}
5152

52-
private async bindControllers(): Promise<void> {
53-
this.getFiles('/controllers', (files: string[]) => {
54-
files.forEach((file: any) => {
55-
console.log(file);
56-
const fileExport = require(`${file.path}/${file.fileName}`);
57-
console.log(fileExport);
58-
this.container
59-
.bind<interfaces.Controller>(Types.Controller)
60-
.to(fileExport[file.name])
61-
.whenTargetNamed(Controller[file.name]);
62-
});
53+
private bindModels(): Promise<void> {
54+
return this.bindFiles('/models', Model, (name: any, value: any) => {
55+
this.container
56+
.bind<any>(Types.Model)
57+
.toConstantValue(value)
58+
.whenTargetNamed(name);
6359
});
6460
}
6561

66-
private bindModels(): Promise<void> {
67-
return new Promise<void>((resolve, reject) => {
68-
console.log('Models');
69-
this.getFiles('/models', (files: string[]) => {
70-
files.forEach((file: any) => {
71-
const fileExport = require(`${file.path}/${file.fileName}`);
72-
this.validateExport(fileExport[file.name]);
73-
this.validateTarget(Model, file.name);
62+
private bindRepositories(): Promise<void> {
63+
return this.bindFiles('/repositories', Repository, (name: any, value: any) => {
64+
this.container
65+
.bind<any>(Types.Repository)
66+
.to(value)
67+
.whenTargetNamed(name);
68+
});
69+
}
7470

75-
this.container
76-
.bind<any>(Types.Model)
77-
.toConstantValue(fileExport[file.name])
78-
.whenTargetNamed(Model[file.name]);
71+
private bindServices(): Promise<void> {
72+
return this.bindFiles('/services', Service, (name: any, value: any) => {
73+
this.container
74+
.bind<any>(Types.Service)
75+
.to(value)
76+
.whenTargetNamed(name);
77+
});
78+
}
7979

80-
resolve();
80+
private bindControllers(): Promise<void> {
81+
return this.bindFiles('/controllers', Controller, (name: any, value: any) => {
82+
this.container
83+
.bind<any>(Types.Controller)
84+
.to(value)
85+
.whenTargetNamed(name);
86+
});
87+
}
88+
89+
private bindFiles(path: string, target: any, callback: (name: any, value: any) => void): Promise<void> {
90+
return new Promise<void>((resolve, reject) => {
91+
this.getFiles(path, (files: string[]) => {
92+
files.forEach((file: any) => {
93+
let fileExport;
94+
try {
95+
fileExport = require(`${file.path}/${file.fileName}`);
96+
} catch (e) {
97+
log.warn(e.message);
98+
return;
99+
}
100+
if (fileExport === undefined) {
101+
log.warn(`Could not find the file ${file.name}!`);
102+
return;
103+
}
104+
if (fileExport[file.name] === undefined) {
105+
log.warn(`Name of the file '${file.name}' does not match to the class name!`);
106+
return;
107+
}
108+
if (target && target[file.name] === undefined) {
109+
log.warn(`Please define your '${file.name}' class is in the target constants.`);
110+
return;
111+
}
112+
callback(target[file.name], fileExport[file.name]);
81113
});
114+
resolve();
82115
});
83116
});
84117
}
@@ -92,7 +125,8 @@ class IoC {
92125
private getFiles(path: string, done: (files: any[]) => void): void {
93126
fs.readdir(this.getBasePath() + path, (err: any, files: string[]): void => {
94127
if (err) {
95-
console.error(err);
128+
log.warn(`Could not read the folder ${path}!`);
129+
return;
96130
}
97131
done(files.map((fileName: string) => ({
98132
path: this.getBasePath() + path,
@@ -106,18 +140,6 @@ class IoC {
106140
return fileName.substring(0, fileName.length - 3);
107141
}
108142

109-
private validateExport(value: any): void {
110-
if (!value) {
111-
throw new Error(`${value} is not defined in the target constants`);
112-
}
113-
}
114-
115-
private validateTarget(target: any, value: any): void {
116-
if (target && target[value] === undefined) {
117-
throw new Error(`${value} is not defined in the target constants`);
118-
}
119-
}
120-
121143
}
122144

123145
export const ioc = new IoC();

src/core/old_IoC.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,3 @@ import './app';
2020
import { bootstrap } from './core/Bootstrap';
2121

2222
bootstrap.main();
23-
24-
// import { Server } from './core/Server';
25-
// Start our app and listen for it
26-
// const server = app.listen(app.get('port'));
27-
// Server.use(server, app);
28-
29-
// console.log('app', app);
30-
// app.then(Server.use);

0 commit comments

Comments
 (0)