/
app.js
126 lines (112 loc) · 4.11 KB
/
app.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
121
122
123
124
const { Stack, inToPost, outPrec, inPrec, isOperator, isOperand, spaceSeparator, evaluatePostfix } = require('./Stack')
const { Stock, registerProduct } = require('./helper')
const express = require('express')
const app = express()
app.set('port', process.env.PORT || 8080)
app.set('host', process.env.HOST || '13.115.100.160')
app.get('/', function(req, res) {
res.send('AMAZON\n')
})
app.get('/secret', function(req, res) {
const serverType = res.getHeaders()['x-powered-by']
const authFailMessage = `<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>${serverType} (Amazon) Server at ${app.settings.host} Port ${app.settings.port}</address>
</body></html>`
const auth = { login: 'amazon', password: 'candidate' }
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')
if (login && password && login === auth.login && password === auth.password)
res.send('SUCCESS\n')
else
res.status(401).send(authFailMessage)
})
app.get('/calc', function(req, res) {
const encodedQuery = encodeURIComponent(Object.keys(req.query))
const decodedQuery = decodeURIComponent(encodedQuery.replace(/\%20/g, '+'))
const regex = new RegExp('[-0-9()+*/]', 'gy')
const regexNeg = new RegExp('^(-[0-9]+)', 'gm')
const regexNegMatch = decodedQuery.match(regexNeg)
function regexNegFormatter(arr) {
const negInt = arr[0]
return decodedQuery.replace(regexNeg, `(0${negInt})`)
}
if (!regex.test(decodedQuery))
res.send('ERROR\n')
else {
let spaceSeparatedInfix = spaceSeparator((regexNegMatch) ? regexNegFormatter(regexNegMatch) : decodedQuery)
let parsedInfix = spaceSeparatedInfix.filter(i => i)
let postfix = inToPost(parsedInfix)
let result = evaluatePostfix(postfix)
res.send(`${result}\n`)
}
})
app.get('/stocker', function(req, res) {
// parse req query
const parameter = {}
for (let [key, value] of Object.entries(req.query))
parameter[key] = value
if (parameter.function === 'deleteall') {
stockTable = new Stock()
res.end()
} else if (parameter.function === 'addstock') {
// + sign converts string into a number
if (!parameter.name || (parameter.amount && !(Number.isInteger(+parameter.amount))))
res.send('ERROR\n')
else {
if (!(parameter.name in stockTable.names))
stockTable.names = Object.assign(registerProduct(parameter.name, parameter.amount),stockTable.names)
else {
if (!parameter.amount)
stockTable.names[parameter.name].amount += 1
else
stockTable.names[parameter.name].amount += parameter.amount
}
res.end()
}
} else if (parameter.function === 'checkstock') {
if (!parameter.name) {
// send all product list sorted by alpha
Object.keys(stockTable.names).sort().forEach(key => {
if (stockTable.names[key].amount != 0)
res.write(`${key}: ${stockTable.names[key].amount}\n`)
})
res.end()
} else {
if (!(parameter.name in stockTable.names))
res.send(`${parameter.name}: 0\n`)
else {
res.send(`${parameter.name}: ${stockTable.names[parameter.name].amount}\n`)
}
}
} else if (parameter.function === 'sell') {
if (!parameter.name)
res.send('ERROR\n')
else if (parameter.price && parameter.price <= 0)
res.send('ERROR\n')
else if (parameter.price)
stockTable.addSales(parameter.price, parameter.amount)
else
stockTable.names[parameter.name].amount -= (parameter.amount) ? parameter.amount : 1
res.end()
} else if (parameter.function === 'checksales') {
if (!(Number.isInteger(stockTable.sales)))
res.send(`sales: ${stockTable.sales.toFixed(2)}\n`).end()
else
res.send(`sales: ${stockTable.sales}\n`).end()
} else
res.send('ERROR\n')
})
app.listen(app.get('port'), function() {
console.log(`listening to ${app.settings.host} at port ${app.settings.port}...`)
})