UNPKG

5.04 kBJavaScriptView Raw
1"use strict";
2var __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};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const core_1 = require("@nestjs/core");
13const swagger_1 = require("@nestjs/swagger");
14const class_transformer_1 = require("class-transformer");
15const class_validator_1 = require("class-validator");
16const app_module_1 = require("./app.module");
17const exception_1 = require("./exception");
18const log_1 = require("./log");
19const request_logger_interceptor_1 = require("./log/request-logger.interceptor");
20const Chalk = require("chalk");
21const express = require("express");
22const cors = require("cors");
23const websockets_1 = require("@nestjs/websockets");
24/**
25 * The LXDHub API settings
26 */
27class LXDHubAPISettings {
28 constructor() {
29 this.port = 3000;
30 this.hostUrl = '0.0.0.0';
31 this.logLevel = 'silly';
32 this.docUrl = '/api/v1/doc';
33 }
34}
35__decorate([
36 class_validator_1.IsInt(),
37 class_transformer_1.Type(() => Number),
38 __metadata("design:type", Number)
39], LXDHubAPISettings.prototype, "port", void 0);
40__decorate([
41 class_validator_1.IsString(),
42 class_transformer_1.Type(() => String),
43 __metadata("design:type", String)
44], LXDHubAPISettings.prototype, "hostUrl", void 0);
45exports.LXDHubAPISettings = LXDHubAPISettings;
46/**
47 * The LXDHub API is the interface for the
48 * LXDHub Web user interface.
49 */
50class LXDHubAPI {
51 constructor(settings, server) {
52 this.settings = settings;
53 this.server = server;
54 this.logger = new log_1.LogService('LXDHubAPI', settings.logLevel);
55 this.url = `http://${this.settings.hostUrl}:${this.settings.port}`;
56 }
57 /**
58 * Conigurates Swagger for Nest
59 */
60 setupSwagger() {
61 const options = new swagger_1.DocumentBuilder()
62 .setTitle('LXDHub API')
63 .setDescription('Display, search and copy LXD images using a web interface.')
64 .setVersion('1.0')
65 .build();
66 const document = swagger_1.SwaggerModule.createDocument(this.app, options);
67 swagger_1.SwaggerModule.setup(this.settings.docUrl || '/api/v1/doc', this.app, document);
68 }
69 /**
70 * Creates the Nest App
71 */
72 async createNestApp() {
73 const nestSettings = { logger: this.logger };
74 if (!this.server) {
75 this.server = express();
76 }
77 this.app = await core_1.NestFactory.create(app_module_1.AppModule.forRoot(this.settings), this.server, nestSettings);
78 this.app.useWebSocketAdapter(new websockets_1.IoAdapter(this.app.getHttpServer()));
79 }
80 /**
81 * Setup the middleware for LXDHub API
82 */
83 setupMiddleware() {
84 // this.app.setGlobalPrefix('/api/v1');
85 // Global execution handler
86 this.app.useGlobalFilters(new exception_1.HttpExceptionFilter());
87 // Global request logger
88 // @ts-ignore
89 this.app.useGlobalInterceptors(new request_logger_interceptor_1.RequestLoggerInterceptor());
90 // In development, allow any origin to access the website
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 * Bootstraps the LXDHub API and returns the
104 * Express instance
105 */
106 async bootstrap() {
107 await this.createNestApp();
108 this.setupSwagger();
109 this.setupMiddleware();
110 return this.server;
111 }
112 /**
113 * Bootstraps & starts LXDHub API with the given conifgurations
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 // Starts listening on the given port and host url
126 await this.app.listen(this.settings.port, this.settings.hostUrl);
127 this.logger.log(`Open on ${Chalk.default.blue(this.url)}`);
128 }
129}
130exports.LXDHubAPI = LXDHubAPI;
131//# sourceMappingURL=main.js.map
\No newline at end of file