just some thoughts
This commit is contained in:
parent
03d7614673
commit
f0d205dee7
15 changed files with 172 additions and 34 deletions
|
@ -8,4 +8,4 @@ set(SOURCES
|
|||
router.cpp
|
||||
)
|
||||
|
||||
target_sources(pica PRIVATE ${SOURCES})
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
|
|
@ -20,18 +20,8 @@ Server::Server():
|
|||
db->setCredentials("pica", "pica");
|
||||
db->setDatabase("pica");
|
||||
|
||||
bool connected = false;
|
||||
try {
|
||||
db->connect("/run/mysqld/mysqld.sock");
|
||||
connected = true;
|
||||
std::cout << "Successfully connected to the database" << std::endl;
|
||||
|
||||
} catch (const std::runtime_error& e) {
|
||||
std::cerr << "Couldn't connect to the database: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
if (connected)
|
||||
db->migrate(currentDbVesion);
|
||||
db->connect("/run/mysqld/mysqld.sock");
|
||||
db->migrate(currentDbVesion);
|
||||
|
||||
router.addRoute("info", Server::info);
|
||||
router.addRoute("env", Server::printEnvironment);
|
||||
|
@ -74,7 +64,7 @@ void Server::handleRequest(std::unique_ptr<Request> request) {
|
|||
try {
|
||||
std::string path = request->getPath(serverName.value());
|
||||
router.route(path.data(), std::move(request), this);
|
||||
} catch (const std::exception e) {
|
||||
} catch (const std::exception& e) {
|
||||
Response error(Response::Status::internalError);
|
||||
error.setBody(std::string(e.what()));
|
||||
error.replyTo(*request.get());
|
||||
|
@ -82,7 +72,7 @@ void Server::handleRequest(std::unique_ptr<Request> request) {
|
|||
}
|
||||
|
||||
bool Server::printEnvironment(Request* request, Server* server) {
|
||||
(void)server;
|
||||
UNUSED(server);
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
request->printEnvironment(body);
|
||||
|
||||
|
@ -94,11 +84,11 @@ bool Server::printEnvironment(Request* request, Server* server) {
|
|||
}
|
||||
|
||||
bool Server::info(Request* request, Server* server) {
|
||||
(void)server;
|
||||
UNUSED(server);
|
||||
Response res;
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["type"] = "Pica";
|
||||
body["version"] = "0.0.1";
|
||||
body["type"] = PROJECT_NAME;
|
||||
body["version"] = PROJECT_VERSION;
|
||||
|
||||
res.setBody(body);
|
||||
res.replyTo(*request);
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
#include "response/response.h"
|
||||
#include "router.h"
|
||||
#include "database/dbinterface.h"
|
||||
#include "utils/helpers.h"
|
||||
#include "config.h"
|
||||
|
||||
class Server {
|
||||
public:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue