This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (145 loc) · 4.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const sqlite3 = require("sqlite3").verbose();
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const rosetta_code = require("./rosetta-code-snippets.json");
const algorithms_code = require("./the-algorithms-snippets.json");
const app = express();
app.use(express.json());
app.use(cors());
app.options("*", cors());
app.use(morgan("tiny"));
let db = new sqlite3.Database("./snippets.db", err => {
if (err) {
console.error(err.message);
}
console.log("Connected to the snippets database.");
});
app.get("/api/code", (req, res) => {
const query = req.query.searchQuery;
let [algorithm, language] = query.split(" in ");
let response;
if (!language) {
language = "javascript";
}
try {
//check first if code snippets for the language and algorithm are available in the rosetta code file
if (algorithms_code[language] === undefined || algorithms_code[language][algorithm] === undefined) {
response = {
algorithm,
code: rosetta_code[algorithm][language],
language,
source: "http://www.rosettacode.org/wiki/Rosetta_Code",
name: "Rosetta code",
};
} else {
//If not , use theTheAlgorithms json file
response = {
algorithm,
code: algorithms_code[language][algorithm],
language,
source: "https://the-algorithms.com",
name: "The Algorithm",
};
}
res.status(200).send(response);
} catch (err) {
res.status(404).send({res: "Query Not found"});
}
});
app.get("/createTable", (req, res) => {
try {
db.run(
"CREATE TABLE IF NOT EXISTS snippets (algorithm varchar(255),language varchar(255),snippet varchar,source varchar,name varchar)"
);
res.send({status: "Table Created"}).status(200);
} catch (err) {
res.status(400).send({error: err.message});
}
});
app.get("/algorithmsCode", (req, res) => {
try {
function fill_db() {
for (lang of Object.keys(algorithms_code)) {
for (algo of Object.keys(algorithms_code[lang])) {
db.run("INSERT INTO snippets(algorithm,language,snippet,source,name) values(?,?,?,?,?)", [
algo,
lang,
algorithms_code[lang][algo],
"https://the-algorithms.com",
"The Algorithm",
]);
}
}
}
fill_db();
res.send({status: "algorithms code data filled"}).status(200);
} catch (err) {
res.status(400).send({error: err.message});
}
});
app.get("/rosettaCode", (req, res) => {
try {
function fill_db() {
for (algo of Object.keys(rosetta_code)) {
for (lang of Object.keys(rosetta_code[algo])) {
db.run("INSERT INTO snippets(algorithm,language,snippet,source,name) values(?,?,?,?,?)", [
algo,
lang,
rosetta_code[algo][lang],
"http://www.rosettacode.org/wiki/Rosetta_Code",
"Rosetta code",
]);
}
}
}
fill_db();
res.send({status: "Rosetta code data filled"}).status(200);
} catch (err) {
res.status(400).send({error: err.message});
}
});
app.get("/api/sql", (req, res) => {
try {
const query = req.query.searchQuery;
let [algorithm, language] = query.split(" in ");
if (!language) {
language = "javascript";
}
console.log(algorithm, language);
db.all("SELECT * FROM snippets WHERE algorithm=? and language=?", [algorithm, language], (err, rows) => {
db.all("SELECT language FROM snippets WHERE algorithm=?", [algorithm], (err, lang_row) => {
let languages = [];
lang_row.forEach(e => {
languages.push(e.language);
});
res.send({
code: rows[0].snippet,
source: rows[0].source,
name: rows[0].name,
algorithm,
language,
languages,
});
});
});
} catch (err) {
res.send({Error: err.message}).status(401);
}
});
app.get("/createIndex", (req, res) => {
try {
db.run("CREATE INDEX idx_algorithm ON snippets (algorithm)");
res.status(200).send({status: "Index created"});
} catch (err) {
res.status(400).send({error: err.message});
}
});
PORT = process.env.port || process.env.PORT || 5000;
app.listen(PORT, (err, res) => {
if (err) {
console.log(err);
} else {
console.log(`SERVER RUNNING ON PORT ${PORT}`);
}
});