UNPKG

5.17 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");
21/**
22 * The LXDHub API settings
23 */
24class LXDHubAPISettings {
25 constructor() {
26 this.port = 3000;
27 this.hostUrl = '0.0.0.0';
28 this.logLevel = 'silly';
29 this.docUrl = '/api/v1/doc';
30 }
31}
32__decorate([
33 class_validator_1.IsInt(),
34 class_transformer_1.Type(() => Number),
35 __metadata("design:type", Number)
36], LXDHubAPISettings.prototype, "port", void 0);
37__decorate([
38 class_validator_1.IsString(),
39 class_transformer_1.Type(() => String),
40 __metadata("design:type", String)
41], LXDHubAPISettings.prototype, "hostUrl", void 0);
42exports.LXDHubAPISettings = LXDHubAPISettings;
43/**
44 * The LXDHub API is the interface for the
45 * LXDHub Web user interface.
46 */
47class LXDHubAPI {
48 constructor(settings, server) {
49 this.settings = settings;
50 this.server = server;
51 this.logger = new log_1.LogService('LXDHubAPI', settings.logLevel);
52 this.url = `http://${this.settings.hostUrl}:${this.settings.port}`;
53 }
54 /**
55 * Conigurates Swagger for Nest
56 */
57 setupSwagger() {
58 const options = new swagger_1.DocumentBuilder()
59 .setTitle('LXDHub API')
60 .setDescription('Display, search and copy LXD images using a web interface.')
61 .setVersion('1.0')
62 .build();
63 const document = swagger_1.SwaggerModule.createDocument(this.app, options);
64 swagger_1.SwaggerModule.setup(this.settings.docUrl || '/api/v1/doc', this.app, document);
65 }
66 /**
67 * Creates the Nest App
68 */
69 async createNestApp() {
70 // const appModule = AppModule.forRoot(this.settings);
71 // const nestSettings = { logger: this.logger };
72 // if (!this.server) {
73 // this.server = express();
74 // }
75 // this.app = await NestFactory.create(appModule, nestSettings);
76 this.app = await core_1.NestFactory.create(app_module_1.AppModule.forRoot(this.settings), {
77 logger: this.logger
78 });
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-Origin', 'http://127.0.0.1:8080');
99 res.header('Access-Control-Allow-Credentials', 'true');
100 res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, DEVICE_ID, SSO_TOKEN');
101 next();
102 });
103 }
104 /**
105 * Bootstraps the LXDHub API and returns the
106 * Express instance
107 */
108 async bootstrap() {
109 await this.createNestApp();
110 this.setupSwagger();
111 this.setupMiddleware();
112 return this.server;
113 }
114 /**
115 * Bootstraps & starts LXDHub API with the given conifgurations
116 */
117 async run() {
118 this.logger.log('Bootstraping application');
119 try {
120 await this.bootstrap();
121 }
122 catch (err) {
123 err = err;
124 this.logger.error(`An error occured while bootstraping the application`);
125 this.logger.error(err.message);
126 }
127 // Starts listening on the given port and host url
128 await this.app.listen(this.settings.port, this.settings.hostUrl);
129 this.logger.log(`Open on ${Chalk.default.blue(this.url)}`);
130 }
131}
132exports.LXDHubAPI = LXDHubAPI;
133//# sourceMappingURL=main.js.map
\No newline at end of file