#ifndef DEVICE_CTRL_HTTP_SVR_H
#define DEVICE_CTRL_HTTP_SVR_H
#include <httplib.h>

#include "http/http_config.pb.h"
#include "app/bean.h"

namespace zsy
{
    class HttpSvr : public Bean
    {
        inline static std::shared_ptr<HttpSvr> HTTP_SVR;
        HTTPConfig_Svr config;
        std::unique_ptr<std::jthread> svrThread;

        std::shared_ptr<httplib::Server> svr;

    public:
        template<typename Func>
            requires std::invocable<Func, const httplib::Request &, httplib::Response &>
        void postEndpoint(std::string path, Func &&handler)
        {
            svr->Post(std::move(path), std::forward<Func>(handler));
        }

        template<typename Func>
            requires std::invocable<Func, const httplib::Request &, httplib::Response &>
        void getEndpoint(std::string path, Func &&handler)
        {
            svr->Get(std::move(path), std::forward<Func>(handler));
        }

        std::string name() const noexcept override;

        std::string sn() const noexcept override;

        static void init();

        static void destroy();

        static std::shared_ptr<HttpSvr> get();

        HttpSvr(const HTTPConfig_Svr &config);

        ~HttpSvr() override;
    };
} // zsy

#endif // DEVICE_CTRL_HTTP_SVR_H
