Skip to content

Commit

Permalink
Implemented ALB support
Browse files Browse the repository at this point in the history
Based on:
  - #1250 by bayoumymac
  - https://github.com/tmaslen/serverless-local-alb-example by tmaslen
  • Loading branch information
ihendriks committed Aug 12, 2022
1 parent c1f3c1b commit 5ff56df
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default class ServerlessOffline {

#webSocket = null

#alb = null

commands = {
offline: {
// add start nested options
Expand Down Expand Up @@ -61,7 +63,7 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()

const { httpEvents, lambdas, scheduleEvents, webSocketEvents } =
const { httpEvents, lambdas, scheduleEvents, webSocketEvents, albEvents } =
this.#getEvents()

// if (lambdas.length > 0) {
Expand All @@ -70,6 +72,10 @@ export default class ServerlessOffline {

const eventModules = []

if (albEvents.length > 0) {
eventModules.push(this.#createAlb(albEvents))
}

if (httpEvents.length > 0) {
eventModules.push(this.#createHttp(httpEvents))
}
Expand Down Expand Up @@ -99,6 +105,10 @@ export default class ServerlessOffline {
eventModules.push(this.#lambda.stop(SERVER_SHUTDOWN_TIMEOUT))
}

if (this.#alb) {
eventModules.push(this.#alb.stop(SERVER_SHUTDOWN_TIMEOUT))
}

if (this.#http) {
eventModules.push(this.#http.stop(SERVER_SHUTDOWN_TIMEOUT))
}
Expand Down Expand Up @@ -210,6 +220,18 @@ export default class ServerlessOffline {
return this.#webSocket.start()
}

async #createAlb(events, skipStart) {
const { default: Alb } = await import('./events/alb/index.js')

this.#alb = new Alb(this.#serverless, this.#options, this.#lambda)

this.#alb.create(events)

if (!skipStart) {
await this.#alb.start()
}
}

#mergeOptions() {
const {
service: { custom = {}, provider },
Expand Down Expand Up @@ -260,6 +282,7 @@ export default class ServerlessOffline {
const lambdas = []
const scheduleEvents = []
const webSocketEvents = []
const albEvents = []

const functionKeys = service.getAllFunctions()

Expand All @@ -273,7 +296,7 @@ export default class ServerlessOffline {
const events = service.getAllEventsInFunction(functionKey) || []

events.forEach((event) => {
const { http, httpApi, schedule, websocket } = event
const { http, httpApi, schedule, websocket, alb } = event

if ((http || httpApi) && functionDefinition.handler) {
const httpEvent = {
Expand Down Expand Up @@ -353,6 +376,14 @@ export default class ServerlessOffline {
websocket,
})
}

if (alb) {
albEvents.push({
alb,
functionKey,
handler: functionDefinition.handler,
})
}
})
})

Expand All @@ -370,6 +401,7 @@ export default class ServerlessOffline {
}

return {
albEvents,
httpEvents,
lambdas,
scheduleEvents,
Expand Down
4 changes: 4 additions & 0 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default {
albPort: {
type: 'string',
usage: 'ALB port to listen on. Default: 3003',
},
apiKey: {
type: 'string',
usage:
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createApiKey } from '../utils/index.js'

export default {
albPort: 3003,
apiKey: createApiKey(),
corsAllowHeaders: 'accept,content-type,x-api-key,authorization',
corsAllowOrigin: '*',
Expand Down
32 changes: 32 additions & 0 deletions src/events/alb/Alb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import AlbEventDefinition from './AlbEventDefinition.js'
import HttpServer from './HttpServer.js'

export default class Alb {
#httpServer = null

constructor(serverless, options, lambda) {
this.#httpServer = new HttpServer(serverless, options, lambda)
}

start() {
return this.#httpServer.start()
}

stop(timeout) {
return this.#httpServer.stop(timeout)
}

#createEvent(functionKey, rawAlbEventDefinition) {
const albEvent = new AlbEventDefinition(rawAlbEventDefinition)

this.#httpServer.createRoutes(functionKey, albEvent)
}

create(events) {
events.forEach(({ functionKey, alb }) => {
this.#createEvent(functionKey, alb)
})

this.#httpServer.writeRoutesTerminal()
}
}
22 changes: 22 additions & 0 deletions src/events/alb/AlbEventDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { assign } = Object

export default class AlbEventDefinition {
constructor(rawAlbEventDefinition) {
let listenerArn
let priority
let conditions
let rest

if (typeof rawAlbEventDefinition === 'string') {
;[listenerArn, priority, conditions] = rawAlbEventDefinition.split(' ')
} else {
;({ listenerArn, priority, conditions, ...rest } = rawAlbEventDefinition)
}

this.listenerArn = listenerArn
this.priority = priority
this.conditions = conditions

assign(this, rest)
}
}
Loading

0 comments on commit 5ff56df

Please sign in to comment.