-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (61 loc) · 2.3 KB
/
index.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
#!/usr/bin/env node
const package = require('./package.json')
const assert = require('assert')
const path = require('path')
const argparse = require('argparse')
const express = require('express')
const cors = require('cors')
const load = require('./src/load')
const h2a = require('./src/hash-to-array')
const save = require('./src/save')
const query = require('./src/query')
const set = require('./src/set')
const sitepath = path.join(__dirname, 'srv', 'public')
const port = 2122
function main() {
let args = parse()
let filepath = args.file
let reduction = load(filepath)
let app = express()
app.use(cors())
app.use(express.json())
app.use(express.static(sitepath))
app.get('/api/words', function(req, res) {
let words = reduction.map(function(entry) {
let { chapter, verse, reduction, translation } = entry
return reduction.map(function(word, index) {
return {
chapter_id: chapter,
verse_id: verse,
word_id: index,
from: word,
to: translation[index]
}
}).filter(entry => entry.from)
}).flat()
res.send(words)
})
app.post('/api/chapters/:chapter_id/verses/:verse_id/words/:word_id', function(req, res) {
let { chapter_id, verse_id, word_id } = req.params
let input = req.body
let chapter = query.chapter(reduction, chapter_id)
let verse = query.verse(chapter, verse_id)
let word = query.word(verse, word_id)
input.to = input.to.trim().toUpperCase()
assert.equal(input.from, word, `Invalid 'from' value: ${input.from}`)
set.translation(verse, word_id, input.to)
res.send(input)
save(filepath, reduction)
})
app.listen(port, function() {
console.log('Listening on port', port)
})
}
function parse() {
let { description, version } = package
let parser = new argparse.ArgumentParser({ description })
parser.add_argument('-V', '--version', { help: 'show version information and exit', action: 'version', version })
parser.add_argument('file', { help: 'reduction file as input' })
return parser.parse_args()
}
main()