Commit a0f1dc41 authored by Markus Opolka's avatar Markus Opolka
Browse files

Add better error handling and remove console timing

parent 2f9906c5
......@@ -141,10 +141,13 @@ app.use(function (req, res, next) {
app.use('/', express.static('dist'))
app.get('/api/search', function (req, res) {
app.get('/api/search/:letter?', function (req, res, next) {
// Search Endpoint with query parameters
// <element attribute="value">content</element>
console.time('> Search All')
if (!req.query.element){
res.send({})
return next('No Query provided')
}
let query = {
element: req.query.element,
......@@ -152,7 +155,15 @@ app.get('/api/search', function (req, res) {
}
let data = []
for (let name of cache.keys()) {
let documents = []
if (req.params.letter) {
documents.push(req.params.letter + '.xml')
} else {
documents = cache.keys()
}
for (let name of documents) {
let obj = loadDocument(name)
if (obj.valid) {
let results = lib.search(obj, query)
......@@ -163,12 +174,15 @@ app.get('/api/search', function (req, res) {
}
res.send(data)
console.timeEnd('> Search All')
})
app.get('/api/features/:letter?', function (req, res) {
// Feature Search Endpoint with query parameters
// <feature category="diakritika" type="akut" subtype="akutstattgravis" ref="26">
if (!req.query.category){
res.send({})
return next('No Query provided')
}
let query = {
category: req.query.category,
......@@ -179,8 +193,6 @@ app.get('/api/features/:letter?', function (req, res) {
let data = []
let documents = []
console.time('> Search Features')
if (req.params.letter) {
documents.push(req.params.letter + '.xml')
} else {
......@@ -198,38 +210,28 @@ app.get('/api/features/:letter?', function (req, res) {
}
res.send(data.sort(lib.compare))
console.timeEnd('> Search Features')
})
app.get('/api/letters/:letter?', function (req, res) {
// Document Endpoint with optional Document name
// Lists either all or one given document
const DOC = req.params.letter + '.xml'
if (req.params.letter) {
console.time('> List Letter')
const DOC = req.params.letter + '.xml'
res.send(loadDocument(DOC).object)
console.timeEnd('> List Letter')
} else {
console.time('> List Letters')
res.send(loadDocuments(DIR))
console.timeEnd('> List Letters')
}
})
app.get('/api/metadata/table', function (req, res) {
// Document Metadata Endpoint in Table Format
console.time('> Table Letters')
res.send(lib.table(loadDocuments(DIR)))
console.timeEnd('> Table Letters')
})
// Where the magic happens
// Loading all documents to cache
loadDocumentsToCache(DIR)
// Where the magic happens
app.listen(PORT, function () {
console.log('> Listening at http://localhost:' + PORT)
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment