Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.
Prev Previous commit
Next Next commit
moved files
  • Loading branch information
mmahalwy committed Aug 14, 2016
commit bf0867a6cea32c58ebed4b170ad8835beffd434f
57 changes: 57 additions & 0 deletions src/server/config/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import express from 'express';
import path from 'path';
import compression from 'compression';
import bodyParser from 'body-parser';
import logger from 'morgan';
import favicon from 'serve-favicon';
import useragent from 'express-useragent';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import httpProxy from 'http-proxy';

import sitemap from './sitemap';
import support from './support';

const proxyApi = httpProxy.createProxyServer({
target: process.env.API_URL,
secure: true
});

proxyApi.on('error', (error, req, res) => {
let json;
if (error.code !== 'ECONNRESET') {
console.error('proxy error', error);
}
if (!res.headersSent) {
res.writeHead(500, {'content-type': 'application/json'});
}

json = {error: 'proxy_error', reason: error.message};
res.end(JSON.stringify(json));
});

export default function(server) {
server.use(compression());
server.use(bodyParser.json());
server.use(logger('dev'));
server.use(useragent.express());
server.use(cookieParser());
server.use(cors());

// Static content
server.use(favicon(path.join((process.env.PWD || process.env.pm_cwd) , '/static/images/favicon.ico')));
server.use(express.static(path.join(process.env.PWD || process.env.pm_cwd, '/static')));
server.use('/public', express.static(path.join((process.env.PWD || process.env.pm_cwd), '/static/dist')));
// server.use('/build', express.static(path.join((process.env.PWD || process.env.pm_cwd), '/static/dist')));

sitemap(server);
support(server);

server.get(/^\/(images|fonts)\/.*/, function(req, res) {
res.redirect(301, '//quran-1f14.kxcdn.com' + req.path);
});

server.use('/api', (req, res) => {
proxyApi.web(req, res);
});
}
56 changes: 56 additions & 0 deletions src/server/config/sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sitemap from 'sitemap';

import ApiClient from '../../src/helpers/ApiClient';


export default (server) => {
server.get('/sitemap.xml', (req, res) => {
const client = new ApiClient(req);
const urls = [];

client.get('/api/v2/surahs').then(surahs => {
surahs.forEach(surah => {
Array.apply(null, {length: surah.ayat}).forEach((_, index) => {
const ayahId = index + 1;

urls.push({
url: `/${surah.id}/${ayahId}`,
changefreq: 'weekly',
priority: 1
});

urls.push({
url: `/${surah.id}/${ayahId}-${ayahId + 9}`,
changefreq: 'weekly',
priority: 1
});
});

urls.push({
url: `/${surah.id}`,
changefreq: 'weekly',
priority: 1
});
});


const xml = sitemap.createSitemap({
hostname: 'https://quran.com',
cacheTime: 600000, // 600 sec cache period
urls: [
...urls,
{ url: '/about', changefreq: 'monthly', priority: 0.3 },
{ url: '/contactus', changefreq: 'monthly', priority: 0.3 },
{ url: '/contact', changefreq: 'monthly', priority: 0.3 },
{ url: '/donations', changefreq: 'monthly', priority: 0.3 },
{ url: '/contributions', changefreq: 'monthly', priority: 0.3 },

{ url: '/search', changefreq: 'weekly', priority: 0.8 }
]
});

res.header('Content-Type', 'application/xml');
res.send(xml.toString());
}).catch(err => console.trace(err));
});
};
13 changes: 13 additions & 0 deletions src/server/config/support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import superagent from 'superagent';

export default (server) => {
server.post('/support', (req, res) => {
superagent
.post('https://quran.zendesk.com/api/v2/tickets.json')
.send({ ticket: req.body })
.auth('[email protected]/token', 'aGGdpbEgkcKgpGscrqq8QU6z8wsdrlrTCKWHMJoz')
.end((err, { body }) => {
res.send(body || err);
});
});
}