forked from iterative/dvc.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (61 loc) · 2.47 KB
/
server.js
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
// This file doesn't go through babel or webpack transformation.
// Make sure the syntax and sources this file requires are compatible with the current node version you are running
// See https://github.com/zeit/next.js/issues/1245 for discussions on Universal Webpack or universal Babel
const { createServer } = require('http')
const { parse } = require('url')
const _ = require('force-ssl-heroku')
const next = require('next')
const querystring = require('querystring')
const dev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
const doc = /^\/doc.*/i
const s3 = /^\/s3\/.*/i
const pkg = /^\/(deb|rpm)\/.*/i
const chat = /^\/(help|chat)\/?$/i
if (req.headers.host === 'man.dvc.org') {
const doc_pathname = "/doc/commands-reference" + pathname
res.writeHead(301, { 'Location': "https://dvc.org" + doc_pathname })
res.end()
} else if (req.headers.host === 'pycon2019.dvc.org') {
res.writeHead(301, { 'Location': "https://dvc.org/doc/get-started" })
res.end()
} else if (req.headers.host === 'remote.dvc.org') {
res.writeHead(301, { 'Location': "https://s3-us-west-2.amazonaws.com/dvc-storage" + pathname})
res.end()
} else if (doc.test(pathname)) {
let normalized_pathname = pathname.replace(/^\/doc[^?\/]*/i, '/doc')
if (normalized_pathname !== pathname) {
res.writeHead(301, { 'Location': normalized_pathname +
(Object.keys(query).length === 0 ? '' : '?') +
querystring.stringify(query)})
res.end()
} else {
app.render(req, res, '/doc', query)
}
} else if (s3.test(pathname)) {
res.writeHead(301, {'Location':
"https://s3-us-west-2.amazonaws.com/dvc-share/" +
pathname.substring(4)})
res.end()
} else if (pkg.test(pathname)) {
res.writeHead(301, {'Location':
"https://s3-us-east-2.amazonaws.com/dvc-s3-repo/" + pathname.substring(1, 4) + '/' +
pathname.substring(5)})
res.end()
} else if (chat.test(pathname)) {
res.writeHead(301, {'Location': "https://discordapp.com/invite/dvwXA2N"})
res.end()
} else {
handle(req, res, parsedUrl)
}
}).listen(port, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})