@lxdhub/api
Version:
Display, search and copy LXD-images using a web interface.
75 lines (68 loc) • 2.21 kB
text/typescript
import { APIDto } from './api.dto';
import { Controller, Get, HttpCode, Inject, Req } from '@nestjs/common';
import { ApiResponse, ApiOperation } from '@nestjs/swagger';
import { Request } from 'express';
import { LXDHubAPISettings } from '.';
('/api/v1')
/**
* The root api controller
*/
export class AppController {
constructor(
('Fs')
private Fs,
('Path')
private Path,
('LXDHubAPISettings')
private appSettings: LXDHubAPISettings
) { }
/**
* Reads the package.json file and returns the parsed object
*/
private async getPackageJson() {
const packageJsonPath = this.Path.join(__dirname, '../package.json');
// @ts-ignore
const packageJson = await this.Fs.readFile(packageJsonPath, 'utf8');
return JSON.parse(packageJson);
}
/**
* Generates the docurl using the express request object
* and the given relative doc url
* @param req The express request
*/
private getDocsUrl(req: Request): string | null {
if (this.appSettings.docUrl) {
return `${req.protocol}://${req.get('host')}${this.appSettings.docUrl}`;
} else {
return null;
}
}
/**
* Returns general informations about the API.
* @param req The express request object
*/
({ title: 'API Info' })
({ status: 200, type: APIDto })
('/')
async apiInfo(() req: Request): Promise<APIDto> {
let packageJson;
try {
packageJson = await this.getPackageJson();
}
catch (ex) {
throw HttpCode(500);
}
return {
api_version: '1.0',
package_version: packageJson.version,
name: packageJson.name,
description: packageJson.description,
_links: {
homepage: packageJson.homepage,
bug_report: packageJson.bugs && packageJson.bugs.url || null,
repository: packageJson.repository && packageJson.repository.url || null,
docs: this.getDocsUrl(req),
}
};
}
}