Skip to content

Commit 2337649

Browse files
authored
Merge pull request #257 from AthennaIO/develop
feat(http): add option to run server for AWS Lambda
2 parents b3726f5 + e065f48 commit 2337649

File tree

7 files changed

+59
-23
lines changed

7 files changed

+59
-23
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/core",
3-
"version": "5.21.0",
3+
"version": "5.22.0",
44
"description": "One foundation for multiple applications.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",
@@ -85,7 +85,7 @@
8585
"@athenna/common": "^5.14.0",
8686
"@athenna/config": "^5.4.0",
8787
"@athenna/cron": "^5.9.0",
88-
"@athenna/http": "^5.34.0",
88+
"@athenna/http": "^5.38.0",
8989
"@athenna/ioc": "^5.2.0",
9090
"@athenna/logger": "^5.8.0",
9191
"@athenna/test": "^5.5.0",

src/applications/Http.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ export class Http {
1818
* Only initialize the server without booting it.
1919
*/
2020
public static async init(options?: HttpOptions): Promise<ServerImpl> {
21-
options = Options.create(options, {
22-
initOnly: false,
23-
host: Config.get('http.host', '127.0.0.1'),
24-
port: Config.get('http.port', 3000),
25-
routePath: Config.get('rc.http.route', Path.routes(`http.${Path.ext()}`)),
26-
kernelPath: Config.get(
27-
'rc.http.kernel',
28-
'@athenna/http/kernels/HttpKernel'
29-
)
30-
})
31-
3221
const server = ioc.safeUse('Athenna/Core/HttpServer')
3322

3423
debug('booting http application with options %o', options)
@@ -46,12 +35,28 @@ export class Http {
4635
* Boot the Http application.
4736
*/
4837
public static async boot(options?: HttpOptions): Promise<ServerImpl> {
38+
options = Options.create(options, {
39+
initOnly: false,
40+
isAWSLambda: false,
41+
host: Config.get('http.host', '127.0.0.1'),
42+
port: Config.get('http.port', 3000),
43+
routePath: Config.get('rc.http.route', Path.routes(`http.${Path.ext()}`)),
44+
kernelPath: Config.get(
45+
'rc.http.kernel',
46+
'@athenna/http/kernels/HttpKernel'
47+
)
48+
})
49+
4950
const server = await this.init(options)
5051

5152
if (options.initOnly) {
5253
return server
5354
}
5455

56+
if (options.isAWSLambda) {
57+
return this.resolveAWSLambdaProxy(server)
58+
}
59+
5560
await server.listen({ host: options.host, port: options.port })
5661

5762
if (Config.notExists('rc.bootLogs') || Config.is('rc.bootLogs', false)) {
@@ -110,4 +115,21 @@ export class Http {
110115
)
111116
}
112117
}
118+
119+
/**
120+
* Resolve the AWS Lambda proxy.
121+
*/
122+
private static async resolveAWSLambdaProxy(server: ServerImpl) {
123+
const awsLambda = await Module.safeImport('@athenna/http/awslambda')
124+
125+
if (awsLambda?.default) {
126+
return awsLambda.default(server.fastify)
127+
}
128+
129+
if (!awsLambda) {
130+
throw new Error('The library @fastify/aws-lambda is not installed')
131+
}
132+
133+
return awsLambda(server.fastify)
134+
}
113135
}

src/types/HttpOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export type HttpOptions = {
1616
*/
1717
initOnly?: boolean
1818

19+
/**
20+
* If true, the server will be initialized to be consumed by AWS Lambda.
21+
*
22+
* @default false
23+
*/
24+
isAWSLambda?: boolean
25+
1926
/**
2027
* The host where the server will run. By default Athenna will read the "http.host" config
2128
* to get this information, but you can set here and subscribe this behavior.

tests/unit/applications/HttpTest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,15 @@ export default class HttpTest {
141141
assert.calledWith(successMock, 'Http server started on ({yellow} localhost:3000)')
142142
assert.calledWith(successMock, 'Kernel ({yellow} HttpKernel) successfully booted')
143143
}
144+
145+
@Test()
146+
public async shouldThrowAnErrorWhenBootingAHttpApplicationToBeConsumedByAWSLambdaAndTheLibraryIsNotInstalled({
147+
assert
148+
}: Context) {
149+
Config.set('rc.bootLogs', true)
150+
151+
assert.rejects(() => Http.boot({ isAWSLambda: true }), {
152+
message: 'The library @fastify/aws-lambda is not installed'
153+
})
154+
}
144155
}

tests/unit/commands/BuildCommandTest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export default class BuildCommandTest extends BaseCommandTest {
1616
public async shouldBeAbleToBuildTheApplicationCode({ assert, command }: Context) {
1717
const output = await command.run('build')
1818

19-
console.log(output.output.stdout)
20-
console.log(output.output.stderr)
2119
output.assertSucceeded()
2220
output.assertLogged('Application successfully compiled')
2321

tests/unit/commands/MakeTestCommandTest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ export default class MakeTestCommandTest extends BaseCommandTest {
6060
public async shouldBeAbleToCreateATestFileUsingCronTemplate({ assert, command }: Context) {
6161
const output = await command.run('make:test TestTest --cron')
6262

63-
console.log(output.output)
64-
6563
output.assertSucceeded()
6664
output.assertLogged('[ MAKING TEST ]')
6765
output.assertLogged('[ success ] Test "TestTest" successfully created.')

0 commit comments

Comments
 (0)