From 59ac47c555d24c5516e504b964c641c6b3f1a7ac Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 17 Sep 2017 17:54:26 +0200 Subject: [PATCH] removed forgotten files --- source/app.d | 31 ------------- source/authenticator.d | 30 ------------- source/calendarwebapp.d | 99 ----------------------------------------- source/configuration.d | 61 ------------------------- source/event.d | 79 -------------------------------- 5 files changed, 300 deletions(-) delete mode 100644 source/app.d delete mode 100644 source/authenticator.d delete mode 100644 source/calendarwebapp.d delete mode 100644 source/configuration.d delete mode 100644 source/event.d diff --git a/source/app.d b/source/app.d deleted file mode 100644 index ca618d1..0000000 --- a/source/app.d +++ /dev/null @@ -1,31 +0,0 @@ -module app; - -import calendarwebapp : CalendarWebapp; -import configuration : Context; - -import poodinis; - -import vibe.core.log : logInfo; - -import vibe.http.fileserver : serveStaticFiles; -import vibe.http.router : URLRouter; -import vibe.http.server : HTTPServerSettings, listenHTTP, MemorySessionStore; -import vibe.web.web : registerWebInterface; - -shared static this() -{ - auto container = new shared DependencyContainer(); - container.registerContext!Context; - - auto router = new URLRouter; - router.registerWebInterface(container.resolve!CalendarWebapp); - router.get("*", serveStaticFiles("public")); - - auto settings = new HTTPServerSettings; - settings.port = 8080; - settings.bindAddresses = ["::1", "127.0.0.1"]; - settings.sessionStore = new MemorySessionStore; - listenHTTP(settings, router); - - logInfo("Please open http://127.0.0.1:8080/ in your browser."); -} diff --git a/source/authenticator.d b/source/authenticator.d deleted file mode 100644 index 2e53f91..0000000 --- a/source/authenticator.d +++ /dev/null @@ -1,30 +0,0 @@ -module authenticator; - -import poodinis; - -import vibe.data.bson : Bson; -import vibe.db.mongo.collection : MongoCollection; - -interface Authenticator -{ - bool checkUser(string username, string password) @safe; -} - -class MongoDBAuthenticator : Authenticator -{ -private: - @Value("users") - MongoCollection users; - -public: - bool checkUser(string username, string password) @safe - { - auto result = users.findOne(["username" : username, "password" : password]); - return result != Bson(null); - } -} - -struct AuthInfo -{ - string userName; -} diff --git a/source/calendarwebapp.d b/source/calendarwebapp.d deleted file mode 100644 index 651f5b6..0000000 --- a/source/calendarwebapp.d +++ /dev/null @@ -1,99 +0,0 @@ -module calendarwebapp; - -import authenticator : Authenticator, AuthInfo; - -import core.time : days; - -import event; - -import poodinis; - -import std.datetime.date : Date; -import std.exception : enforce; -import std.typecons : Nullable; - -import vibe.data.bson : BsonObjectID; -import vibe.http.common : HTTPStatusException; -import vibe.http.server : HTTPServerRequest, HTTPServerResponse; -import vibe.http.status : HTTPStatus; -import vibe.web.auth; -import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar, - terminateSession; - -@requiresAuth class CalendarWebapp -{ - @noRoute AuthInfo authenticate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe - { - if (!req.session || !req.session.isKeySet("auth")) - { - redirect("/login"); - return AuthInfo.init; - } - return req.session.get!AuthInfo("auth"); - } - -public: - @anyAuth void index() - { - auto events = eventStore.getAllEvents(); - render!("showevents.dt", events); - } - - @noAuth void getLogin(string _error = null) - { - render!("login.dt", _error); - } - - @noAuth @errorDisplay!getLogin void postLogin(string username, string password) @safe - { - enforce(authenticator.checkUser(username, password), "Benutzername oder Passwort ungültig"); - immutable AuthInfo authInfo = {username}; - auth = authInfo; - redirect("/"); - } - - @anyAuth void getLogout() @safe - { - terminateSession(); - redirect("/"); - } - - @anyAuth void getCreate(ValidationErrorData _error = ValidationErrorData.init) - { - render!("create.dt", _error); - } - - @anyAuth @errorDisplay!getCreate void postCreate(Date begin, - Nullable!Date end, string description, string name, EventType type, bool shout) @safe - { - import std.array : replace, split; - - if (!end.isNull) - enforce(end - begin >= 1.days, - "Mehrtägige Ereignisse müssen mindestens einen Tag dauern"); - auto event = Event(BsonObjectID.generate, begin, end, name, - description.replace("\r", ""), type, shout); - - eventStore.addEvent(event); - - redirect("/"); - } - - @anyAuth void postRemove(BsonObjectID id) @safe - { - eventStore.removeEvent(id); - redirect("/"); - } - -private: - struct ValidationErrorData - { - string msg; - string field; - } - - SessionVar!(AuthInfo, "auth") auth; - - @Autowire EventStore eventStore; - @Autowire Authenticator authenticator; -} diff --git a/source/configuration.d b/source/configuration.d deleted file mode 100644 index fce925d..0000000 --- a/source/configuration.d +++ /dev/null @@ -1,61 +0,0 @@ -module configuration; - -import authenticator : Authenticator, MongoDBAuthenticator; -import calendarwebapp : CalendarWebapp; -import event : EventStore, MongoDBEventStore; - -import poodinis; - -import vibe.db.mongo.client : MongoClient; -import vibe.db.mongo.collection : MongoCollection; -import vibe.db.mongo.mongo : connectMongoDB; - -class Context : ApplicationContext -{ -public: - override void registerDependencies(shared(DependencyContainer) container) - { - auto mongoClient = connectMongoDB("localhost"); - container.register!MongoClient.existingInstance(mongoClient); - container.register!(EventStore, MongoDBEventStore); - container.register!(Authenticator, MongoDBAuthenticator); - container.register!CalendarWebapp; - container.register!(ValueInjector!string, StringInjector); - container.register!(ValueInjector!MongoCollection, MongoCollectionInjector); - } -} - -class StringInjector : ValueInjector!string -{ -private: - string[string] config; - -public: - this() const @safe pure nothrow - { - // dfmt off - config = ["Database name" : "CalendarWebapp", - "Users collection name": "users", - "Events collection name" : "events"]; - // dfmt on - } - - override string get(string key) const @safe pure nothrow - { - return config[key]; - } -} - -class MongoCollectionInjector : ValueInjector!MongoCollection -{ -private: - @Autowire MongoClient mongoClient; - @Value("Database name") - string databaseName; - -public: - override MongoCollection get(string key) @safe - { - return mongoClient.getCollection(databaseName ~ "." ~ key); - } -} diff --git a/source/event.d b/source/event.d deleted file mode 100644 index 5655cb7..0000000 --- a/source/event.d +++ /dev/null @@ -1,79 +0,0 @@ -module event; - -import poodinis; - -import std.algorithm : map; -import std.datetime.date : Date; -import std.range.interfaces : InputRange, inputRangeObject; -import std.typecons : Nullable; - -import vibe.data.bson : Bson, BsonObjectID, deserializeBson, serializeToBson; -import vibe.data.serialization : serializationName = name; -import vibe.db.mongo.collection : MongoCollection; - -interface EventStore -{ - Event getEvent(BsonObjectID id) @safe; - InputRange!Event getAllEvents() @safe; - void addEvent(Event) @safe; - InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe; - void removeEvent(BsonObjectID id) @safe; -} - -class MongoDBEventStore : EventStore -{ -public: - Event getEvent(BsonObjectID id) @safe - { - return events.findOne(["_id" : id]).deserializeBson!Event; - } - - InputRange!Event getAllEvents() @safe - { - return events.find().map!(deserializeBson!Event).inputRangeObject; - } - - void addEvent(Event event) @safe - { - if (!event.id.valid) - event.id = BsonObjectID.generate; - - events.insert(event.serializeToBson); - } - - InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe - { - return events.find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], ["date" - : ["$lte" : end.serializeToBson]]]]).map!(deserializeBson!Event) - .inputRangeObject; - } - - void removeEvent(BsonObjectID id) @safe - { - events.remove(["_id" : id]); - } - -private: - @Value("events") - MongoCollection events; -} - -enum EventType -{ - Holiday, - Birthday, - FSI_Event, - General_University_Event, - Any -} - -struct Event -{ - @serializationName("_id") BsonObjectID id; - @serializationName("date") Date begin; - @serializationName("end_date") Nullable!Date end; - string name; - @serializationName("desc") string description; - @serializationName("etype") EventType type; - bool shout; -}