-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (57 loc) · 1.85 KB
/
server.js
File metadata and controls
70 lines (57 loc) · 1.85 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
const fs = require('fs')
const http = require('http')
const { getConfig, setConfig } = require('./src/config')
const { authorize } = require('./src/auth')
const { respondToOptions } = require('./src/utils')
const { dbInit, dbShutdown, getCodeUrl, saveUrl, removeOldUrls } = require('./src/db')
process.on('SIGHUP', () => process.exit(128 + 1))
process.on('SIGINT', () => process.exit(128 + 2))
process.on('SIGTERM', () => process.exit(128 + 15))
process.on('exit', () => {
console.log('Shutting down...')
dbShutdown()
})
dbInit()
if (fs.existsSync('./config.json')) {
try {
setConfig(JSON.parse(fs.readFileSync('./config.json', 'utf8')))
} catch (_) {
console.log('Cannot load config file')
}
}
let cleanupCounter = 0
const checkOnEvery = getConfig().cleanup.checkOnEvery
http.createServer((req, res) => {
if (req.url === undefined) {
return
}
if (req.url.startsWith('/shorten/')) {
let
code = 200,
result = {}
try {
if (req.method.toUpperCase() === 'OPTIONS') {
return respondToOptions(res)
}
authorize(req)
if (checkOnEvery !== -1 && !(cleanupCounter++ % checkOnEvery)) {
removeOldUrls()
}
result = { isError: false, data: saveUrl(decodeURIComponent(req.url.slice(9))), errMsg: null }
} catch (err) {
code = err.code === undefined ? 500 : err.code
result = { isError: true, data: null, errMsg: typeof err.message === 'undefined' ? err : err.message }
console.log(err)
}
if (!res.writableEnded) {
res.writeHead(code, { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
}
} else {
// redirect
const url = getCodeUrl(req.url.slice(1))
res.writeHead(302, { 'Location': url ?? getConfig().redirectOnError })
res.end()
}
res.end()
}).listen(getConfig().server.port)