2023-12-10 23:23:15 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
2023-12-10 23:23:15 +00:00
|
|
|
constexpr uint8_t currentDbVesion = 1;
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
Server::Server():
|
|
|
|
terminating(false),
|
|
|
|
requestCount(0),
|
2023-11-23 19:57:32 +00:00
|
|
|
serverName(std::nullopt),
|
2023-12-07 20:32:43 +00:00
|
|
|
router(),
|
|
|
|
db()
|
2023-11-23 19:57:32 +00:00
|
|
|
{
|
2023-12-07 20:32:43 +00:00
|
|
|
std::cout << "Startig pica..." << std::endl;
|
|
|
|
|
|
|
|
db = DBInterface::create(DBInterface::Type::mysql);
|
|
|
|
std::cout << "Database type: MySQL" << std::endl;
|
|
|
|
|
|
|
|
db->setCredentials("pica", "pica");
|
|
|
|
db->setDatabase("pica");
|
2023-12-07 20:47:15 +00:00
|
|
|
|
2023-12-11 23:29:55 +00:00
|
|
|
db->connect("/run/mysqld/mysqld.sock");
|
|
|
|
db->migrate(currentDbVesion);
|
2023-12-08 22:26:16 +00:00
|
|
|
|
2023-11-23 19:57:32 +00:00
|
|
|
router.addRoute("info", Server::info);
|
|
|
|
router.addRoute("env", Server::printEnvironment);
|
|
|
|
}
|
2023-11-21 22:19:08 +00:00
|
|
|
|
|
|
|
Server::~Server() {}
|
|
|
|
|
|
|
|
void Server::run(int socketDescriptor) {
|
|
|
|
while (!terminating) {
|
|
|
|
std::unique_ptr<Request> request = std::make_unique<Request>();
|
|
|
|
bool result = request->wait(socketDescriptor);
|
|
|
|
if (!result) {
|
|
|
|
std::cerr << "Error accepting a request" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleRequest(std::move(request));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::handleRequest(std::unique_ptr<Request> request) {
|
|
|
|
++requestCount;
|
|
|
|
if (!serverName) {
|
|
|
|
try {
|
|
|
|
serverName = request->getServerName();
|
|
|
|
std::cout << "received server name " << serverName.value() << std::endl;
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "failed to read server name" << std::endl;
|
|
|
|
Response error(Response::Status::internalError);
|
|
|
|
error.replyTo(*request.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request->isGet()) {
|
|
|
|
static const Response methodNotAllowed(Response::Status::methodNotAllowed);
|
|
|
|
methodNotAllowed.replyTo(*request.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-11-23 19:57:32 +00:00
|
|
|
std::string path = request->getPath(serverName.value());
|
2023-12-07 20:32:43 +00:00
|
|
|
router.route(path.data(), std::move(request), this);
|
2023-12-11 23:29:55 +00:00
|
|
|
} catch (const std::exception& e) {
|
2023-11-21 22:19:08 +00:00
|
|
|
Response error(Response::Status::internalError);
|
2023-11-23 19:57:32 +00:00
|
|
|
error.setBody(std::string(e.what()));
|
2023-11-21 22:19:08 +00:00
|
|
|
error.replyTo(*request.get());
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 19:57:32 +00:00
|
|
|
|
2023-12-07 20:32:43 +00:00
|
|
|
bool Server::printEnvironment(Request* request, Server* server) {
|
2023-12-11 23:29:55 +00:00
|
|
|
UNUSED(server);
|
2023-11-23 19:57:32 +00:00
|
|
|
nlohmann::json body = nlohmann::json::object();
|
|
|
|
request->printEnvironment(body);
|
|
|
|
|
|
|
|
Response res;
|
|
|
|
res.setBody(body);
|
|
|
|
res.replyTo(*request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-07 20:32:43 +00:00
|
|
|
bool Server::info(Request* request, Server* server) {
|
2023-12-11 23:29:55 +00:00
|
|
|
UNUSED(server);
|
2023-11-23 19:57:32 +00:00
|
|
|
Response res;
|
|
|
|
nlohmann::json body = nlohmann::json::object();
|
2023-12-11 23:29:55 +00:00
|
|
|
body["type"] = PROJECT_NAME;
|
|
|
|
body["version"] = PROJECT_VERSION;
|
2023-11-23 19:57:32 +00:00
|
|
|
|
|
|
|
res.setBody(body);
|
|
|
|
res.replyTo(*request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|