aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/SearchManager.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-10-22 19:39:19 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-10-22 19:39:19 -0400
commitcee55b4a1b13909d55708eee6c364206ae7c0d4f (patch)
tree859fa90363abfe93a457f74f73e04ddf4c56bfe0 /src/server/ApiManagers/SearchManager.ts
parentf42620a33f76f8fdcf7417498f3fb2c1588c064d (diff)
api managers and web socket initial refactoring
Diffstat (limited to 'src/server/ApiManagers/SearchManager.ts')
-rw-r--r--src/server/ApiManagers/SearchManager.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/server/ApiManagers/SearchManager.ts b/src/server/ApiManagers/SearchManager.ts
new file mode 100644
index 000000000..15b87204c
--- /dev/null
+++ b/src/server/ApiManagers/SearchManager.ts
@@ -0,0 +1,49 @@
+import ApiManager from "./ApiManager";
+import RouteManager, { Method } from "../RouteManager";
+import { Search } from "../Search";
+var findInFiles = require('find-in-files');
+import * as path from 'path';
+import { uploadDirectory } from "..";
+
+export default class SearchManager extends ApiManager {
+
+ public register(router: RouteManager): void {
+
+ router.addSupervisedRoute({
+ method: Method.GET,
+ subscription: "/textsearch",
+ onValidation: async ({ req, res }) => {
+ let q = req.query.q;
+ if (q === undefined) {
+ res.send([]);
+ return;
+ }
+ let results = await findInFiles.find({ 'term': q, 'flags': 'ig' }, uploadDirectory + "text", ".txt$");
+ let resObj: { ids: string[], numFound: number, lines: string[] } = { ids: [], numFound: 0, lines: [] };
+ for (var result in results) {
+ resObj.ids.push(path.basename(result, ".txt").replace(/upload_/, ""));
+ resObj.lines.push(results[result].line);
+ resObj.numFound++;
+ }
+ res.send(resObj);
+ }
+ });
+
+ router.addSupervisedRoute({
+ method: Method.GET,
+ subscription: "/search",
+ onValidation: async ({ req, res }) => {
+ const solrQuery: any = {};
+ ["q", "fq", "start", "rows", "hl", "hl.fl"].forEach(key => solrQuery[key] = req.query[key]);
+ if (solrQuery.q === undefined) {
+ res.send([]);
+ return;
+ }
+ let results = await Search.Instance.search(solrQuery);
+ res.send(results);
+ }
+ });
+
+ }
+
+} \ No newline at end of file