-
Notifications
You must be signed in to change notification settings - Fork 4
/
unshrtn.js
executable file
·97 lines (87 loc) · 2.36 KB
/
unshrtn.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
#!/usr/bin/env node
let URL = require('url')
let jsdom = require('jsdom')
let level = require('level')
let stream = require('stream')
let express = require('express')
let request = require('request')
let metaweb = require('metaweb')
let winston = require('winston')
const logger = new winston.Logger()
const env = process.env.NODE_ENV
if (env === 'development') {
logger.add(winston.transports.Console, {level: 'debug'})
} else if (env === 'test') {
const logFile = path.join(__dirname, '..', '..', 'test.log')
logger.add(winston.transports.File, {filename: logFile})
} else if (env === 'production') {
const logFile = path.join(__dirname, '..', '..', 'app.log')
logger.add(winston.transports.File, {filename: logFile})
}
let app = express()
app.set('json spaces', 2)
let db = level('./unshrtndb')
process.setMaxListeners(10)
app.get("/", (req, resp) => {
let short = req.query.url
let sent = false
if (short) {
return lookup(short, (error, result) => {
if (error) {
logger.error(`Error fetching ${short}: ${error}`)
try {
result = {error}
if (! sent) {
sent = true
return resp.json(result)
}
} catch (error1) {
error = error1
return logger.error(`unable to send response: ${error}`)
}
} else if (! sent) {
sent = true
logger.info(`${result.short} -> ${result.long}`)
return resp.json(result)
}
})
} else if (! sent) {
sent = true
return resp.status(400).json({
error: "please supply url query parameter"
})
}
})
var lookup = (url, next) => {
if (url === null) {
next("url cannot be null", null)
return
}
short = String(url)
return db.get(url, (err, s) => {
if (!err) {
return next(null, JSON.parse(s))
} else {
metaweb.get(short)
.then((result) => {
result.short = url
result.long = result.url
delete result.url
db.put(short, JSON.stringify(result), (e) => {
if (e) {
logger.error("unable to write to leveldb:", e)
}
})
return next(null, result)
})
.catch((err) => {
logger.error("unable to get url: ", url)
next(err, null)
})
}
})
}
if (require.main === module) {
app.listen(3000)
}
exports.app = app