1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const core_1 = require("@nestjs/core");
|
13 | const swagger_1 = require("@nestjs/swagger");
|
14 | const class_transformer_1 = require("class-transformer");
|
15 | const class_validator_1 = require("class-validator");
|
16 | const app_module_1 = require("./app.module");
|
17 | const exception_1 = require("./exception");
|
18 | const log_1 = require("./log");
|
19 | const request_logger_interceptor_1 = require("./log/request-logger.interceptor");
|
20 | const Chalk = require("chalk");
|
21 | const express = require("express");
|
22 | const cors = require("cors");
|
23 | const platform_socket_io_1 = require("@nestjs/platform-socket.io");
|
24 | const platform_express_1 = require("@nestjs/platform-express");
|
25 |
|
26 |
|
27 |
|
28 | class LXDHubAPISettings {
|
29 | constructor() {
|
30 | this.port = 3000;
|
31 | this.hostUrl = '0.0.0.0';
|
32 | this.logLevel = 'silly';
|
33 | this.docUrl = '/api/v1/doc';
|
34 | }
|
35 | }
|
36 | __decorate([
|
37 | class_validator_1.IsInt(),
|
38 | class_transformer_1.Type(() => Number),
|
39 | __metadata("design:type", Number)
|
40 | ], LXDHubAPISettings.prototype, "port", void 0);
|
41 | __decorate([
|
42 | class_validator_1.IsString(),
|
43 | class_transformer_1.Type(() => String),
|
44 | __metadata("design:type", String)
|
45 | ], LXDHubAPISettings.prototype, "hostUrl", void 0);
|
46 | exports.LXDHubAPISettings = LXDHubAPISettings;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | class LXDHubAPI {
|
52 | constructor(settings, server) {
|
53 | this.settings = settings;
|
54 | this.server = server;
|
55 | this.logger = new log_1.LogService('LXDHubAPI', settings.logLevel);
|
56 | this.url = `http://${this.settings.hostUrl}:${this.settings.port}`;
|
57 | }
|
58 | |
59 |
|
60 |
|
61 | setupSwagger() {
|
62 | const options = new swagger_1.DocumentBuilder()
|
63 | .setTitle('LXDHub API')
|
64 | .setDescription('Display, search and copy LXD images using a web interface.')
|
65 | .setVersion('1.0')
|
66 | .build();
|
67 | const document = swagger_1.SwaggerModule.createDocument(this.app, options);
|
68 | swagger_1.SwaggerModule.setup(this.settings.docUrl || '/api/v1/doc', this.app, document);
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | async createNestApp() {
|
74 | const nestSettings = { logger: this.logger };
|
75 | if (!this.server) {
|
76 | this.server = express();
|
77 | }
|
78 | this.app = await core_1.NestFactory.create(app_module_1.AppModule.forRoot(this.settings), new platform_express_1.ExpressAdapter(this.server), nestSettings);
|
79 | this.app.useWebSocketAdapter(new platform_socket_io_1.IoAdapter(this.app.getHttpServer()));
|
80 | }
|
81 | |
82 |
|
83 |
|
84 | setupMiddleware() {
|
85 |
|
86 |
|
87 | this.app.useGlobalFilters(new exception_1.HttpExceptionFilter());
|
88 |
|
89 | this.app.useGlobalInterceptors(new request_logger_interceptor_1.RequestLoggerInterceptor());
|
90 |
|
91 | if (process.env.NODE_ENV !== 'production') {
|
92 | this.app.use(cors({
|
93 | origin: true,
|
94 | credentials: true
|
95 | }));
|
96 | }
|
97 | this.app.use((req, res, next) => {
|
98 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, DEVICE_ID, SSO_TOKEN');
|
99 | next();
|
100 | });
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 | async bootstrap() {
|
107 | await this.createNestApp();
|
108 | this.setupSwagger();
|
109 | this.setupMiddleware();
|
110 | return this.server;
|
111 | }
|
112 | |
113 |
|
114 |
|
115 | async run() {
|
116 | this.logger.log('Bootstraping application');
|
117 | try {
|
118 | await this.bootstrap();
|
119 | }
|
120 | catch (err) {
|
121 | err = err;
|
122 | this.logger.error(`An error occured while bootstraping the application`);
|
123 | this.logger.error(err.message);
|
124 | }
|
125 |
|
126 | await this.app.listen(this.settings.port, this.settings.hostUrl);
|
127 | this.logger.log(`Open on ${Chalk.default.blue(this.url)}`);
|
128 | }
|
129 | }
|
130 | exports.LXDHubAPI = LXDHubAPI;
|
131 |
|
\ | No newline at end of file |