summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Dockerfile26
-rw-r--r--modules/abbrgen/abbreviation_gen.cpp58
-rw-r--r--package.json2
3 files changed, 82 insertions, 4 deletions
diff --git a/Dockerfile b/Dockerfile
index 2b38630..ad2bb69 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,9 +1,29 @@
-FROM alpine:3.11
+FROM alpine:3.11 AS builder
-RUN apk add --no-cache nodejs npm
+RUN apk add --no-cache npm g++
WORKDIR /webserver
+
+# Get a word list for the abbrgen module
+RUN wget 'http://app.aspell.net/create?max_size=60&spelling=GBs&max_variant=0&diacritic=strip&special=hacker&download=wordlist&encoding=utf-8&format=inline' -O words && \
+ printf "1,/---/ d\nw\nq\n" | ed words >/dev/null
+
COPY . .
-RUN npm install
+# The --unsafe-perm flag is to allow building the abbreviation_gen executable
+RUN npm install --unsafe-perm
+
+
+#####
+
+FROM alpine:3.11 AS runner
+
+RUN apk add --no-cache nodejs
+
+WORKDIR /webserver
+COPY --from=builder /webserver /webserver
+
+# Move the word list to where abbreviation_gen expects it
+RUN mkdir -p /usr/share/dict && \
+ mv /webserver/words /usr/share/dict/words
ENTRYPOINT ["./webserver.js"]
diff --git a/modules/abbrgen/abbreviation_gen.cpp b/modules/abbrgen/abbreviation_gen.cpp
new file mode 100644
index 0000000..708bfdf
--- /dev/null
+++ b/modules/abbrgen/abbreviation_gen.cpp
@@ -0,0 +1,58 @@
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <cstring>
+#include <cctype>
+#include <sys/time.h>
+
+using namespace std;
+
+int main(int argc, char **argv) {
+ if (argc != 2 && argc != 3) {
+ cout << "Usage: " << argv[0] << " <abbreviation> [number of answers]" << endl;
+ return 1;
+ }
+
+ char *abbr = argv[1];
+ int abbrlen = strlen(abbr);
+ transform(abbr, abbr+abbrlen, abbr, ::tolower);
+ for (int i = 0; i < abbrlen; i++) {
+ if (abbr[i] < 'a' || abbr[i] > 'z') {
+ cout << "Abbreviation cannot contain '" << abbr[i] << "'!" << endl;
+ return 1;
+ }
+ }
+
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ srand(1000000 * tv.tv_sec + tv.tv_usec);
+ int numanswers = 1;
+ if (argc == 3) {
+ numanswers = strtol(argv[2], nullptr, 10);
+ if (numanswers <= 0) return 0;
+ }
+
+ vector<string> dict[26];
+ ifstream dictfile("/usr/share/dict/words");
+ string line;
+
+ while (getline(dictfile, line)) {
+ transform(line.begin(), line.end(), line.begin(), ::tolower);
+ if (line[0] >= 'a' && line[0] <= 'z' && all_of(line.begin(), line.end(), ::islower))
+ dict[line[0] - 'a'].push_back(line);
+ }
+
+ while (numanswers --> 0) {
+ for (int i = 0; i < abbrlen; i++) {
+ if (i > 0) cout << ' ';
+ size_t index = rand() % dict[abbr[i] - 'a'].size();
+ cout << dict[abbr[i] - 'a'][index];
+ }
+ cout << '\n';
+ }
+ cout.flush();
+
+ return 0;
+}
diff --git a/package.json b/package.json
index c6d7d65..08b9de9 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,7 @@
"url": "https://github.com/tomsmeding/webserver"
},
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
+ "prepare": "g++ -Wall -Wextra -std=c++11 -O3 -o modules/abbrgen/abbreviation_gen_$(uname) modules/abbrgen/abbreviation_gen.cpp"
},
"author": "Tom Smeding <tom.smeding@gmail.com> (http://tomsmeding.com)",
"license": "MIT"