From 50598010bdaa7b2928b5b32ce2b5d55ffa63592d Mon Sep 17 00:00:00 2001 From: maslow Date: Wed, 14 Dec 2022 20:45:16 +0800 Subject: [PATCH] refactor(server): add log module --- deploy/build/images/shim/RuntimeImageList | 2 + deploy/scripts/install-on-linux.sh | 2 +- server/.env | 2 +- server/src/app.module.ts | 2 + server/src/function/function.controller.ts | 36 --------- server/src/log/log.controller.ts | 87 ++++++++++++++++++++++ server/src/log/log.module.ts | 13 ++++ 7 files changed, 106 insertions(+), 38 deletions(-) create mode 100644 deploy/build/images/shim/RuntimeImageList create mode 100644 server/src/log/log.controller.ts create mode 100644 server/src/log/log.module.ts diff --git a/deploy/build/images/shim/RuntimeImageList b/deploy/build/images/shim/RuntimeImageList new file mode 100644 index 0000000000..8f6a159f8c --- /dev/null +++ b/deploy/build/images/shim/RuntimeImageList @@ -0,0 +1,2 @@ +docker.io/lafyun/runtime-node-init:latest +docker.io/lafyun/runtime-node:latest \ No newline at end of file diff --git a/deploy/scripts/install-on-linux.sh b/deploy/scripts/install-on-linux.sh index 7e422b83a6..afb3c1e56a 100644 --- a/deploy/scripts/install-on-linux.sh +++ b/deploy/scripts/install-on-linux.sh @@ -41,7 +41,7 @@ fi # install k8s cluster -sealos run labring/kubernetes:v1.24.0 labring/flannel:v0.19.0 --single +sealos run labring/kubernetes:v1.24.0-4.1.1 labring/flannel:v0.19.0 --single # taint master node NODENAME=$(kubectl get nodes -ojsonpath='{.items[0].metadata.name}') diff --git a/server/.env b/server/.env index ed5ea8215a..bc928e7eaf 100644 --- a/server/.env +++ b/server/.env @@ -1,6 +1,6 @@ # database -DATABASE_URL=mongodb://admin:passw0rd@mongo.laf-system.svc.cluster.local:27017/sys_db?authSource=admin&replicaSet=rs0&w=majority +DATABASE_URL=mongodb://admin:passw0rd@127.0.0.1:27017/sys_db?authSource=admin&replicaSet=rs0&w=majority&directConnection=true # jwt settings JWT_SECRET=laf_server_abc123 diff --git a/server/src/app.module.ts b/server/src/app.module.ts index c52d730361..6dfcabb140 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -14,6 +14,7 @@ import { ScheduleModule } from '@nestjs/schedule' import { DatabaseModule } from './database/database.module' import { PrismaService } from './prisma.service' import { StorageModule } from './storage/storage.module' +import { LogModule } from './log/log.module'; @Module({ imports: [ @@ -32,6 +33,7 @@ import { StorageModule } from './storage/storage.module' InstanceModule, DatabaseModule, StorageModule, + LogModule, ], controllers: [AppController], providers: [AppService, PrismaService], diff --git a/server/src/function/function.controller.ts b/server/src/function/function.controller.ts index 43435f24db..aa1283f1f6 100644 --- a/server/src/function/function.controller.ts +++ b/server/src/function/function.controller.ts @@ -10,7 +10,6 @@ import { HttpException, HttpStatus, Req, - Query, } from '@nestjs/common' import { CreateFunctionDto } from './dto/create-function.dto' import { UpdateFunctionDto } from './dto/update-function.dto' @@ -164,39 +163,4 @@ export class FunctionController { const res = this.functionsService.compile(func) return res } - - /** - * Get function logs - * @param appid - * @param name - * @returns - */ - @ApiResponse({ type: ResponseUtil }) - @ApiOperation({ summary: 'Get function logs' }) - @UseGuards(JwtAuthGuard, ApplicationAuthGuard) - @Get('logs') - async getLogs( - @Param('appid') appid: string, - @Query('requestId') requestId?: string, - @Query('functionName') functionName?: string, - @Query('limit') limit?: number, - @Query('page') page?: number, - ) { - page = page || 1 - limit = limit || 10 - - const res = await this.functionsService.findLogs(appid, { - requestId, - functionName, - limit, - page, - }) - - return ResponseUtil.ok({ - list: res.data, - total: res.total, - page, - limit, - }) - } } diff --git a/server/src/log/log.controller.ts b/server/src/log/log.controller.ts new file mode 100644 index 0000000000..8de5eaff68 --- /dev/null +++ b/server/src/log/log.controller.ts @@ -0,0 +1,87 @@ +import { + Controller, + Get, + Logger, + Param, + Query, + UseGuards, +} from '@nestjs/common' +import { + ApiBearerAuth, + ApiOperation, + ApiQuery, + ApiResponse, + ApiTags, +} from '@nestjs/swagger' +import { ApplicationAuthGuard } from '../auth/application.auth.guard' +import { JwtAuthGuard } from '../auth/jwt.auth.guard' +import { FunctionService } from '../function/function.service' +import { ResponseUtil } from '../utils/response' + +@ApiBearerAuth('Authorization') +@Controller('apps/:appid/logs') +export class LogController { + private readonly logger = new Logger(LogController.name) + + constructor(private readonly funcService: FunctionService) {} + + /** + * Get function logs + * @param appid + * @param name + * @returns + */ + @ApiTags('Function') + @ApiResponse({ type: ResponseUtil }) + @ApiOperation({ summary: 'Get function logs' }) + @UseGuards(JwtAuthGuard, ApplicationAuthGuard) + @ApiQuery({ + name: 'functionName', + type: String, + description: 'The function name. Optional', + required: false, + }) + @ApiQuery({ + name: 'requestId', + type: String, + description: 'The request id. Optional', + required: false, + }) + @ApiQuery({ + name: 'page', + type: String, + description: 'The page number, default is 1', + required: false, + }) + @ApiQuery({ + name: 'limit', + type: String, + description: 'The limit number, default is 10', + required: false, + }) + @Get('functions') + async getLogs( + @Param('appid') appid: string, + @Query('requestId') requestId?: string, + @Query('functionName') functionName?: string, + @Query('limit') limit?: number, + @Query('page') page?: number, + ) { + page = page || 1 + limit = limit || 10 + + const res = await this.funcService.findLogs(appid, { + requestId, + functionName, + limit, + page, + }) + + return ResponseUtil.ok({ + list: res.data, + total: res.total, + page, + limit, + }) + } +} diff --git a/server/src/log/log.module.ts b/server/src/log/log.module.ts new file mode 100644 index 0000000000..7ee9ecbbe4 --- /dev/null +++ b/server/src/log/log.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common' +import { JwtService } from '@nestjs/jwt' +import { ApplicationModule } from '../application/application.module' +import { FunctionService } from '../function/function.service' +import { PrismaService } from '../prisma.service' +import { LogController } from './log.controller' + +@Module({ + imports: [ApplicationModule], + controllers: [LogController], + providers: [FunctionService, PrismaService, JwtService], +}) +export class LogModule {}