Skip to content

Commit

Permalink
refactor(server): add log module (labring#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored and LeezQ committed Dec 14, 2022
1 parent acd7fee commit 7fc06d2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 38 deletions.
2 changes: 2 additions & 0 deletions deploy/build/images/shim/RuntimeImageList
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker.io/lafyun/runtime-node-init:latest
docker.io/lafyun/runtime-node:latest
2 changes: 1 addition & 1 deletion deploy/scripts/install-on-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down
2 changes: 1 addition & 1 deletion server/.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -32,6 +33,7 @@ import { StorageModule } from './storage/storage.module'
InstanceModule,
DatabaseModule,
StorageModule,
LogModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
Expand Down
36 changes: 0 additions & 36 deletions server/src/function/function.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
})
}
}
87 changes: 87 additions & 0 deletions server/src/log/log.controller.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
}
13 changes: 13 additions & 0 deletions server/src/log/log.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 7fc06d2

Please sign in to comment.