Older/Server/Application.h

60 lines
1.9 KiB
C
Raw Normal View History

2024-01-24 23:19:53 +08:00
#ifndef __SETTINGS_H__
#define __SETTINGS_H__
#include "ApplicationSettings.h"
#include "Singleton.h"
#include "router.hpp"
#include <boost/asio/system_timer.hpp>
#include <boost/beast/http/string_body.hpp>
#include <thread>
class HttpSession;
class ChatRoom;
2024-05-03 22:30:46 +08:00
class SystemUsage;
2024-01-24 23:19:53 +08:00
class IoContext;
2024-11-10 18:33:39 +08:00
namespace Nng {
namespace Asio {
class Socket;
}
} // namespace Nng
2024-01-24 23:19:53 +08:00
class Application : public ApplicationSettings<Application>, public std::enable_shared_from_this<Application> {
public:
using Pointer = std::shared_ptr<Application>;
using Request = boost::beast::http::request<boost::beast::http::string_body>;
using RequestHandler = std::function<void(HttpSession &, const Request &, const boost::urls::matches &)>;
BUILD_SETTING(std::string, Server, "0.0.0.0");
BUILD_SETTING(uint16_t, Port, 8081);
2024-11-20 14:55:56 +08:00
BUILD_SETTING(uint16_t, WtPort, 8082);
2024-01-24 23:19:53 +08:00
BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency());
BUILD_SETTING(std::string, DocumentRoot, ".");
2024-05-03 22:30:46 +08:00
BUILD_SETTING(std::string, HomeAssistantAccessToken, "");
2024-10-23 19:53:51 +08:00
BUILD_SETTING(std::string, Live2dModelsRoot, "./live2d");
2024-01-24 23:19:53 +08:00
INITIALIZE_FIELDS(Server, Port, Threads, DocumentRoot);
Application(const std::string &path);
boost::asio::io_context &ioContext();
int exec();
const RequestHandler *find(boost::urls::segments_encoded_view path,
boost::urls::matches_base &matches) const noexcept;
2024-11-01 19:05:20 +08:00
void insertUrl(std::string_view url, RequestHandler &&handler);
2024-11-10 18:33:39 +08:00
static void requetExit();
2024-01-24 23:19:53 +08:00
protected:
void alarmTask();
2024-11-10 18:33:39 +08:00
void startAcceptRequest();
2024-01-24 23:19:53 +08:00
private:
int m_status = 0;
std::shared_ptr<IoContext> m_ioContext;
std::shared_ptr<boost::urls::router<RequestHandler>> m_router;
std::shared_ptr<boost::asio::system_timer> m_timer;
std::shared_ptr<ChatRoom> m_charRoom;
2024-05-03 22:30:46 +08:00
std::shared_ptr<SystemUsage> m_systemUsage;
2024-11-10 18:33:39 +08:00
std::shared_ptr<Nng::Asio::Socket> m_replyer;
2024-01-24 23:19:53 +08:00
};
#endif // __SETTINGS_H__