use the webframework

This commit is contained in:
Johannes Loher 2017-08-05 17:31:35 +02:00
parent 88cf802e4b
commit 6cf7d27115

View file

@ -1,54 +1,16 @@
import vibe.vibe;
import std.datetime.date; import std.datetime.date;
shared static this() import vibe.vibe;
class CalendarWebapp
{ {
auto router = new URLRouter; private:
router.get("/", &listEvents); immutable fileName = Path("events.json");
router.get("/create", staticTemplate!"create.dt");
router.get("/event/create", &createEvent);
router.post("/event/create", &createEvent);
router.get("*", serveStaticFiles("/public"));
auto settings = new HTTPServerSettings; struct Event
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:8080/ in your browser.");
}
void listEvents(HTTPServerRequest req, HTTPServerResponse res)
{ {
auto fileName = Path("events.json"); string name, place;
DateTime begin, end;
auto events = fileName.getEventsFromFile;
render!("listevents.dt", events)(res);
}
void createEvent(HTTPServerRequest req, HTTPServerResponse res)
{
if (req.method != HTTPMethod.POST && req.method != HTTPMethod.GET)
return;
auto formdata = (req.method == HTTPMethod.POST) ? &req.form : &req.query;
auto fileName = Path("events.json");
Event event;
event.name = formdata.get("Ereignisname");
event.place = formdata.get("Ereignisort");
event.begin = DateTime.fromISOExtString(formdata.get("Von") ~ ":00");
event.end = DateTime.fromISOExtString(formdata.get("Bis") ~ ":00");
auto events = fileName.getEventsFromFile;
events ~= event;
writeFileUTF8(fileName, events.serializeToJsonString());
render!("listevents.dt", events)(res);
} }
Event[] getEventsFromFile(in Path fileName) Event[] getEventsFromFile(in Path fileName)
@ -60,12 +22,57 @@ Event[] getEventsFromFile(in Path fileName)
deserializeJson(events, eventsString.parseJsonString); deserializeJson(events, eventsString.parseJsonString);
} }
catch (std.json.JSONException) catch (std.json.JSONException)
{} {
}
return events; return events;
} }
struct Event public:
@method(HTTPMethod.POST) @path("/event/create")
void createEvent(string Ereignisname, string Ereignisort, string Von, string Bis)
{ {
string name, place; Event event;
DateTime begin, end;
event.name = Ereignisname;
event.place = Ereignisort;
event.begin = DateTime.fromISOExtString(Von ~ ":00");
event.end = DateTime.fromISOExtString(Bis ~ ":00");
enforce(event.end - event.begin > 0.seconds);
auto events = getEventsFromFile(fileName);
events ~= event;
writeFileUTF8(fileName, events.serializeToJsonString());
render!("listevents.dt", events);
}
@method(HTTPMethod.GET) @path("create")
void newEvent()
{
render!("create.dt");
}
void index()
{
auto events = getEventsFromFile(fileName);
render!("listevents.dt", events);
}
}
shared static this()
{
auto router = new URLRouter;
router.registerWebInterface(new CalendarWebapp);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:8080/ in your browser.");
} }