removed forgotten files
This commit is contained in:
parent
40f438852f
commit
59ac47c555
5 changed files with 0 additions and 300 deletions
31
source/app.d
31
source/app.d
|
@ -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.");
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue