Created
April 23, 2019 08:52
-
-
Save AndrejMitrovic/73a6c256155bf90ea6d3593334a3d662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import vibe.vibe; | |
import std.stdio; | |
class Room | |
{ | |
/// id of this room | |
private string id; | |
/// archive of all messages | |
/// note: must be public to be exposed to the diet template | |
public string[] messages; | |
public this (string id) | |
{ | |
this.id = id; | |
} | |
public void addMessage (string name, string message) | |
{ | |
string encoded_msg = format("%s: %s", name, message); | |
writefln("[%s] archived message: %s", this.id, encoded_msg); | |
this.messages ~= encoded_msg; | |
} | |
} | |
/// note: mapped as getApiCall => GET /api_call | |
/// note: parameter names are also encoded and checked at compile-time | |
/// when compiling diet templates! | |
/// | |
/// See also: http://vibed.org/api/vibe.web.web/registerWebInterface | |
class WebChat | |
{ | |
/// map of all the rooms | |
private Room[string] rooms; | |
/// automatically mapped to GET | |
public void get () | |
{ | |
render!"index.dt"; | |
} | |
/// mapped to GET /room | |
public void getRoom (string id, string name) | |
{ | |
render!("room.dt", id, name); | |
} | |
/// mapped to POST /room | |
public void postRoom (string id, string name, string message) | |
{ | |
// add the message in the (possibly newly created) room | |
if (message.length > 0) | |
this.getOrCreateRoom(id).addMessage(name, message); | |
// redirect back to the room page | |
string redirected_url = format("room?id=%s&name=%s", | |
id.urlEncode(), name.urlEncode()); | |
.redirect(redirected_url); | |
} | |
// not part of the public API | |
private Room getOrCreateRoom (string id) | |
{ | |
if (auto room = id in rooms) | |
return *room; | |
return this.rooms[id] = new Room(id); | |
} | |
} | |
void main () | |
{ | |
// our actual web API | |
auto web_chat = new WebChat; | |
// the router will match incoming HTTP requests to the proper routes | |
auto router = new URLRouter; | |
// registers each method of WebChat in the router | |
router.registerWebInterface(web_chat); | |
// match incoming requests to files in the public/ folder | |
router.get("*", serveStaticFiles("public/")); | |
auto settings = new HTTPServerSettings; | |
settings.bindAddresses = ["::1", "127.0.0.1"]; | |
settings.port = 8080; | |
// for production installations, the error stack trace option should | |
// stay disabled, because it can leak internal address information to | |
// an attacker. However, we'll let keep it enabled during development | |
// as a convenient debugging facility. | |
//settings.options &= ~HTTPServerOption.errorStackTraces; | |
listenHTTP(settings, router); | |
logInfo("Open http://%s:%s/ in your browser", settings.bindAddresses.back, | |
settings.port); | |
runApplication(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment