Skip to content

Feature/websockets #6

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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
64 changes: 55 additions & 9 deletions src/routeSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const get = require('lodash.get');

const config = require('./config');

const isNonNegativeInteger = (val) => {
return Number.isInteger(val) && val >= 0;
}
const isNonNegativeInteger = val => Number.isInteger(val) && val >= 0;

const isLoggingLevel = val => val === 'INFO' || val === 'ERROR' || val === 'OFF';

const chooseSetting = (routeSetting, defaultSetting) => {
if (typeof routeSetting !== 'undefined') {
Expand All @@ -17,13 +17,18 @@ const chooseSetting = (routeSetting, defaultSetting) => {

class RouteSettings {

constructor(functionName, path, method, detailedMetricsEnabled, burstLimit, rateLimit) {
constructor(functionName, path, method, route, detailedMetricsEnabled, burstLimit, rateLimit, dataTraceEnabled, loggingLevel) {
this.functionName = functionName;
this.path = path;
this.method = method;
this.detailedMetricsEnabled = detailedMetricsEnabled;
this.burstLimit = burstLimit;
this.rateLimit = rateLimit;

/* Websocket only fields */
this.dataTraceEnabled = dataTraceEnabled;
this.loggingLevel = loggingLevel;

this.validate();
}

Expand All @@ -42,17 +47,30 @@ class RouteSettings {
if (!(typeof this.rateLimit === 'undefined') && !isNonNegativeInteger(this.rateLimit)) {
throw new Error(`[${config.app}] rateLimit must be greater than or equal to 0.`);
}

if (!(typeof this.dataTraceEnabled === 'undefined') && !(typeof this.dataTraceEnabled === 'boolean')) {
throw new Error(`[${config.app}] dataTraceEnabled must be boolean.`);
}

if (!(typeof this.loggingLevel === 'undefined') && !isLoggingLevel(this.loggingLevel)) {
throw new Error(`[${config.app}] loggingLevel must be one of INFO, ERROR or OFF.`);
}
}

getRouteKey() {
if (this.route) {
return this.route;
}
return `${this.method} ${this.path}`;
}

static buildDefaultRouteSettings(serverless) {
const metricsEnabled = get(serverless, `service.custom.${config.key}.detailedMetricsEnabled`);
const burstLimit = get(serverless, `service.custom.${config.key}.burstLimit`);
const rateLimit = get(serverless, `service.custom.${config.key}.rateLimit`);
return new RouteSettings(undefined, undefined, undefined, metricsEnabled, burstLimit, rateLimit);
const metricsEnabled = get(serverless, `service.custom.${config.key}.detailedMetricsEnabled`);
const burstLimit = get(serverless, `service.custom.${config.key}.burstLimit`);
const rateLimit = get(serverless, `service.custom.${config.key}.rateLimit`);
const dataTraceEnabled = get(serverless, `service.custom.${config.key}.dataTraceEnabled`);
const loggingLevel = get(serverless, `service.custom.${config.key}.loggingLevel`);
return new RouteSettings(undefined, undefined, undefined, undefined, metricsEnabled, burstLimit, rateLimit, dataTraceEnabled, loggingLevel);
}

static buildRouteSettings(functionName, event, defaultRouteSettings) {
Expand All @@ -67,7 +85,35 @@ class RouteSettings {
const actualBurstLimit = chooseSetting(burstLimit, defaultRouteSettings.burstLimit);
const actualRateLimit = chooseSetting(rateLimit, defaultRouteSettings.rateLimit);

return new RouteSettings(functionName, path, method, actualMetricsEnabled, actualBurstLimit, actualRateLimit);
return new RouteSettings(functionName, path, method, undefined, actualMetricsEnabled, actualBurstLimit, actualRateLimit, undefined, undefined);
}

static buildWsRouteSettings(functionName, event, defaultRouteSettings) {
const route = (typeof event.websocket === 'string') ? event.websocket : get(event, 'websocket.route');
const metricsEnabled = get(event, `websocket.${config.key}.detailedMetricsEnabled`);
const burstLimit = get(event, `websocket.${config.key}.burstLimit`);
const rateLimit = get(event, `websocket.${config.key}.rateLimit`);
const dataTraceEnabled = get(event, `websocket.${config.key}.dataTraceEnabled`);
const loggingLevel = get(event, `websocket.${config.key}.loggingLevel`);

// Must choose default route settings if otherwise undefined, or will use account limits.
const actualMetricsEnabled = chooseSetting(metricsEnabled, defaultRouteSettings.metricsEnabled);
const actualBurstLimit = chooseSetting(burstLimit, defaultRouteSettings.burstLimit);
const actualRateLimit = chooseSetting(rateLimit, defaultRouteSettings.rateLimit);
const actualDataTraceEnabled = chooseSetting(dataTraceEnabled, defaultRouteSettings.dataTraceEnabled);
const actualLoggingLevel = chooseSetting(loggingLevel, defaultRouteSettings.loggingLevel);

return new RouteSettings(
functionName,
undefined,
undefined,
route,
actualMetricsEnabled,
actualBurstLimit,
actualRateLimit,
actualDataTraceEnabled,
actualLoggingLevel
);
}

}
Expand Down
4 changes: 4 additions & 0 deletions src/stageSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class StageSettings {
this.defaultRouteSettings = RouteSettings.buildDefaultRouteSettings(serverless);

this.routeSettings = [];
this.wsRouteSettings = [];

for (let functionName in serverless.service.functions) {
let functionSettings = serverless.service.functions[functionName];
Expand All @@ -34,6 +35,9 @@ class StageSettings {
for (let event of functionSettings.events) {
if (get(event, `httpApi.${config.key}`)) {
this.routeSettings.push(RouteSettings.buildRouteSettings(functionName, event, this.defaultRouteSettings));
}
else if (get(event, `websocket.${config.key}`)) {
this.outeSettings.push(WsRouteSettings.buildRouteSettings(functionName, event, this.defaultRouteSettings));
}
}
}
Expand Down