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
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
.dub
|
||||
docs.json
|
||||
__dummy.html
|
||||
*.o
|
||||
*.obj
|
||||
6
dub.sdl
Normal file
6
dub.sdl
Normal file
|
|
@ -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"
|
||||
11
dub.selections.json
Normal file
11
dub.selections.json
Normal file
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
6
views/hello.dt
Normal file
6
views/hello.dt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
doctype html
|
||||
html
|
||||
head
|
||||
title HELLO
|
||||
body
|
||||
h1 HELLO
|
||||
15
views/index.dt
Normal file
15
views/index.dt
Normal file
|
|
@ -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
|
||||
18
views/room.dt
Normal file
18
views/room.dt
Normal file
|
|
@ -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)
|
||||
|
||||
Loading…
Add table
Reference in a new issue