diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-06-25 11:29:30 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-06-25 11:29:30 -0400 |
commit | 62b04185e7938f10232fdb199c2faffb6b87fb3c (patch) | |
tree | 72bd2b995c0280ab84d6edded17f204d13f869eb | |
download | jdtalk_web-62b04185e7938f10232fdb199c2faffb6b87fb3c.tar.gz |
Initial commit
-rw-r--r-- | .gitignore | 16 | ||||
-rw-r--r-- | dub.sdl | 7 | ||||
-rw-r--r-- | source/app.d | 80 |
3 files changed, 103 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5c5d83 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +.dub +dub.selections.json +docs.json +__dummy.html +docs/ +/jdtalk_web +jdtalk_web.so +jdtalk_web.dylib +jdtalk_web.dll +jdtalk_web.a +jdtalk_web.lib +jdtalk_web-test-* +*.exe +*.o +*.obj +*.lst @@ -0,0 +1,7 @@ +name "jdtalk_web" +description "A minimal D application." +authors "Joseph Hunkeler" +copyright "Copyright © 2019, Joseph Hunkeler" +license "BSD 3-clause" +dependency "vibe-d:http" version="~>0.8.0" +dependency "jdtalk:core" version="*" diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..9c603d5 --- /dev/null +++ b/source/app.d @@ -0,0 +1,80 @@ +module app; + +import jdtalk.core; +import vibe.core.core : runApplication; +import vibe.http.server; +import vibe.http.router; +import std.string; +import std.process : environment; +import std.conv : to; + +enum MAX_LIMIT = 100_000; +enum MAX_LIMIT_PATTERN = 100; +__gshared string dataRoot; +__gshared dict_t dicts; + +void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res) +{ + if (req.path == "/") { + long i = 0; + auto limit = req.query.get("limit", "1").to!long; + auto pattern = req.query.get("pattern", null); + auto exactMatch = req.query.get("exact", "false").to!bool; + + if (pattern is null && limit > MAX_LIMIT) { + res.bodyWriter.write(format("Requested too many: %d (MAX: %d)\n", + limit, MAX_LIMIT)); + return; + } + else if (pattern !is null && limit > MAX_LIMIT_PATTERN) { + res.bodyWriter.write(format("Requested too many for pattern: %d (MAX: %d)\n", + limit, MAX_LIMIT_PATTERN)); + return; + } + + if (pattern !is null && !searchDict(dicts, pattern)) { + res.bodyWriter.write(format("Word not available in dictionary: %s\n", pattern)); + return; + } + + while(true) { + string output = talk(dicts); + + if (pattern !is null) { + if (exactMatch && !hasWord(pattern, output)) { + continue; + } + else if (!exactMatch && !output.canFind(pattern)) { + continue; + } + } + + res.bodyWriter.write(format("%s\n", output)); + + if (limit > 0) { + i++; + if (i == limit) + break; + } + } + } +} + + +int main(string[] args) +{ + dataRoot = environment["JDTALK_DATA"]; + dicts = getData(dataRoot); + + auto routes = new URLRouter; + routes.get("/", &handleRequest); + + auto settings = new HTTPServerSettings; + settings.port = 8080; + settings.bindAddresses = ["::1", "127.0.0.1"]; + auto l = listenHTTP(settings, routes); + scope (exit) l.stopListening(); + + runApplication(); + return 0; +} |