@@ -28,8 +28,6 @@ export class Bootstrap {
2828 */
2929 static getApp ( ) : express . Application {
3030 const app = express ( ) ;
31-
32- // Set serve configs for running it
3331 app . set ( 'host' , Environment . get ( 'APP_HOST' ) ) ;
3432 app . set ( 'port' , Bootstrap . normalizePort ( Environment . get < string > ( 'PORT' ) || Environment . get < string > ( 'APP_PORT' ) ) ) ;
3533 log . debug ( 'app is defined' ) ;
@@ -48,11 +46,47 @@ export class Bootstrap {
4846 * @memberof Bootstrap
4947 */
5048 static build ( app : express . Application , container : Container ) : express . Application {
49+ app = Bootstrap . setupSwagger ( app ) ;
5150 let server = new InversifyExpressServer ( container , undefined , { rootPath : Environment . get < string > ( 'APP_URL_PREFIX' ) } , app ) ;
5251 log . debug ( 'ioc is bonded' ) ;
52+ server = Bootstrap . setupConfigurations ( server ) ;
53+ return server . build ( ) ;
54+ }
55+
56+ /**
57+ * Sets up the express middlewares witch are last in the chain
58+ *
59+ * @static
60+ * @param {InversifyExpressServer } server
61+ * @returns {InversifyExpressServer }
62+ *
63+ * @memberof Bootstrap
64+ */
65+ static setupConfigurations ( server : InversifyExpressServer ) : InversifyExpressServer {
5366 server . setConfig ( ( a ) => a . use ( extendExpressResponse ) ) ;
5467 server . setErrorConfig ( ( a ) => a . use ( exceptionHandler ) ) ;
55- return server . build ( ) ;
68+ return server ;
69+ }
70+
71+ /**
72+ * Sets up the swagger documentation
73+ *
74+ * @static
75+ * @param {express.Application } app
76+ * @returns {express.Application }
77+ *
78+ * @memberof Bootstrap
79+ */
80+ static setupSwagger ( app : express . Application ) : express . Application {
81+ if ( Environment . get < string > ( 'SWAGGER_ENABLED' ) === 'true' ) {
82+ const basePath = __dirname . substring ( 0 , __dirname . indexOf ( '/src/' ) + 4 ) ;
83+ const swaggerUi = require ( 'swagger-ui-express' ) ;
84+ const swaggerDocument = require ( basePath + Environment . get < string > ( 'SWAGGER_FILE' ) ) ;
85+ const route = Environment . get < string > ( 'APP_URL_PREFIX' ) + Environment . get < string > ( 'SWAGGER_ROUTE' ) ;
86+ app . use ( route , swaggerUi . serve , swaggerUi . setup ( swaggerDocument ) ) ;
87+ log . info ( `Now you can access the swagger docs under -> ${ app . get ( 'host' ) } :${ ( app . get ( 'port' ) ) } ${ route } ` ) ;
88+ }
89+ return app ;
5690 }
5791
5892 /**
0 commit comments