-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
71 lines (55 loc) · 1.96 KB
/
server.ts
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
import { memoryUsage } from "./utils/memory-usage";
import { TrieSearch } from "./trie/trie-search";
import { isProd, readDictionary } from "./utils/read-dictionary";
import fastify from "fastify";
import { Trie } from "./trie/trie";
import { TrieSolve } from "./trie/trie-solve";
import path from "path";
type Prefix = { prefix: string };
type Suffix = { suffix: string };
type Keyword = { keyword: string };
type Depth = { depth?: string };
type Solve = { row: string[]; hand: string[] };
const server = fastify({ logger: true });
const frontendPath = isProd()
? path.join(__dirname, "..", "frontend", "build")
: path.join(__dirname, "frontend", "build");
server.register(require("fastify-cors"));
server.register(require("fastify-static"), {
root: frontendPath,
});
const trie = new Trie();
const trieSearch = new TrieSearch(trie);
server.get("/startsWith", async (request, reply) => {
const { prefix, depth } = request.query as Prefix & Depth;
return trieSearch.startsWith(prefix, depth ? +depth : undefined);
});
server.get("/endsWith", async (request, reply) => {
const { suffix, depth } = request.query as Suffix & Depth;
return trieSearch.endsWith(suffix, depth ? +depth : undefined);
});
server.get("/contains", async (request, reply) => {
const { keyword } = request.query as Keyword;
return trieSearch.contains(keyword);
});
server.get("/between", async (request, reply) => {
const { prefix, suffix } = request.query as Prefix & Suffix;
return trieSearch.between(prefix, suffix);
});
server.post("/solve", async (request, reply) => {
const { row, hand } = request.body as Solve;
const words = TrieSolve.solveForRow(trie, row, hand);
return words;
});
const start = async () => {
try {
readDictionary(trie);
memoryUsage();
console.log("Starting server at port: " + process.env.PORT || 5100);
await server.listen(process.env.PORT || 5100, "0.0.0.0");
} catch (err) {
server.log.error(err);
process.exit(1);
}
};
start();