From a24ab226c2772036121b8b6de60be543ad6c666d Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 24 Jul 2024 16:32:08 -0400 Subject: [PATCH] Added base entity. --- finnow-api/source/app.d | 19 +++++++++++++---- finnow-api/source/model/base.d | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 finnow-api/source/model/base.d diff --git a/finnow-api/source/app.d b/finnow-api/source/app.d index c3eec7f..e5600ec 100644 --- a/finnow-api/source/app.d +++ b/finnow-api/source/app.d @@ -1,6 +1,17 @@ -import std.stdio; +import slf4d; +import handy_httpd; +import handy_httpd.handlers.path_handler; -void main() -{ - writeln("Edit source/app.d to start your project."); +import model.base; + +void main() { + ServerConfig cfg; + cfg.workerPoolSize = 5; + cfg.port = 8080; + PathHandler pathHandler = new PathHandler(); + pathHandler.addMapping(Method.GET, "/status", (ref ctx) { + ctx.response.writeBodyString("online"); + }); + HttpServer server = new HttpServer(pathHandler, cfg); + server.start(); } diff --git a/finnow-api/source/model/base.d b/finnow-api/source/model/base.d new file mode 100644 index 0000000..d5c12ce --- /dev/null +++ b/finnow-api/source/model/base.d @@ -0,0 +1,37 @@ +module model.base; + +/** + * The base class for all persistent entities with a unique integer id. + * It offers some basic utilities like equality and comparison by default. + */ +abstract class IdEntity { + const ulong id; + + this(ulong id) { + this.id = id; + } + + override bool opEquals(Object other) const { + if (IdEntity e = cast(IdEntity) other) { + return e.id == id; + } + return false; + } + + override size_t toHash() const { + import std.conv : to; + return id.to!size_t; + } + + override string toString() const { + import std.format : format; + return format!"IdEntity(id = %d)"(id); + } + + override int opCmp(Object other) const { + if (IdEntity e = cast(IdEntity) other) { + return this.id < e.id; + } + return 1; + } +} \ No newline at end of file