fix: serverless update

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-03-23 11:53:51 +08:00
parent 0a19970bac
commit 4776de6bbe
4 changed files with 40 additions and 31 deletions

View File

@@ -37,4 +37,5 @@ export enum CacheKeys {
RSSXml = `${API_CACHE_PREFIX}rss_xml`,
}
export const SERVERLESS_COMPLIE_CACHE_TTL = 60 * 10 // 10 分钟
// 30 days
export const SERVERLESS_COMPLIE_CACHE_TTL = 60 * 60 * 24 * 30

View File

@@ -88,7 +88,14 @@ export class ServerlessController {
name,
reference,
type: SnippetType.Function,
method: requestMethod,
$or: [
{
method: 'ALL',
},
{
method: requestMethod,
},
],
})
.select('+secret')
.lean({
@@ -101,7 +108,7 @@ export class ServerlessController {
throw new NotFoundException(notExistMessage)
}
if (snippet.method !== requestMethod || !snippet.enable) {
if (!snippet.enable) {
throw new NotFoundException(notExistMessage)
}

View File

@@ -100,30 +100,27 @@ export class ServerlessService implements OnModuleInit {
return this.snippetModel
}
private mockStorageCache() {
return {
get: async (key: string) => {
const client = this.cacheService.getClient()
return client
.get(getRedisKey(RedisKeys.ServerlessStorage, key))
.then((string) => {
if (!string) return null
return JSON.safeParse(string)
})
},
set: async (key: string, value: object | string, ttl?: string) => {
const client = this.cacheService.getClient()
const cacheKey = getRedisKey(RedisKeys.ServerlessStorage, key)
await client.set(cacheKey, JSON.stringify(value))
await client.expire(cacheKey, ttl || 60 * 60 * 24 * 7)
},
del: async (key: string) => {
const client = this.cacheService.getClient()
return client.hdel(getRedisKey(RedisKeys.ServerlessStorage), key)
},
} as const
}
private mockStorageCache = Object.freeze({
get: async (key: string) => {
const client = this.cacheService.getClient()
return client
.get(getRedisKey(RedisKeys.ServerlessStorage, key))
.then((string) => {
if (!string) return null
return JSON.safeParse(string)
})
},
set: async (key: string, value: object | string, ttl?: string) => {
const client = this.cacheService.getClient()
const cacheKey = getRedisKey(RedisKeys.ServerlessStorage, key)
await client.set(cacheKey, JSON.stringify(value))
await client.expire(cacheKey, ttl || 60 * 60 * 24 * 7)
},
del: async (key: string) => {
const client = this.cacheService.getClient()
return client.hdel(getRedisKey(RedisKeys.ServerlessStorage), key)
},
})
private async mockGetMaster() {
const collection = this.databaseService.db.collection('users')
const cur = collection.aggregate([
@@ -460,7 +457,10 @@ export class ServerlessService implements OnModuleInit {
private async createScopeContext(
scope: string,
context: any,
context: {
req: FunctionContextRequest
res: FunctionContextResponse
},
model: SnippetModel,
logger: Logger,
) {
@@ -480,7 +480,8 @@ export class ServerlessService implements OnModuleInit {
query: context.req.query,
headers: context.req.headers,
// TODO wildcard params
params: { ...context.req.params },
params: context.req.params || {},
method: context.req.method,
secret: secretObj,
@@ -507,7 +508,7 @@ export class ServerlessService implements OnModuleInit {
...requestContext,
storage: {
cache: this.mockStorageCache(),
cache: this.mockStorageCache,
db: this.mockDb(
`${model.reference || '#########debug######'}@${model.name}`,
),

View File

@@ -89,7 +89,7 @@ export class SnippetModel extends BaseModel {
// for function start
@prop()
@IsEnum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
@IsEnum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'ALL'])
@IsOptional()
method?: string