Skip to content

Commit

Permalink
Merge pull request #1 from trishume/dlang
Browse files Browse the repository at this point in the history
Dlang
  • Loading branch information
trishume committed Jan 22, 2016
2 parents bbe2982 + af6ca91 commit bda15a6
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 191 deletions.
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.tern-port
server
nimcache
data
lib/graph
jester
.dub
docs.json
__dummy.html
*.o
*.obj
ratewithscience
__test__library__
11 changes: 0 additions & 11 deletions Rakefile

This file was deleted.

18 changes: 14 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A web app for finding a rating scale between two arbitrary things.
Ever wanted to rate something on a scale of David Hasselhoff to the Eiffel Tower? Well now you can.

[Dave Pagurek](http://davepagurek.com/) and I made this for the [TerribleHack Winter 2015](http://terriblehack.website/) hackathon. I wrote the backend, Dave wrote the frontend.
I later rewrote the backend in [Nim](http://nim-lang.org/) to make it cleaner and easier to deploy.
I later rewrote the backend in [Nim](http://nim-lang.org/) and then [D](http://dlang.org) to make it cleaner and easier to deploy as well as learn new languages.

Try it out at [http://ratewith.science](http://ratewith.science)!

Expand All @@ -21,16 +21,24 @@ Dave's JS sends my backend two endpoints via AJAX and my backend gives back a li
## The Current Tech

The frontend is written in static HTML and JS with CSS3 animations and properties for extra fancy design.
The backend was recently rewritten in [Nim](http://nim-lang.org/) by Tristan as a way of learning the language and make the whole thing less hacky than the old Rust version was.
The backend is currently written in [D](http://dlang.org/), which is the third compiled language I (Tristan) have written the same thing in.
Now I have a nice comparison corpus of having implemented the exact same app in Rust, Nim and D. It is a great way to try out a language.

The Nim backend uses the [Jester][] web framework to serve Dave's frontend as well
as respond to API calls for path finding.
The D backend uses the [Vibe.d][] web framework to serve Dave's frontend as well
as respond to API calls for path finding. It was a mostly straightforward translation of the Nim version, but a bit nicer.

First the backend translates the given pages into something usable with the binary graph using the [wikicrush][] `xindex.db` file with Nim's standard Sqlite library.
The backend then performs a breadth-first-search on an in-memory buffer of the [wikicrush][] `indexbi.bin` file.
It first tries to BFS through only bidirectional links as these lead to better paths but if that fails it tries a single-direction search.
Then it uses the Sqlite database to translate the path offsets it got back into article names and ships them back to the frontend as JSON.

The JSON API is generated using [Vibe.d's fancy REST generator][https://vibed.org/api/vibe.web.rest/].

## The Nim Tech

The first rewrite was done in [Nim](http://nim-lang.org/) by Tristan as a way of learning the language and make the whole thing less hacky than the old Rust version was.
The Nim backend used the [Jester][] web framework to serve Dave's frontend as well as respond to API calls for path finding.

## The Original Tech (From the Hackathon)
The backend uses a compiled Rust binary from the [ratews_backend](https://github.com/trishume/ratews_backend) project to find a path.

Expand All @@ -55,3 +63,5 @@ Steps:

[wikicrush]: https://github.com/trishume/wikicrush
[Jester]: https://github.com/dom96/jester
[Vibe.d]: https://vibed.org/
[d2sqlite3]: https://github.com/biozic/d2sqlite3
15 changes: 15 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ratewithscience",
"description": "The backend for http://ratewith.science/",
"copyright": "Copyright © 2016, Tristan Hume",
"authors": ["Tristan Hume"],
"libs": ["sqlite3"],
"lflags": ["-L/usr/local/Cellar/sqlite/3.8.10.2/lib/"],
"dependencies": {
"d2sqlite3": "~>0.9.0",
"dunit": "~>1.0.10",
"gfm:core": "~>3.0.11",
"vibe-d": "~>0.7.26"
},
"versions": ["VibeDefaultMain"]
}
14 changes: 14 additions & 0 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"fileVersion": 1,
"versions": {
"libevent": "2.0.1+2.0.16",
"libev": "5.0.0+4.04",
"openssl": "1.1.4+1.0.1g",
"memutils": "0.4.4",
"vibe-d": "0.7.26",
"d2sqlite3": "0.9.0",
"gfm": "3.0.11",
"libasync": "0.7.5",
"dunit": "1.0.12"
}
}
129 changes: 0 additions & 129 deletions lib/graph.nim

This file was deleted.

2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h2>Rate things on arbitrary scales with scientific accuracy</h2>
<section id="error" class="hidden">
<div class="wrapper">
<h2>Uh oh!</h2>
<p>We couldn't make a scale with your input. Try some other endpoints!</p>
<p id="errormsg">We couldn't make a scale with your input. Try some other endpoints!</p>
<input type="button" id="close_error" value="Try again">
</div>
</section>
Expand Down
16 changes: 12 additions & 4 deletions public/rws.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,31 @@ document.getElementById("go").addEventListener("click", function() {
},
dataType: "json",
success: function(result) {
if (result.status == "ok") {
if (result.status > 2) { // success
var scale = document.getElementById("scale");
scale.classList.remove("quality");
if (result.quality && result.quality == 2) {
if (result.status == 3) {
scale.classList.add("quality");
}
scale.innerHTML = "";
for (var i=0; i<result.scale.length; i++) {
for (var i=0; i<result.path.length; i++) {
var item = "<div class='item'><h3>" + (i+1) + "</h3>"
item += "<a href='http://en.wikipedia.org/wiki/" + encodeURIComponent(result.scale[i]) + "' target='_blanl'>" + result.scale[i] + "</a>"
item += "<a href='http://en.wikipedia.org/wiki/" + encodeURIComponent(result.path[i]) + "' target='_blanl'>" + result.path[i] + "</a>"
item += "<div class='bg'></div>";
item += "</div>";
scale.innerHTML += item;
}
hide(document.getElementById("loading"));
show(document.getElementById("results"));
} else {
var errorMsgEl = document.getElementById("errormsg");
if(result.status == 0) {
errorMsgEl.innerHTML = "There's no path between these pages! This is super rare, you must be good at finding obscure pages.";
} else if(result.status == 1) {
errorMsgEl.innerHTML = "I can't find a Wikipedia page for your start point. This might be because I can't currently use redirects as endpoints.";
} else if(result.status == 2) {
errorMsgEl.innerHTML = "I can't find a Wikipedia page for your end point. This might be because I can't currently use redirects as endpoints.";
}
hide(document.getElementById("loading"));
show(document.getElementById("error"));
}
Expand Down
37 changes: 0 additions & 37 deletions server.nim

This file was deleted.

39 changes: 39 additions & 0 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import std.stdio, std.datetime;
import vibe.d;
import graph;

Graph wikiGraph;
TaskMutex mtx;

@path("/api")
interface Api {
@path("findscale")
graph.Path getFindScale(string start, string stop);
}

class ApiImpl : Api {
override:
graph.Path getFindScale(string start, string stop) {
StopWatch sw = StopWatch(AutoStart.yes);
writeln("Pathing from ", start, " to ", stop);
mtx.lock();
auto res = wikiGraph.rateWithScience(start, stop);
mtx.unlock();
sw.stop();
writeln("Done pathing with result", res, " it took ", sw.peek().msecs, "ms");
return res;
}
}

shared static this() {
wikiGraph = new Graph("data");
mtx = new TaskMutex;

auto router = new URLRouter;
router.registerRestInterface(new ApiImpl());
router.get("*", serveStaticFiles("./public/"));

auto settings = new HTTPServerSettings;
settings.port = 5000;
listenHTTP(settings, router);
}
Loading

0 comments on commit bda15a6

Please sign in to comment.