Skip to content
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
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "5.21.0",
"version": "5.22.0",
"description": "One foundation for multiple applications.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -85,7 +85,7 @@
"@athenna/common": "^5.14.0",
"@athenna/config": "^5.4.0",
"@athenna/cron": "^5.9.0",
"@athenna/http": "^5.34.0",
"@athenna/http": "^5.38.0",
"@athenna/ioc": "^5.2.0",
"@athenna/logger": "^5.8.0",
"@athenna/test": "^5.5.0",
Expand Down
44 changes: 33 additions & 11 deletions src/applications/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ export class Http {
* Only initialize the server without booting it.
*/
public static async init(options?: HttpOptions): Promise<ServerImpl> {
options = Options.create(options, {
initOnly: false,
host: Config.get('http.host', '127.0.0.1'),
port: Config.get('http.port', 3000),
routePath: Config.get('rc.http.route', Path.routes(`http.${Path.ext()}`)),
kernelPath: Config.get(
'rc.http.kernel',
'@athenna/http/kernels/HttpKernel'
)
})

const server = ioc.safeUse('Athenna/Core/HttpServer')

debug('booting http application with options %o', options)
Expand All @@ -46,12 +35,28 @@ export class Http {
* Boot the Http application.
*/
public static async boot(options?: HttpOptions): Promise<ServerImpl> {
options = Options.create(options, {
initOnly: false,
isAWSLambda: false,
host: Config.get('http.host', '127.0.0.1'),
port: Config.get('http.port', 3000),
routePath: Config.get('rc.http.route', Path.routes(`http.${Path.ext()}`)),
kernelPath: Config.get(
'rc.http.kernel',
'@athenna/http/kernels/HttpKernel'
)
})

const server = await this.init(options)

if (options.initOnly) {
return server
}

if (options.isAWSLambda) {
return this.resolveAWSLambdaProxy(server)
}

await server.listen({ host: options.host, port: options.port })

if (Config.notExists('rc.bootLogs') || Config.is('rc.bootLogs', false)) {
Expand Down Expand Up @@ -110,4 +115,21 @@ export class Http {
)
}
}

/**
* Resolve the AWS Lambda proxy.
*/
private static async resolveAWSLambdaProxy(server: ServerImpl) {
const awsLambda = await Module.safeImport('@athenna/http/awslambda')

if (awsLambda?.default) {
return awsLambda.default(server.fastify)
}

if (!awsLambda) {
throw new Error('The library @fastify/aws-lambda is not installed')
}

return awsLambda(server.fastify)
}
}
7 changes: 7 additions & 0 deletions src/types/HttpOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export type HttpOptions = {
*/
initOnly?: boolean

/**
* If true, the server will be initialized to be consumed by AWS Lambda.
*
* @default false
*/
isAWSLambda?: boolean

/**
* The host where the server will run. By default Athenna will read the "http.host" config
* to get this information, but you can set here and subscribe this behavior.
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/applications/HttpTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ export default class HttpTest {
assert.calledWith(successMock, 'Http server started on ({yellow} localhost:3000)')
assert.calledWith(successMock, 'Kernel ({yellow} HttpKernel) successfully booted')
}

@Test()
public async shouldThrowAnErrorWhenBootingAHttpApplicationToBeConsumedByAWSLambdaAndTheLibraryIsNotInstalled({
assert
}: Context) {
Config.set('rc.bootLogs', true)

assert.rejects(() => Http.boot({ isAWSLambda: true }), {
message: 'The library @fastify/aws-lambda is not installed'
})
}
}
2 changes: 0 additions & 2 deletions tests/unit/commands/BuildCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default class BuildCommandTest extends BaseCommandTest {
public async shouldBeAbleToBuildTheApplicationCode({ assert, command }: Context) {
const output = await command.run('build')

console.log(output.output.stdout)
console.log(output.output.stderr)
output.assertSucceeded()
output.assertLogged('Application successfully compiled')

Expand Down
2 changes: 0 additions & 2 deletions tests/unit/commands/MakeTestCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export default class MakeTestCommandTest extends BaseCommandTest {
public async shouldBeAbleToCreateATestFileUsingCronTemplate({ assert, command }: Context) {
const output = await command.run('make:test TestTest --cron')

console.log(output.output)

output.assertSucceeded()
output.assertLogged('[ MAKING TEST ]')
output.assertLogged('[ success ] Test "TestTest" successfully created.')
Expand Down
Loading