Skip to content

Commit

Permalink
return API info
Browse files Browse the repository at this point in the history
  • Loading branch information
dancespiele committed Jun 13, 2022
1 parent bdf6f54 commit cf0ebfe
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RouterModule } from 'nest-router';
import { routes } from './routes';
import { ConfigModule } from './shared/config/config.module';
import { LoggerModule } from './shared/logger/logger.module';
import { InfoModule } from './info/info.module';
import { BookmarkModule } from './bookmarks/bookmark.module';
import { PermissionModule } from './permissions/permission.module';
import { AssetModule } from './assets/asset.module';
Expand All @@ -16,6 +17,7 @@ import { AuthModule } from './auth/auth.module';
AssetModule,
LoggerModule,
ConfigModule,
InfoModule,
BookmarkModule,
UserProfileModule,
AuthModule,
Expand Down
27 changes: 27 additions & 0 deletions src/info/dto/get-info.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsUrl } from 'class-validator';

export class GetInfoDto {
@ApiProperty({
example: '7.17',
description: 'Elasticsearch version',
})
@IsString()
elasticsearchVersion: string;

@ApiProperty({
example: '1.0.4',
description: 'Matching API Version',
})
@IsString()
APIversion: string;

@ApiProperty({
example: '1.0.4',
description: 'Matching API Version',
})
@IsUrl({
require_tld: false,
})
docs: string;
}
42 changes: 42 additions & 0 deletions src/info/info.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Get, Req, Controller } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { readFileSync } from 'fs';
import path from 'path';
import { ElasticService } from '../shared/elasticsearch/elastic.service';
import { Public } from '../common/decorators/auth.decorator';
import { Request } from '../common/helpers/request.interface';
import { GetInfoDto } from './dto/get-info.dto';

@ApiTags('Info')
@Controller()
export class InfoController {
constructor(private readonly elasticService: ElasticService) {}

@Get()
@ApiOperation({
description: 'Get API info',
summary: 'Public',
})
@ApiResponse({
status: 200,
description: 'Return API Info',
})
@Public()
async getInfo(@Req() req: Request<unknown>): Promise<GetInfoDto> {
const pathEndpoint = `${req.protocol}://${req.hostname}${req.client.localPort ? `:${req.client.localPort}` : ''}${
req.url
}`;
const packageJsonPath = path.join(__dirname, '../..', 'package.json');
const packageJsonString = readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonString) as { version: string };

const elsInfo = await this.elasticService.getInfo();

return {
APIversion: packageJson.version,
// prettier-ignore
elasticsearchVersion: (elsInfo.body as { version: { "number": string } }).version.number,
docs: `${pathEndpoint}api/v1/docs`,
};
}
}
11 changes: 11 additions & 0 deletions src/info/info.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ElasticModule } from '../shared/elasticsearch/elastic.module';
import { InfoController } from './info.controller';

@Module({
imports: [ElasticModule],
providers: [],
controllers: [InfoController],
exports: [],
})
export class InfoModule {}
2 changes: 2 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { BookmarkModule } from './bookmarks/bookmark.module';
import { AssetModule } from './assets/asset.module';
import { UserProfileModule } from './user-profiles/user-profile.module';
import { PermissionModule } from './permissions/permission.module';
import { InfoModule } from './info/info.module';

export const routes: Routes = [
{ path: '/api/v1/ugc/bookmarks', module: BookmarkModule },
{ path: '/api/v1/metadata/assets', module: AssetModule },
{ path: '/api/v1/metadata/profiles', module: UserProfileModule },
{ path: '/api/v1/auth', module: AuthModule },
{ path: '/api/v1/permissions', module: PermissionModule },
{ path: '/', module: InfoModule },
];
4 changes: 4 additions & 0 deletions src/shared/elasticsearch/elastic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ export class ElasticService {
index,
});
}

async getInfo() {
return this.elasticsearchService.info();
}
}

0 comments on commit cf0ebfe

Please sign in to comment.