Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): implement cron trigger schema & crud apis #578

Merged
merged 1 commit into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ghaction",
"gonanoid",
"healthz",
"hokify",
"hostpath",
"Kube",
"kubebuilder",
Expand Down
12 changes: 9 additions & 3 deletions runtimes/nodejs/src/handler/invoke-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { logger } from '../support/logger'
import { CloudFunction } from '../support/function-engine'
import { IRequest } from '../support/types'
import { handleDebugFunction } from './debug-func'
import { parseToken } from '../support/token'

const DEFAULT_FUNCTION_NAME = '__default__'

Expand All @@ -21,7 +22,12 @@ export async function handleInvokeFunction(req: IRequest, res: Response) {
if (req.get('x-laf-debug-token')) {
return await handleDebugFunction(req, res)
}


let isTrigger = false
if (parseToken(req.get('x-laf-trigger-token'))) {
isTrigger = true
}

const requestId = req.requestId
const func_name = req.params?.name

Expand All @@ -38,7 +44,7 @@ export async function handleInvokeFunction(req: IRequest, res: Response) {
const func = new CloudFunction(funcData)

// reject while no HTTP enabled
if (!func.methods.includes(req.method.toUpperCase())) {
if (!func.methods.includes(req.method.toUpperCase()) && !isTrigger) {
return res.status(405).send('Method Not Allowed')
}

Expand All @@ -49,7 +55,7 @@ export async function handleInvokeFunction(req: IRequest, res: Response) {
files: req.files as any,
body: req.body,
headers: req.headers,
method: req.method,
method: isTrigger ? 'trigger' : req.method,
auth: req['auth'],
user: req.user,
requestId,
Expand Down
4 changes: 3 additions & 1 deletion runtimes/nodejs/src/support/function-engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export interface FunctionContext {
query?: any
body?: any
params?: any
// @Deprecated use user instead
/**
* @deprecated use user instead
*/
auth?: any
user?: any
requestId: string
Expand Down
157 changes: 151 additions & 6 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"@aws-sdk/client-sts": "^3.226.0",
"@hokify/agenda": "^6.3.0",
"@kubernetes/client-node": "^0.17.1",
"@nestjs/axios": "^1.0.0",
"@nestjs/common": "^9.0.0",
Expand Down
14 changes: 14 additions & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,19 @@ model CloudFunction {
updatedAt DateTime @updatedAt
createdBy String @db.ObjectId

cronTriggers CronTrigger[]

@@unique([appid, name])
}

model CronTrigger {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String
desc String
cron String
target String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

cloudFunction CloudFunction @relation(fields: [appid, target], references: [appid, name])
}
2 changes: 2 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PrismaService } from './prisma.service'
import { StorageModule } from './storage/storage.module'
import { LogModule } from './log/log.module'
import { DependencyModule } from './dependency/dependency.module'
import { TriggerModule } from './trigger/trigger.module';

@Module({
imports: [
Expand All @@ -36,6 +37,7 @@ import { DependencyModule } from './dependency/dependency.module'
StorageModule,
LogModule,
DependencyModule,
TriggerModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
Expand Down
7 changes: 7 additions & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ dotenv.config({ path: '.env.local' })
dotenv.config()

export class ServerConfig {
static get DATABASE_URL() {
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL is not defined')
}
return process.env.DATABASE_URL
}

static get JWT_SECRET() {
if (!process.env.JWT_SECRET) {
throw new Error('JWT_SECRET is not defined')
Expand Down
Loading