a bit better way to treah handlers
This commit is contained in:
parent
f0d205dee7
commit
3fe6d25448
16 changed files with 291 additions and 95 deletions
13
handler/CMakeLists.txt
Normal file
13
handler/CMakeLists.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
set(HEADERS
|
||||
handler.h
|
||||
info.h
|
||||
env.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
handler.cpp
|
||||
info.cpp
|
||||
env.cpp
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
17
handler/env.cpp
Normal file
17
handler/env.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "env.h"
|
||||
|
||||
Handler::Env::Env():
|
||||
Handler("env", Request::Method::get)
|
||||
{}
|
||||
|
||||
void Handler::Env::handle(Request& request) {
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
request.printEnvironment(body);
|
||||
|
||||
Response res(request);
|
||||
res.setBody(body);
|
||||
res.send();
|
||||
}
|
16
handler/env.h
Normal file
16
handler/env.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "handler.h"
|
||||
|
||||
namespace Handler {
|
||||
|
||||
class Env : public Handler::Handler {
|
||||
public:
|
||||
Env();
|
||||
virtual void handle(Request& request);
|
||||
|
||||
};
|
||||
}
|
11
handler/handler.cpp
Normal file
11
handler/handler.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "handler.h"
|
||||
|
||||
Handler::Handler::Handler(const std::string& path, Request::Method method):
|
||||
path(path),
|
||||
method(method)
|
||||
{}
|
||||
|
||||
Handler::Handler::~Handler() {}
|
26
handler/handler.h
Normal file
26
handler/handler.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "request/request.h"
|
||||
#include "response/response.h"
|
||||
|
||||
namespace Handler {
|
||||
|
||||
class Handler {
|
||||
protected:
|
||||
Handler(const std::string& path, Request::Method method);
|
||||
|
||||
public:
|
||||
virtual ~Handler();
|
||||
|
||||
virtual void handle(Request& request) = 0;
|
||||
|
||||
const std::string path;
|
||||
const Request::Method method;
|
||||
};
|
||||
}
|
18
handler/info.cpp
Normal file
18
handler/info.cpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "info.h"
|
||||
|
||||
Handler::Info::Info():
|
||||
Handler("info", Request::Method::get)
|
||||
{}
|
||||
|
||||
void Handler::Info::handle(Request& request) {
|
||||
Response res(request);
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["type"] = PROJECT_NAME;
|
||||
body["version"] = PROJECT_VERSION;
|
||||
|
||||
res.setBody(body);
|
||||
res.send();
|
||||
}
|
17
handler/info.h
Normal file
17
handler/info.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "handler.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace Handler {
|
||||
|
||||
class Info : public Handler {
|
||||
public:
|
||||
Info();
|
||||
void handle(Request& request) override;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue