commit 04d90b7c7ad7eb8b7bbdca256c500ae052a38343 Author: Jonathan Harker Date: Fri Feb 5 16:06:23 2016 +1300 Test vibe.d initial commit. - Now ready for tutorial section: "Incremental updates" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..433d266 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.dub +docs.json +__dummy.html +*.o +*.obj diff --git a/dub.sdl b/dub.sdl new file mode 100644 index 0000000..0f39a6d --- /dev/null +++ b/dub.sdl @@ -0,0 +1,6 @@ +name "webchat" +description "A simple vibe.d server application." +copyright "Copyright © 2016, johnno" +authors "johnno" +dependency "vibe-d" version="~>0.7.23" +versions "VibeDefaultMain" diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..9d33b20 --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,11 @@ +{ + "fileVersion": 1, + "versions": { + "libasync": "0.7.5", + "memutils": "0.4.4", + "openssl": "1.1.4+1.0.1g", + "vibe-d": "0.7.26", + "libevent": "2.0.1+2.0.16", + "libev": "5.0.0+4.04" + } +} diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..ef5798f --- /dev/null +++ b/source/app.d @@ -0,0 +1,74 @@ +/** +Following along the tutorial at +https://vibed.org/blog/posts/a-scalable-chat-room-service-in-d + +More notes at https://wiki.jon.geek.nz/D + +Add the D apt respository in /etc/apt/sources.list.d/dlang.list: + + deb http://netcologne.dl.sourceforge.net/project/d-apt/ d-apt main + +Then get the apt key, and install the things: + + sudo apt-get update + sudo apt-get -y --allow-unauthenticated install --reinstall d-apt-keyring + sudo apt-get install dmd-bin dcd dub +*/ + +import vibe.d; + +final class Room { + string[] messages; + + void addMessage(string nick, string message) { + messages ~= nick ~ ": " ~ message; + } +} + +final class WebChat { + private Room[string] rooms; + + // Uses vibe.d magic method names - in this case, GET / + void get() { + render!("index.dt"); + } + + // Uses vibe.d magic method names - in this case, GET /hello + void getHello() { + render!("hello.dt"); + } + + // GET /room + void getRoom(string room, string nick) { + auto messages = getOrCreateRoom(room).messages; + render!("room.dt", room, nick, messages); + } + + // POST /room + void postRoom(string room, string nick, string message) { + if (message.length) + getOrCreateRoom(room).addMessage(nick, message); + redirect("room?room=" ~ room.urlEncode ~ "&nick=" ~ nick.urlEncode); + } + + private Room getOrCreateRoom(string room) { + if (auto pr = room in rooms) return *pr; + return rooms[room] = new Room; + } +} + +shared static this() +{ + auto router = new URLRouter; + router.registerWebInterface(new WebChat); + + // If nothing else matches, try serving it from public + router.get("*", serveStaticFiles("public/")); + + auto settings = new HTTPServerSettings; + settings.port = 8083; + settings.bindAddresses = ["::1", "127.0.0.1"]; + listenHTTP(settings, router); + + logInfo("Please open http://127.0.0.1:%d/ in your browser.".format(settings.port)); +} diff --git a/views/hello.dt b/views/hello.dt new file mode 100644 index 0000000..76c83e2 --- /dev/null +++ b/views/hello.dt @@ -0,0 +1,6 @@ +doctype html +html + head + title HELLO + body + h1 HELLO diff --git a/views/index.dt b/views/index.dt new file mode 100644 index 0000000..4aeef8a --- /dev/null +++ b/views/index.dt @@ -0,0 +1,15 @@ +doctype html +html + head + title Welcome to Webchat + body + h1 Welcome to Webchat + p Choose a chatroom: + form(action="/room", method="GET") + p + label(for="room") Chat room: + input#room(name="room", type="text", autofocus="true") + p + label(for="nick") Your Name: + input#nick(name="nick", type="text") + button(type="submit") Go diff --git a/views/room.dt b/views/room.dt new file mode 100644 index 0000000..f7d65f5 --- /dev/null +++ b/views/room.dt @@ -0,0 +1,18 @@ +doctype html +html + head + title #{room} - Webchat + style. + textarea, input { width: 100%; } + textarea { resize: vertical; } + body + h1 Room '#{room}' + textarea#history(rows=20, readonly=true) + - foreach (m; messages) + |= m + + form(action="room", method="POST") + input(type="hidden", name="room", value=room) + input(type="hidden", name="nick", value=nick) + input#inputLine(type="text", name="message", autofocus=true) +