Test vibe.d initial commit.
- Now ready for tutorial section: "Incremental updates"
This commit is contained in:
commit
04d90b7c7a
7 changed files with 135 additions and 0 deletions
74
source/app.d
Normal file
74
source/app.d
Normal file
|
|
@ -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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue