Skip to content

Commit e90a896

Browse files
author
hirsch88
committed
move api-controller logic into the bootstrap process
1 parent c96e7b0 commit e90a896

File tree

7 files changed

+93
-41
lines changed

7 files changed

+93
-41
lines changed

.env.example

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
APP_NAME=express-typescript-boilerplate
55
APP_ENV=local
66
APP_HOST=http://localhost
7+
APP_URL_PREFIX=/api/v1
78
APP_PORT=3000
8-
APP_URL_PREFIX=/api
99

1010
#
1111
# LOGGING
1212
#
1313
DEBUG=app*,api*,core*
1414
LOG_LEVEL=debug
15-
LOG_ADAPTER=debug
15+
LOG_ADAPTER=winston
16+
17+
#
18+
# API Info
19+
#
20+
API_INFO_ENABLED=true
21+
API_INFO_ROUTE=/info
1622

1723
#
1824
# Swagger Documentation
@@ -21,6 +27,12 @@ SWAGGER_ENABLED=true
2127
SWAGGER_ROUTE=/docs
2228
SWAGGER_FILE=/src/api/swagger.json
2329

30+
#
31+
# Monitor
32+
#
33+
MONITOR_ENABLED=true
34+
MONITOR_ROUTE=/status
35+
2436
#
2537
# DATABASE
2638
#
@@ -42,4 +54,5 @@ DB_SEEDS_DIR=./src/database/seeds
4254
#
4355
# Auth0
4456
#
57+
# AUTH0_HOST=https://w3tecch.auth0.com
4558
AUTH0_HOST=http://localhost:3333

src/api/controllers/ApiController.ts

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

src/constants/Targets.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export const Controller = {
2-
ApiController: 'ApiController',
32
UserController: 'UserController'
43
};
54

src/container.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Container } from 'inversify';
33
import { Types } from './constants/Types';
44
import { Controller, Repository } from './constants/Targets';
55

6-
// Home
7-
import { ApiController } from './api/controllers/ApiController';
86

97
// User Resource
108
import { UserController } from './api/controllers/UserController';
@@ -21,7 +19,6 @@ import { UserRepository } from './api/repositories/UserRepository';
2119
const container = new Container();
2220

2321
// Controllers
24-
container.bind<interfaces.Controller>(Types.Controller).to(ApiController).whenTargetNamed(Controller.ApiController);
2522
container.bind<interfaces.Controller>(Types.Controller).to(UserController).whenTargetNamed(Controller.UserController);
2623

2724
// Services

src/core/Bootstrap.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import 'reflect-metadata';
22
import * as express from 'express';
3+
import * as monitor from 'express-status-monitor';
34
import { Container } from 'inversify';
45
import { InversifyExpressServer } from 'inversify-express-utils';
56
import { Environment } from './Environment';
7+
import { my } from 'my-express';
68
import { exceptionHandler, extendExpressResponse } from './api';
79
import { Log } from './log';
810

@@ -30,7 +32,7 @@ export class Bootstrap {
3032
const app = express();
3133
app.set('host', Environment.get('APP_HOST'));
3234
app.set('port', Bootstrap.normalizePort(Environment.get<string>('PORT') || Environment.get<string>('APP_PORT')));
33-
log.debug('app is defined');
35+
log.info('Starting app...');
3436
return app;
3537
}
3638

@@ -46,9 +48,10 @@ export class Bootstrap {
4648
* @memberof Bootstrap
4749
*/
4850
static build(app: express.Application, container: Container): express.Application {
51+
app = Bootstrap.setupApiInfo(app);
4952
app = Bootstrap.setupSwagger(app);
53+
app = Bootstrap.setupApiMonitor(app);
5054
let server = new InversifyExpressServer(container, undefined, { rootPath: Environment.get<string>('APP_URL_PREFIX') }, app);
51-
log.debug('ioc is bonded');
5255
server = Bootstrap.setupConfigurations(server);
5356
return server.build();
5457
}
@@ -68,6 +71,39 @@ export class Bootstrap {
6871
return server;
6972
}
7073

74+
/**
75+
* Sets up a route for all the api info
76+
*
77+
* @static
78+
* @param {express.Application} app
79+
* @returns {express.Application}
80+
*
81+
* @memberof Bootstrap
82+
*/
83+
static setupApiInfo(app: express.Application): express.Application {
84+
if (Environment.get<string>('API_INFO_ENABLED') === 'true') {
85+
app.get(Environment.get<string>('APP_URL_PREFIX') + Environment.get<string>('API_INFO_ROUTE'), (req: my.Request, res: my.Response) => {
86+
const pkg = Environment.getPkg();
87+
const links = {
88+
links: {}
89+
};
90+
if (Environment.get<string>('SWAGGER_ENABLED') === 'true') {
91+
links.links['swagger'] = `${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}${process.env.SWAGGER_ROUTE}`;
92+
}
93+
if (Environment.get<string>('MONITOR_ENABLED') === 'true') {
94+
links.links['monitor'] = `${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}${process.env.MONITOR_ROUTE}`;
95+
}
96+
return res.json({
97+
name: pkg.name,
98+
version: pkg.version,
99+
description: pkg.description,
100+
...links
101+
});
102+
});
103+
}
104+
return app;
105+
}
106+
71107
/**
72108
* Sets up the swagger documentation
73109
*
@@ -95,7 +131,22 @@ export class Bootstrap {
95131
const swaggerUi = require('swagger-ui-express');
96132
const route = Environment.get<string>('APP_URL_PREFIX') + Environment.get<string>('SWAGGER_ROUTE');
97133
app.use(route, swaggerUi.serve, swaggerUi.setup(swaggerFile));
98-
log.info(`Now you can access the swagger docs under -> ${app.get('host')}:${(app.get('port'))}${route}`);
134+
}
135+
return app;
136+
}
137+
138+
/**
139+
* Sets up a small monitor app
140+
*
141+
* @static
142+
* @param {express.Application} app
143+
* @returns {express.Application}
144+
*
145+
* @memberof Bootstrap
146+
*/
147+
static setupApiMonitor(app: express.Application): express.Application {
148+
if (Environment.get<string>('MONITOR_ENABLED') === 'true') {
149+
app.get(Environment.get<string>('APP_URL_PREFIX') + Environment.get<string>('MONITOR_ROUTE'), monitor().pageRoute);
99150
}
100151
return app;
101152
}

src/core/Environment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ export class Environment {
2020
return process.env[key];
2121
}
2222

23+
static getPkg(): any {
24+
return require('../../package.json');
25+
}
26+
2327
}

src/core/Server.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as http from 'http';
22
import * as express from 'express';
33
import { Log } from './log';
4+
import { Environment } from './Environment';
45

56
const log = new Log('core:Server');
67

@@ -39,7 +40,25 @@ export class Server {
3940
* @memberof Server
4041
*/
4142
static onStartUp(app: express.Application): void {
43+
log.info(``);
4244
log.info(`Aloha, your app is ready on ${app.get('host')}:${app.get('port')}`);
45+
log.info(`To shut it down, press <CTRL> + C at any time.`);
46+
log.info(``);
47+
log.debug('-------------------------------------------------------');
48+
log.debug(`Environment : ${Environment.getNodeEnv()}`);
49+
log.debug(`Version : ${Environment.getPkg().version}`);
50+
log.debug(``);
51+
if (Environment.get<string>('API_INFO_ENABLED') === 'true') {
52+
log.debug(`API Info : ${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}${process.env.API_INFO_ROUTE}`);
53+
}
54+
if (Environment.get<string>('SWAGGER_ENABLED') === 'true') {
55+
log.debug(`Swagger : ${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}${process.env.SWAGGER_ROUTE}`);
56+
}
57+
if (Environment.get<string>('MONITOR_ENABLED') === 'true') {
58+
log.debug(`Monitor : ${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}${process.env.MONITOR_ROUTE}`);
59+
}
60+
log.debug('-------------------------------------------------------');
61+
log.debug('');
4362
}
4463

4564
/**
@@ -62,7 +81,7 @@ export class Server {
6281
process.exit(1);
6382
break;
6483
case 'EADDRINUSE':
65-
log.error(`${this.bind(addr)} is already in use`);
84+
log.error(`Port is already in use`);
6685
process.exit(1);
6786
break;
6887
default:

0 commit comments

Comments
 (0)