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(gcp-functions): Improved function runner #128

Merged
merged 5 commits into from
Jun 28, 2023
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
2 changes: 0 additions & 2 deletions actions/run-many/src/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { resolve } from 'path'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs/yargs'

import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json'

import { buildCommand } from './utils/build-command'
import { execCommand } from './utils/exec'
import { runTarget } from './utils/run-target'
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-runner/src/executors/run/run.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function endToEndRunner(
try {
if (runner === 'cypress') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cypressExecutor = require('@nrwl/cypress/src/executors/cypress/cypress.impl').default
const cypressExecutor = require('@nx/cypress/src/executors/cypress/cypress.impl').default

success = (await cypressExecutor(rest, context)).success
} else if (runner === 'playwright') {
Expand Down
2 changes: 2 additions & 0 deletions packages/e2e-runner/src/executors/run/utils/nx-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ function launchProcess(
cwd: process.cwd(),
env: {
...process.env,
// Make sure NODE_ENV is set to test
NODE_ENV: 'test',
...options.env
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/gcp-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"license": "MIT",
"main": "src/index.js",
"builders": "./executors.json",
"generators": "./generators.json"
"generators": "./generators.json",
"devDependencies": {
"@nestjs/common": "^10.0.3",
"@nestjs/core": "^10.0.3",
"@nestjs/platform-express": "^10.0.3"
}
}
1 change: 0 additions & 1 deletion packages/gcp-functions/runner/.gitignore

This file was deleted.

115 changes: 115 additions & 0 deletions packages/gcp-functions/runner/__runner.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Controller, Delete, Get, Logger, Options, Post, Req, Res } from '@nestjs/common'
import { Request, Response } from 'express'

import type { NxEndpoints } from './'

export function createController(gcpFunctions: NxEndpoints) {
@Controller()
class FunctionController {

readonly logger = new Logger('Runner')
readonly endpoints = gcpFunctions

constructor() {
for (const { endpoint, trigger } of this.endpoints) {
this.logger.debug(`Register ${trigger} -> "${endpoint}"`)
}
}

@Get('/*')
public async get(@Req() req, @Res() res): Promise<unknown> {
this.logger.log(`[GET] Handle "${req.path}"`)

return this.executeFunction(req, res)
}

@Post('/*')
public async post(@Req() req, @Res() res): Promise<unknown> {
this.logger.log(`[POST] Handle "${req.path}"`)

return this.executeFunction(req, res)
}

@Options('/*')
public option(@Req() req, @Res() res): Promise<unknown> {
this.logger.log(`[OPTIONS] Handle "${req.path}"`)

return this.executeFunction(req, res)
}

@Delete('/*')
public delete(@Req() req, @Res() res): Promise<unknown> {
this.logger.log(`[DELETE] Handle "${req.path}"`)

return this.executeFunction(req, res)
}

public async executeFunction(req: Request, res: Response): Promise<void> {
const endpoint = this.endpoints.find(({ endpoint }) => (
req.path.startsWith(endpoint)
))

if (endpoint) {
if (endpoint.trigger === 'http') {
return endpoint.func(req, res)

} else if (endpoint.trigger === 'topic') {
return this.simulatePubSubEvent(req, res, endpoint.func)
}

this.logger.warn(`"${req.path}" unsupported trigger!`)

} else {
this.logger.warn(`"${req.path}" not found!`)
}
res
.status(404)
.send('Function not found!')
}

public async simulatePubSubEvent(req: Request, res: Response, pubSub): Promise<void> {
if (
req.method === 'POST'
&& req.headers['content-type'].includes('application/json')
&& Object.getPrototypeOf(req.body) === Object.prototype
) {
try {
let message = { attributes: null, data: null, timestamp: new Date() }

if (req.body.message) {
message = req.body.message
}

if (req.body.data) {
message.data = Buffer.from(
Object.getPrototypeOf(req.body.data) === Object.prototype
? JSON.stringify(req.body.data)
: req.body.data,
'binary'
).toString('base64')
}

const response = await pubSub(message)

if (response) {
res.send(response)

} else {
res.send('ok')
}

} catch (err) {
console.error(err)

res.status(500)
.send(err)
}
}

res.status(405)
.send()
}
}

return FunctionController
}
28 changes: 28 additions & 0 deletions packages/gcp-functions/runner/__runner.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Module } from '@nestjs/common'

import type { NxEndpoints } from './'

import { createController } from './__runner.controller'

export function createRunnerModule(gcpFunctions: NxEndpoints) {
// Always add health endpoint
gcpFunctions.push({
endpoint: '/_check/health',
trigger: 'http',
func: (req, res) => res
.status(200)
.send()
})

@Module({
imports: [],
controllers: [
createController(gcpFunctions)
],
providers: []
})
class RunnerModule {
}

return RunnerModule
}
53 changes: 53 additions & 0 deletions packages/gcp-functions/runner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Logger } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { ExpressAdapter } from '@nestjs/platform-express'
import { FsTree } from 'nx/src/generators/tree'
import { getProjects } from 'nx/src/generators/utils/project-configuration'
import { workspaceRoot } from 'nx/src/utils/app-root'

import type { HttpFunction } from '@google-cloud/functions-framework'
import type { DeployExecutorSchema } from '@nx-extend/gcp-functions/src/executors/deploy/deploy.impl'

import { createRunnerModule } from './__runner.module'

export type NxEndpoint = {
endpoint: string
func: HttpFunction
} & Pick<DeployExecutorSchema, 'trigger'>

export type RunnerFunctionsMap = Map<string, Promise<any>>
export type NxEndpoints = NxEndpoint[]

export interface RunnerOptions {
port?: number
}

export async function bootstrapRunner(basicFunctionsMap: RunnerFunctionsMap, options: RunnerOptions = {}) {
const nxTree = new FsTree(workspaceRoot, false)
const projects = getProjects(nxTree)

const nxEndpoints = [] as NxEndpoint[]
for (const [projectName, module] of basicFunctionsMap) {
const project = projects.get(projectName)

if (!project || project?.targets?.['deploy']?.executor !== '@nx-extend/gcp-functions:deploy' || !project?.targets?.['deploy']?.options) {
continue
}

const options: DeployExecutorSchema = project.targets['deploy'].options

nxEndpoints.push({
endpoint: `/${options.functionName || projectName}`,
trigger: options.trigger || 'http',
func: (await module)[options.entryPoint] as HttpFunction
})
}

const app = await NestFactory.create(createRunnerModule(nxEndpoints), new ExpressAdapter(), {
rawBody: true
})

await app.listen(options.port || 8080, '0.0.0.0').then(() => {
Logger.log('Functions running on http://localhost:8080')
})
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { NestFactory } from '@nestjs/core'
import { Logger } from '@nestjs/common'
import { ExpressAdapter } from '@nestjs/platform-express'
import { RunnerModule } from './__runner.module'
import { bootstrapRunner } from '@nx-extend/gcp-functions/runner'

async function bootstrap() {
const app = await NestFactory.create(RunnerModule, new ExpressAdapter())

await app.listen(8080, '0.0.0.0').then(() => {
Logger.log(`Functions running on http://localhost:8080}`)
})
}

bootstrap()
/* eslint-disable @nx/enforce-module-boundaries */
bootstrapRunner(new Map([
['nx function project name', import('path to main of of project')]
])
)
Loading
Loading