Merge pull request #522 from calzoneman/sioconfig-migration

Migrate socket.io configuration to new API
This commit is contained in:
Calvin Montgomery 2015-10-25 17:25:00 -07:00
commit 535b1d5d3a
9 changed files with 202 additions and 18 deletions

View file

@ -0,0 +1,47 @@
export default class IOConfiguration {
constructor(config) {
this.config = config;
}
getSocketEndpoints() {
return this.config.endpoints.slice();
}
}
IOConfiguration.fromOldConfig = function (oldConfig) {
const config = {
endpoints: []
};
if (oldConfig.get('io.ipv4-ssl')) {
config.endpoints.push({
url: oldConfig.get('io.ipv4-ssl'),
secure: true
});
}
if (oldConfig.get('io.ipv4-nossl')) {
config.endpoints.push({
url: oldConfig.get('io.ipv4-nossl'),
secure: false
});
}
if (oldConfig.get('io.ipv6-ssl')) {
config.endpoints.push({
url: oldConfig.get('io.ipv4-ssl'),
secure: true,
ipv6: true
});
}
if (oldConfig.get('io.ipv6-nossl')) {
config.endpoints.push({
url: oldConfig.get('io.ipv4-nossl'),
secure: false,
ipv6: true
});
}
return new IOConfiguration(config);
};

View file

@ -0,0 +1,14 @@
import Promise from 'bluebird';
export default class NullClusterClient {
constructor(ioConfig) {
this.ioConfig = ioConfig;
}
getSocketConfig(channel) {
const servers = this.ioConfig.getSocketEndpoints();
return Promise.resolve({
servers: servers
});
}
}

View file

@ -0,0 +1,27 @@
import IOConfiguration from '../../configuration/ioconfig';
import NullClusterClient from '../../io/cluster/nullclusterclient';
import Config from '../../config';
import CyTubeUtil from '../../utilities';
import Logger from '../../logger';
export default function initialize(app) {
const ioConfig = IOConfiguration.fromOldConfig(Config);
const clusterClient = new NullClusterClient(ioConfig);
app.get('/socketconfig/:channel.json', (req, res) => {
if (!req.params.channel || !CyTubeUtil.isValidChannelName(req.params.channel)) {
return res.status(404).json({
error: `Channel "${req.params.channel}" does not exist.`
});
}
clusterClient.getSocketConfig(req.params.channel).then(config => {
res.json(config);
}).catch(err => {
Logger.errlog.log(err.stack);
return res.status(500).json({
error: err.message
});
});
});
}

View file

@ -117,7 +117,8 @@ function handleIndex(req, res) {
}
/**
* Handles a request for the socket.io information
* Legacy socket.io configuration endpoint. This is being migrated to
* /socketconfig/<channel name>.json (see ./routes/socketconfig.js)
*/
function handleSocketConfig(req, res) {
if (/\.json$/.test(req.path)) {
@ -243,6 +244,7 @@ module.exports = {
app.get("/r/:channel", handleChannel);
app.get("/", handleIndex);
app.get("/sioconfig(.json)?", handleSocketConfig);
require("./routes/socketconfig")(app);
app.get("/useragreement", handleUserAgreement);
app.get("/contact", handleContactPage);
require("./auth").init(app);