2016-10-22 00:27:47 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
2016-10-18 20:03:51 +08:00
|
|
|
import Logger from './logger';
|
|
|
|
import Redis from './redis';
|
2017-04-06 20:36:59 +08:00
|
|
|
import locales from '../../utils/locales';
|
|
|
|
|
2017-11-01 01:13:44 +08:00
|
|
|
let DEFAULT_LANGUAGE = null;
|
2017-04-06 20:36:59 +08:00
|
|
|
const availableLocales = [];
|
2016-08-17 23:48:03 +08:00
|
|
|
|
2016-10-18 20:03:51 +08:00
|
|
|
Meteor.startup(() => {
|
2016-08-17 23:48:03 +08:00
|
|
|
const APP_CONFIG = Meteor.settings.public.app;
|
2016-10-22 00:27:47 +08:00
|
|
|
Logger.info(`SERVER STARTED. ENV=${Meteor.settings.runtime.env}`, APP_CONFIG);
|
2017-11-01 01:13:44 +08:00
|
|
|
|
|
|
|
DEFAULT_LANGUAGE = Meteor.settings.public.app.defaultSettings.application.locale
|
2016-05-05 04:25:34 +08:00
|
|
|
});
|
2016-05-05 01:00:57 +08:00
|
|
|
|
2017-10-06 20:50:01 +08:00
|
|
|
WebApp.connectHandlers.use('/check', (req, res) => {
|
2017-06-03 03:25:02 +08:00
|
|
|
const payload = { html5clientStatus: 'running' };
|
2016-06-14 01:56:41 +08:00
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.writeHead(200);
|
|
|
|
res.end(JSON.stringify(payload));
|
|
|
|
});
|
|
|
|
|
2016-10-08 00:36:27 +08:00
|
|
|
WebApp.connectHandlers.use('/locale', (req, res) => {
|
2017-03-10 03:50:21 +08:00
|
|
|
const APP_CONFIG = Meteor.settings.public.app;
|
2017-10-07 00:29:52 +08:00
|
|
|
const defaultLocale = APP_CONFIG.defaultSettings.application.locale;
|
2017-11-01 01:13:44 +08:00
|
|
|
const localeRegion = req.query.locale.split(/[-_]/g);;
|
2017-10-27 19:36:27 +08:00
|
|
|
const localeList = [defaultLocale, localeRegion[0]];
|
|
|
|
|
|
|
|
let normalizedLocale = localeRegion[0];
|
2017-03-10 03:50:21 +08:00
|
|
|
let messages = {};
|
2017-10-27 19:36:27 +08:00
|
|
|
|
2017-03-10 03:50:21 +08:00
|
|
|
if (localeRegion.length > 1) {
|
2017-10-27 19:36:27 +08:00
|
|
|
normalizedLocale = `${localeRegion[0]}_${localeRegion[1].toUpperCase()}`;
|
|
|
|
localeList.push(normalizedLocale);
|
2017-03-10 03:50:21 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:36:27 +08:00
|
|
|
localeList.forEach((locale) => {
|
2017-03-10 03:50:21 +08:00
|
|
|
try {
|
|
|
|
const data = Assets.getText(`locales/${locale}.json`);
|
|
|
|
messages = Object.assign(messages, JSON.parse(data));
|
2017-11-01 01:13:44 +08:00
|
|
|
normalizedLocale = locale
|
2017-03-10 03:50:21 +08:00
|
|
|
} catch (e) {
|
2017-11-01 01:13:44 +08:00
|
|
|
console.log('error', locale);
|
2017-03-10 03:50:21 +08:00
|
|
|
}
|
|
|
|
});
|
2016-10-08 00:36:27 +08:00
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2017-10-27 19:36:27 +08:00
|
|
|
res.end(JSON.stringify({ normalizedLocale, messages }));
|
2016-10-08 00:36:27 +08:00
|
|
|
});
|
|
|
|
|
2017-04-06 20:36:59 +08:00
|
|
|
WebApp.connectHandlers.use('/locales', (req, res) => {
|
|
|
|
if (!availableLocales.length) {
|
2017-06-03 03:25:02 +08:00
|
|
|
locales.forEach((l) => {
|
2017-04-06 20:36:59 +08:00
|
|
|
try {
|
|
|
|
Assets.absoluteFilePath(`locales/${l.locale}.json`);
|
|
|
|
availableLocales.push(l);
|
|
|
|
} catch (e) {
|
2017-04-06 21:14:12 +08:00
|
|
|
// Getting here means the locale is not available on the files.
|
2017-04-06 20:36:59 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.writeHead(200);
|
|
|
|
res.end(JSON.stringify(availableLocales));
|
|
|
|
});
|
|
|
|
|
2016-10-18 20:03:51 +08:00
|
|
|
export const eventEmitter = Redis.emitter;
|
2016-05-05 01:00:57 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
export const redisPubSub = Redis;
|