Skip to content

Instantly share code, notes, and snippets.

@kokes
Created March 24, 2016 18:27
Show Gist options
  • Save kokes/e409fc10358816e2bdc0 to your computer and use it in GitHub Desktop.
Save kokes/e409fc10358816e2bdc0 to your computer and use it in GitHub Desktop.
hledani pomoci onoho indexu
var lunr = require('lunr')
var jf = require('jsonfile')
var http = require('http');
var pokryti = jf.readFileSync('pokryti.json')
var cache = {}
var soubor = function(id) {
// pomalejsi nez hash mapa, ale pro par souboru to nevadi
for (var j in pokryti) {
if (id <= pokryti[j].lid) return j
}
}
var projev = function(id) {
var sb = soubor(id)
if (!cache.hasOwnProperty(sb)) cache[sb] = jf.readFileSync(sb)
return cache[sb][id.toFixed()]
}
var idx = lunr.Index.load(jf.readFileSync('index.json'))
http.createServer(function (req, res) {
var parts = req.url.split('/')
if (parts[1] != "hledej") return // TODO: asi poslat nejakej err
if (parts.length != 3) return // opet, err
var q = parts[2].trim()
var ret = {} // se bude posilat
var vysl = idx.search(q)
ret.meta = {
'q': q,
'vysledku': Object.keys(vysl).length
}
ret.data = {}
for (var j in vysl) {
// console.log(vysl[j].ref)
ret.data[vysl[j].ref.toFixed()] = projev(vysl[j].ref)
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(ret));
}).listen(3000, 'localhost');
console.log('Server at http://localhost:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment