feat(server): Add URL utils to compute server URL, HOST, PORT, etc.

pull/12989/merge
Mrugesh Mohapatra 2017-04-27 01:54:56 +05:30 committed by Berkeley Martinez
parent d313ab68e4
commit 2321f7b326
2 changed files with 50 additions and 9 deletions

View File

@ -10,12 +10,16 @@ import loopback from 'loopback';
import { saveUser, observeMethod } from '../../server/utils/rx.js';
import { blacklistedUsernames } from '../../server/utils/constants.js';
import { wrapHandledError } from '../../server/utils/create-handled-error.js';
import {
getServerFullURL,
getEmailSender,
getProtocol,
getHost,
getPort
} from '../../server/utils/url-utils.js';
const debug = debugFactory('fcc:user:remote');
const BROWNIEPOINTS_TIMEOUT = [1, 'hour'];
const isDev = process.env.NODE_ENV !== 'production';
const devHost = process.env.HOST || 'localhost';
const emailSender = process.env.EMAIL_SENDER || 'team@freecodecamp.org';
const createEmailError = () => new Error(
'Please check to make sure the email is a valid email address.'
@ -592,11 +596,11 @@ module.exports = function(User) {
const { id: loginToken } = token;
const loginEmail = user.email;
const host = getServerFullURL();
const mailOptions = {
type: 'email',
to: user.email,
from: emailSender,
from: getEmailSender(),
subject: 'freeCodeCamp - Authentication Request!',
text: renderAuthEmail({
loginEmail,
@ -697,11 +701,11 @@ module.exports = function(User) {
const mailOptions = {
type: 'email',
to: email,
from: emailSender,
from: getEmailSender(),
subject: 'freeCodeCamp - Email Update Requested',
protocol: isDev ? null : 'https',
host: isDev ? devHost : 'freecodecamp.org',
port: isDev ? null : 443,
protocol: getProtocol(),
host: getHost(),
port: getPort(),
template: path.join(
__dirname,
'..',

37
server/utils/url-utils.js Normal file
View File

@ -0,0 +1,37 @@
const isDev = process.env.NODE_ENV !== 'production';
const isBeta = !!process.env.BETA;
export function getEmailSender() {
return process.env.EMAIL_SENDER || 'team@freecodecamp.com';
}
export function getPort() {
if (!isDev) {
return '443';
}
return process.env.SYNC_PORT || '3000';
}
export function getProtocol() {
return isDev ? 'http' : 'https';
}
export function getHost() {
if (isDev) {
return process.env.HOST || 'localhost';
}
return isBeta ? 'beta.freecodecamp.com' : 'freecodecamp.com';
}
export function getServerFullURL() {
if (!isDev) {
return getProtocol()
+ '://'
+ getHost();
}
return getProtocol()
+ '://'
+ getHost()
+ ':'
+ getPort();
}