forked from stacks-archive/app.co
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
168 lines (148 loc) · 6.08 KB
/
server.js
File metadata and controls
168 lines (148 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require('express')
const next = require('next')
const dotenv = require('dotenv')
const compression = require('compression')
const cookiesMiddleware = require('universal-cookie-express')
const expressSitemapXml = require('express-sitemap-xml')
const fs = require('fs-extra')
const secure = require('express-force-https')
const dev = process.env.NODE_ENV !== 'production'
if (dev) {
dotenv.config()
}
const { ssrCache } = require('./common/lib/cache')
const { getApps, getAppMiningMonths } = require('./common/lib/api')
const getSitemapURLs = require('./common/lib/sitemap')
const RSSController = require('./common/controllers/rss-controller')
const slugify = require('./common/lib/slugify')
const port = parseInt(process.env.PORT, 10) || 3000
const app = next({ dev })
const handle = app.getRequestHandler()
const apiServer = process.env.API_SERVER || 'https://app-co-api-staging.herokuapp.com'
async function renderAndCache(req, res, pagePath, serverData) {
try {
const data = await getApps(apiServer)
data.apiServer = apiServer
let appMiningMonths = []
if (serverData && serverData.fetchMiningResults) {
appMiningMonths = await getAppMiningMonths(apiServer)
}
const dataToPass = {
...data,
...serverData,
appMiningMonths
}
const html = await app.renderToHTML(req, res, pagePath, dataToPass)
res.send(html)
} catch (err) {
console.log(err)
app.renderError(err, req, res, pagePath)
}
}
app.prepare().then(() => {
getApps(apiServer).then((apps) => {
const server = express()
if (!dev) {
server.use(secure)
}
server.use(cookiesMiddleware())
server.use(compression())
server.set('views', './common/server-views')
server.set('view engine', 'pug')
server.use((req, res, _next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', '*')
_next()
})
server.get('/', (req, res) => renderAndCache(req, res, '/'))
/**
* Mining Pages
*/
server.get('/mining', (req, res) => renderAndCache(req, res, '/mining'))
server.get('/mining/apps', (req, res) => renderAndCache(req, res, '/mining/apps'))
server.get('/mining/faq', (req, res) => renderAndCache(req, res, '/mining/faq'))
server.get('/mining/terms', (req, res) => renderAndCache(req, res, '/mining/terms'))
server.get('/mining/privacy', (req, res) => renderAndCache(req, res, '/mining/privacy'))
server.get('/mining/developer-instructions', (req, res) =>
renderAndCache(req, res, '/mining/developer-instructions')
)
server.get('/mining/reviewer-instructions', (req, res) => renderAndCache(req, res, '/mining/reviewer-instructions'))
server.get('/mining/:date', (req, res) => {
const { date } = req.params
const [month, year] = date.split('-')
renderAndCache(req, res, '/mining/month-results', { month, year, fetchMiningResults: true })
})
/**
* App Pages
*/
server.get('/app/:appSlug', (req, res) => renderAndCache(req, res, '/'))
server.get('/apps', (req, res) => renderAndCache(req, res, '/'))
/**
* Platforms
*/
server.get('/platforms', (req, res) => renderAndCache(req, res, '/platforms'))
server.get('/platforms/all', (req, res) => renderAndCache(req, res, '/platforms'))
// redirect to top-level page
server.get('/platforms/:platform', (req, res) => res.redirect(`/${req.params.platform}`))
server.get('/platform/:platform', (req, res) => res.redirect(`/${req.params.platform}`))
/**
* Categories
*/
server.get('/categories', (req, res) => renderAndCache(req, res, '/categories'))
server.get('/categories/all', (req, res) => renderAndCache(req, res, '/categories/all'))
server.get('/categories/:category', (req, res) =>
renderAndCache(req, res, '/categories', { category: req.params.category })
)
server.get('/category/:category', (req, res) => res.redirect(`/categories/${req.params.category}`))
/**
* General Pages
*/
server.get('/faq', (req, res) => renderAndCache(req, res, '/faq'))
server.get('/submit', (req, res) => renderAndCache(req, res, '/submit'))
server.get('/all', (req, res) => renderAndCache(req, res, '/all'))
server.get('/terms', (req, res) => renderAndCache(req, res, '/terms'))
/**
* Admin Pages
*/
server.get('/admin', (req, res) => renderAndCache(req, res, '/admin'))
server.get('/admin/app', (req, res) => renderAndCache(req, res, '/admin/app'))
server.get('/admin/pending', (req, res) => renderAndCache(req, res, '/admin/pending'))
server.get('/admin/mining/months', (req, res) => renderAndCache(req, res, '/admin/mining/months'))
server.get('/admin/mining/months/:id', (req, res) =>
renderAndCache(req, res, '/admin/mining/month', { monthId: req.params.id })
)
server.get('/admin/mining/months/:id/upload-report', (req, res) =>
renderAndCache(req, res, '/admin/mining/upload-report', { monthId: req.params.id })
)
server.get('/admin/mining/months/:monthId/reviewers/:reviewerId', (req, res) =>
renderAndCache(req, res, '/admin/mining/reviewer')
)
apps.platforms.forEach((platform) => {
server.get(`/${slugify(platform)}`, (req, res) => {
req.params.platform = platform
return renderAndCache(req, res, '/platforms', { platform })
})
})
server.get('/clear-cache', (req, res) => {
if (req.query.key === process.env.API_KEY) {
console.log('Clearing cache from API')
ssrCache.reset()
res.json({ success: true })
} else {
res.status(400).json({ success: false })
}
})
server.use('/rss', RSSController)
server.use(expressSitemapXml(getSitemapURLs(apiServer), 'https://app.co'))
server.get('/robots.txt', async (req, res) => {
const robots = await fs.readFile('./static/robots.txt')
res.header('Content-Type', 'text/plain')
res.status(200).send(robots)
})
server.get('*', (req, res) => handle(req, res))
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
})