forked from iterative/dvc.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (114 loc) · 4.2 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
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
/* eslint-env node */
// 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.js
// version you are running. (See https://github.com/zeit/next.js/issues/1245 for
// discussions on universal Webpack vs universal Babel.)
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const querystring = require('querystring')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const port = process.env.PORT || 3000
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
// Special URL redirects:
if (
(req.headers['x-forwarded-proto'] !== 'https' && !dev) ||
req.headers.host.match(/^www/) !== null
) {
// Enforce https protocol and remove www from host.
res.writeHead(301, {
Location: 'https://' + req.headers.host.replace(/^www\./, '') + req.url
})
res.end()
} else if (req.headers.host === 'man.dvc.org') {
// man.dvc.org/{cmd} -> dvc.org/doc/command-reference/{cmd},
// replace - for / in {cmd} except for /get-url, /import-url
res.writeHead(301, {
'Cache-Control': 'no-cache',
Location:
'https://dvc.org/doc/command-reference' +
(['/get-url', '/import-url'].indexOf(pathname) < 0
? pathname.replace('-', '/')
: pathname)
})
res.end()
} else if (/^(code|data|remote)\.dvc\.org$/.test(req.headers.host)) {
// {code/data/remote}.dvc.org -> corresponding S3 bucket
res.writeHead(301, {
Location:
'https://s3-us-east-2.amazonaws.com/dvc-public/' +
req.headers.host.split('.')[0] +
pathname
})
res.end()
} else if (/^\/(deb|rpm)\/.*/i.test(pathname)) {
// path /(deb|rpm) -> corresponding S3 bucket
res.writeHead(301, {
Location:
'https://s3-us-east-2.amazonaws.com/dvc-s3-repo/' +
pathname.substring(1)
})
res.end()
} else if (/^\/(help|chat)\/?$/i.test(pathname)) {
// path /(help|chat) -> Discord chat invite
res.writeHead(301, { Location: 'https://discordapp.com/invite/dvwXA2N' })
res.end()
} else if (/^\/doc\/commands-reference(\/.*)?/.test(pathname)) {
// path /doc/commands-reference... -> /doc/command-reference...
res.writeHead(301, {
Location: req.url.replace('commands-reference', 'command-reference')
})
res.end()
} else if (/^\/doc\/tutorial\/(.*)?/.test(pathname)) {
// path /doc/tutorial/... -> /doc/tutorials/deep/...
res.writeHead(301, {
Location: req.url.replace('/doc/tutorial/', '/doc/tutorials/deep/')
})
res.end()
} else if (pathname === '/doc/tutorial' || pathname === '/doc/tutorial/') {
// path /doc/tutorial -> /doc/tutorials
res.writeHead(301, {
Location: req.url.replace('/doc/tutorial', '/doc/tutorials')
})
res.end()
} else if (
pathname === '/doc/use-cases/data-and-model-files-versioning' ||
pathname === '/doc/use-cases/data-and-model-files-versioning/'
) {
// path /doc/use-cases/data-and-model-files-versioning
// -> /doc/use-cases/versioning-data-and-model-files
res.writeHead(301, {
Location: req.url.replace(
'data-and-model-files-versioning',
'versioning-data-and-model-files'
)
})
res.end()
} else if (/^\/doc.*/i.test(pathname)) {
// path /doc*/... -> /doc/...
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 {
handle(req, res, parsedUrl)
}
// Invokes server `createServer`
}).listen(port, err => {
if (err) throw err
console.info('> Ready on http://localhost:3000')
})
})