@lxdhub/api
Version:
Display, search and copy LXD-images using a web interface.
103 lines (87 loc) • 2.94 kB
text/typescript
import { Command, command, metadata, option, Options } from 'clime';
import { LXDHubAPI, LXDHubAPISettings } from '../../..';
import * as fs from 'fs';
import { LogType } from '@lxdhub/common';
export class StartOptions extends Options {
({
flag: 'p',
description: 'Port on which lxdhub-api should listen. Default is 3000',
})
port: number = 3000;
({
flag: 'h',
description: 'Hostname of lxdhub-api. Default is 0.0.0.0'
})
host: string = '0.0.0.0';
({
description: 'The name of the database to connect to. Default is lxdhub'
})
databaseName = 'lxdhub';
({
description: 'The host of the database to connect to. Default is localhost'
})
databaseHost: string = 'localhost';
({
description: 'The database password for the given user. Default is lxdhub'
})
databasePassword: string = 'lxdhub';
({
description: 'The database port to connect to. Default is 5432'
})
databasePort: number = 5432;
({
description: 'The database username. Default is lxdhub'
})
databaseUsername: string = 'lxdhub';
({
description: 'The LXD certificate for the remote'
})
cert: string = `${process.env.HOME}/.config/lxc/client.crt`;
({
description: 'The LXC key for the remote'
})
key: string = `${process.env.HOME}/.config/lxc/client.key`;
({
description: 'The log level'
})
logLevel: LogType = 'info';
({
description: 'The url to the swagger documentation'
})
docUrl: string = '/api/v1/doc';
({
description: 'If the image upload is enabled'
})
upload: boolean = false;
}
({
description: 'Start the lxdhub api'
})
export default class extends Command {
async execute(
options: StartOptions
) {
const apiOptions: LXDHubAPISettings = {
port: options.port ?? 3000,
hostUrl: options.host ?? '0.0.0.0',
logLevel: options.logLevel ?? 'info',
docUrl: options.docUrl ?? '/api/v1/doc',
database: {
database: options.databaseName ?? 'lxdhub',
host: options.databaseHost ?? 'localhost',
password: options.databasePassword ?? 'lxdhub',
port: options.databasePort ?? 5432,
username: options.databaseUsername ?? 'lxdhub'
},
lxd: {
// @ts-ignore
cert: fs.readFileSync(options.cert ?? `${process.env.HOME}/.config/lxc/client.crt`),
// @ts-ignore
key: fs.readFileSync(options.key ?? `${process.env.HOME}/.config/lxc/client.key`)
},
upload: options.upload ?? false
};
await new LXDHubAPI(apiOptions).run();
}
}