web-logbook/source/app.d

184 lines
5.5 KiB
D
Raw Normal View History

2023-07-22 22:04:30 +00:00
import handy_httpd;
2024-01-26 01:31:01 +00:00
import handy_httpd.handlers.path_handler;
2023-07-22 22:04:30 +00:00
import slf4d;
import d_properties;
import d2sqlite3;
import std.file;
import std.conv;
import std.json;
import std.datetime;
void main() {
2024-01-29 21:30:37 +00:00
ServerConfig config;
config.receiveBufferSize = 2048;
2023-07-22 22:04:30 +00:00
if (exists("application.properties")) {
Properties props = Properties("application.properties");
if (props.has("hostname")) {
config.hostname = props.get("hostname");
}
if (props.has("port")) {
config.port = props.get("port").to!ushort;
}
if (props.has("workers")) {
config.workerPoolSize = props.get("workers").to!size_t;
}
2023-07-22 22:04:30 +00:00
}
initDb();
HttpServer server = new HttpServer((ref HttpRequestContext ctx) {
ctx.response.addHeader("Access-Control-Allow-Origin", "*");
ctx.response.addHeader("Access-Control-Allow-Headers", "*");
if (ctx.request.method == Method.GET) {
handleLogbookRequest(ctx);
} else if (ctx.request.method == Method.POST) {
handleVisitorLog(ctx);
} else if (ctx.request.method == Method.OPTIONS) {
ctx.response.setStatus(HttpStatus.OK);
} else {
ctx.response.setStatus(HttpStatus.METHOD_NOT_ALLOWED);
}
}, config);
server.start();
}
void handleVisitorLog(ref HttpRequestContext ctx) {
JSONValue logBody = ctx.request.readBodyAsJson();
string name = logBody.object["name"].str;
2023-07-23 23:59:21 +00:00
if (name.length > 32) {
ctx.response.setStatus(HttpStatus.BAD_REQUEST);
ctx.response.writeBodyString("Name is too long.");
return;
}
2023-07-22 22:04:30 +00:00
string message = logBody.object["message"].str;
2023-07-23 23:59:21 +00:00
if (message.length > 255) {
ctx.response.setStatus(HttpStatus.BAD_REQUEST);
ctx.response.writeBodyString("Message is too long.");
return;
}
2024-01-26 01:31:01 +00:00
if (!ctx.request.headers.contains("X-Forwarded-For")) {
2023-07-23 23:59:21 +00:00
ctx.response.setStatus(HttpStatus.FORBIDDEN);
ctx.response.writeBodyString("Missing remote IP");
return;
}
2024-01-26 01:31:01 +00:00
string remoteAddress = ctx.request.headers.getFirst("X-Forwarded-For").orElseThrow();
2023-07-24 00:01:02 +00:00
// If the user has sent another log within the last minute, block this one.
2023-07-23 23:59:21 +00:00
LogEntry[] recentLogsByThisAddress = getRecentLogEntriesByRemoteAddress(remoteAddress);
SysTime now = Clock.currTime();
if (recentLogsByThisAddress.length > 0 && now - recentLogsByThisAddress[0].createdAt < minutes(1)) {
ctx.response.setStatus(HttpStatus.TOO_MANY_REQUESTS);
return;
2023-07-22 22:04:30 +00:00
}
2023-07-24 00:01:02 +00:00
2023-07-22 22:04:30 +00:00
insertLogEntry(remoteAddress, name, message);
}
void handleLogbookRequest(ref HttpRequestContext ctx) {
2023-07-23 23:59:21 +00:00
uint limit = ctx.request.getParamAs!uint("limit", 5);
if (limit > 100) {
ctx.response.setStatus(HttpStatus.BAD_REQUEST);
ctx.response.writeBodyString("Limit is too large.");
return;
}
uint offset = ctx.request.getParamAs!uint("offset", 0);
LogEntry[] entries = getRecentLogEntries(limit, offset);
2023-07-22 22:04:30 +00:00
JSONValue entriesJson = JSONValue(JSONValue[].init);
foreach (LogEntry entry; entries) {
entriesJson.array ~= entry.toJson();
}
ctx.response.writeBodyString(entriesJson.toString(), "application/json");
}
struct LogEntry {
ulong id;
SysTime createdAt;
string remoteAddress;
string name;
string message;
JSONValue toJson() const {
JSONValue obj = JSONValue(string[string].init);
obj["id"] = JSONValue(id);
2023-08-01 12:33:11 +00:00
obj["createdAt"] = JSONValue(createdAt.toISOExtString());
2023-07-22 22:04:30 +00:00
obj["remoteAddress"] = JSONValue(remoteAddress);
obj["name"] = JSONValue(name);
obj["message"] = JSONValue(message);
return obj;
}
2023-07-23 23:59:21 +00:00
static LogEntry fromDbRow(ref Row row) {
LogEntry entry;
entry.id = row.peek!ulong(0);
string createdAtStr = row.peek!string(1);
string isoCreatedAt = createdAtStr[0 .. 4] ~ createdAtStr[5 .. 7] ~ createdAtStr[8 .. 10] ~ 'T' ~
createdAtStr[11 .. 13] ~ createdAtStr[14 .. 16] ~ createdAtStr[17 .. $];
entry.createdAt = SysTime.fromISOString(isoCreatedAt);
entry.remoteAddress = row.peek!string(2);
entry.name = row.peek!string(3);
entry.message = row.peek!string(4);
return entry;
}
2023-07-22 22:04:30 +00:00
}
void initDb() {
Database db = Database("logbook.sqlite");
db.run(q"SQL
CREATE TABLE IF NOT EXISTS log_entry (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
remote_address TEXT NOT NULL,
name TEXT NOT NULL,
message TEXT NOT NULL
);
SQL");
db.close();
info("Initialized database.");
}
void insertLogEntry(string remoteAddress, string name, string message) {
Database db = Database("logbook.sqlite");
Statement stmt = db.prepare(q"SQL
INSERT INTO log_entry (remote_address, name, message)
VALUES (:addr, :name, :msg);
SQL");
stmt.bind(1, remoteAddress);
stmt.bind(2, name);
stmt.bind(3, message);
stmt.execute();
stmt.finalize();
db.close();
infoF!"Added log entry for %s @ %s"(name, remoteAddress);
}
2023-07-23 23:59:21 +00:00
LogEntry[] findAllByQuery(string query) {
import std.array : Appender, appender;
2023-07-22 22:04:30 +00:00
Database db = Database("logbook.sqlite");
2023-07-23 23:59:21 +00:00
ResultRange results = db.execute(query);
Appender!(LogEntry[]) app = appender!(LogEntry[])();
2023-07-22 22:04:30 +00:00
foreach (Row row; results) {
2023-07-23 23:59:21 +00:00
app ~= LogEntry.fromDbRow(row);
}
return app.data();
}
LogEntry[] getRecentLogEntries(uint limit, uint offset) {
import std.format;
string query = format!"SELECT * FROM log_entry ORDER BY created_at DESC LIMIT %d OFFSET %d"(limit, offset);
return findAllByQuery(query);
}
LogEntry[] getRecentLogEntriesByRemoteAddress(string remoteAddress) {
import std.array : Appender, appender;
Database db = Database("logbook.sqlite");
Statement stmt = db.prepare("SELECT * FROM log_entry WHERE remote_address = :addr ORDER BY created_at DESC LIMIT 10");
2023-07-24 00:02:53 +00:00
stmt.bind(1, remoteAddress);
2023-07-23 23:59:21 +00:00
ResultRange results = stmt.execute();
Appender!(LogEntry[]) app = appender!(LogEntry[])();
foreach (Row row; results) {
app ~= LogEntry.fromDbRow(row);
2023-07-22 22:04:30 +00:00
}
2023-07-23 23:59:21 +00:00
return app.data();
2023-07-22 22:04:30 +00:00
}