Older/Server/Application.h
amass dc5b750056
Some checks failed
Deploy / Build (push) Failing after 53s
add webrtc test.
2025-01-12 00:46:14 +08:00

64 lines
2.1 KiB
C++

#ifndef __SETTINGS_H__
#define __SETTINGS_H__
#include "ApplicationSettings.h"
#include "Singleton.h"
#include <boost/asio/system_timer.hpp>
#include <boost/beast/http/string_body.hpp>
#include <thread>
#include "matches.hpp"
class HttpSession;
class SystemUsage;
class IoContext;
class SignalServer;
namespace Nng {
namespace Asio {
class Socket;
}
} // namespace Nng
class ApplicationPrivate;
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);
BUILD_SETTING(uint16_t, WtPort, 8082);
BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency());
BUILD_SETTING(std::string, ApplicationRoot, "."); // 内部配置文件,不应被外部访问
BUILD_SETTING(std::string, DocumentRoot, "."); // 外部可访问的公共文件
BUILD_SETTING(std::string, HomeAssistantAccessToken, "");
BUILD_SETTING(std::string, MeiliSearchApiKey, "");
BUILD_SETTING(std::string, MeiliSearchConfig, "");
INITIALIZE_FIELDS(Server, Port, Threads, ApplicationRoot, DocumentRoot);
Application(const std::string &path);
~Application();
boost::asio::io_context &ioContext();
int exec();
void startAcceptHttpConnections(const std::string &address, uint16_t port);
void insertUrl(std::string_view url, RequestHandler &&handler);
static void requetExit();
protected:
void alarmTask();
void startAcceptRequest();
void asyncAcceptHttpConnections();
private:
ApplicationPrivate *m_d = nullptr;
int m_status = 0;
std::shared_ptr<IoContext> m_ioContext;
std::shared_ptr<boost::asio::system_timer> m_timer;
std::shared_ptr<SystemUsage> m_systemUsage;
std::shared_ptr<Nng::Asio::Socket> m_replyer;
std::shared_ptr<SignalServer> m_signalServer;
};
#endif // __SETTINGS_H__