Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6aecd27

Browse files
committed
Cleanup the http api.
1 parent 62a0738 commit 6aecd27

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

src/api/http-api.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Log } from './../log';
2-
var url = require('url');
2+
let url = require('url');
33
import * as _ from 'lodash';
44

55
export class HttpApi {
@@ -9,22 +9,15 @@ export class HttpApi {
99
* @param {any} io
1010
* @param {any} channel
1111
* @param {any} express
12-
* @param {any} options object apiOriginAllow
12+
* @param {object} options object
1313
*/
1414
constructor(private io, private channel, private express, private options) { }
1515

1616
/**
1717
* Initialize the API.
1818
*/
1919
init(): void {
20-
if(this.options.allowCors){
21-
this.express.use( (req, res, next) => {
22-
res.header('Access-Control-Allow-Origin', this.options.allowOrigin);
23-
res.header('Access-Control-Allow-Methods', this.options.allowMethods);
24-
res.header('Access-Control-Allow-Headers', this.options.allowHeaders);
25-
next();
26-
});
27-
}
20+
this.corsMiddleware();
2821

2922
this.express.get(
3023
'/apps/:appId/status',
@@ -47,6 +40,20 @@ export class HttpApi {
4740
);
4841
}
4942

43+
/**
44+
* Add CORS middleware if applicable.
45+
*/
46+
corsMiddleware(): void {
47+
if (this.options.allowCors) {
48+
this.express.use((req, res, next) => {
49+
res.header('Access-Control-Allow-Origin', this.options.allowOrigin);
50+
res.header('Access-Control-Allow-Methods', this.options.allowMethods);
51+
res.header('Access-Control-Allow-Headers', this.options.allowHeaders);
52+
next();
53+
});
54+
}
55+
}
56+
5057
/**
5158
* Get the status of the server.
5259
*
@@ -68,9 +75,9 @@ export class HttpApi {
6875
* @param {any} res
6976
*/
7077
getChannels(req: any, res: any): void {
71-
var prefix = url.parse(req.url, true).query.filter_by_prefix;
72-
var rooms = this.io.sockets.adapter.rooms;
73-
var channels = {};
78+
let prefix = url.parse(req.url, true).query.filter_by_prefix;
79+
let rooms = this.io.sockets.adapter.rooms;
80+
let channels = {};
7481

7582
Object.keys(rooms).forEach(function(channelName) {
7683
if (rooms[channelName].sockets[channelName]) {
@@ -97,11 +104,11 @@ export class HttpApi {
97104
* @param {any} res
98105
*/
99106
getChannel(req: any, res: any): void {
100-
var channelName = req.params.channelName;
101-
var room = this.io.sockets.adapter.rooms[channelName];
102-
var subscriptionCount = room ? room.length : 0;
107+
let channelName = req.params.channelName;
108+
let room = this.io.sockets.adapter.rooms[channelName];
109+
let subscriptionCount = room ? room.length : 0;
103110

104-
var result = {
111+
let result = {
105112
subscription_count: subscriptionCount,
106113
occupied: !!subscriptionCount
107114
};

0 commit comments

Comments
 (0)