@@ -17,7 +17,7 @@ namespace hcpp
1717 namespace log = spdlog;
1818 // inline std::latch latch(1);
1919
20- awaitable<void > http_do (std::unique_ptr<http_client> client, std::shared_ptr<service_keeper> sk)
20+ awaitable<void > http_handler:: http_do (std::unique_ptr<http_client> client, std::shared_ptr<service_keeper> sk)
2121 {
2222 co_await client->init ();
2323 while (true )
@@ -136,8 +136,15 @@ namespace hcpp
136136 }
137137 else // TODO 用于控制自身行为
138138 {
139- co_await ss->async_write_all (" HTTP/1.1 200 OK\r\n Content-Type: text/html; charset=utf-8\r\n Content-Length: 16\r\n\r\n Hello,from http!" );
140- continue ;
139+ if (auto p=base_handlers_.find (req.url_ );p!=base_handlers_.end ())
140+ {
141+ co_await ss->async_write_all ((p->second )(req.url_ ));
142+ }
143+ else
144+ {
145+ co_await ss->async_write_all (" HTTP/1.1 200 OK\r\n Content-Type: text/html; charset=utf-8\r\n Content-Length: 16\r\n\r\n Hello,from http!" );
146+ continue ;
147+ }
141148 }
142149 }
143150 }
@@ -151,24 +158,35 @@ namespace hcpp
151158 }
152159 }
153160 }
161+
162+ void http_handler::add_handler (std::string_view path, std::function<std::string (std::string_view)> handler)
163+ {
164+ base_handlers_.insert ({path, handler});
165+ }
154166
155167 using tcp_acceptor = use_awaitable_t <>::as_default_on_t <ip::tcp::acceptor>;
156168
157- awaitable<void > httpserver::wait_http (uint16_t port)
169+ awaitable<void > httpserver::wait_http (uint16_t port, io_context &ic )
158170 {
159171 auto executor = co_await this_coro::executor;
160172
161173 tcp_acceptor acceptor (executor, {ip::tcp::v4 (), port});
162174 co_await nc->async_receive ();
163175 spdlog::debug (" http_server监听端口:{}" , acceptor.local_endpoint ().port ());
176+ http_handler hh;
177+ hh.add_handler (" /stop" , [&ic](auto &&path)
178+ {
179+ ic.stop ();
180+ return " HTTP/1.1 200 OK\r\n Content-Type: text/html; charset=utf-8\r\n Content-Length: 12\r\n\r\n server stop!" ;
181+ });
164182 for (;;)
165183 {
166184 try
167185 {
168186 auto socket = co_await acceptor.async_accept ();
169187 auto sk = std::make_unique<http_svc_keeper>(make_threadlocal_svc_cache (), slow_dns::get_slow_dns ());
170188 auto hsk = std::make_shared<hack_sk>(std::move (sk), ta_);
171- co_spawn (executor, http_do (std::make_unique<http_client>(std::move (socket)), std::move (hsk)), detached);
189+ co_spawn (executor, hh. http_do (std::make_unique<http_client>(std::move (socket)), std::move (hsk)), detached);
172190 }
173191 catch (const std::exception &e)
174192 {
@@ -204,8 +222,9 @@ namespace hcpp
204222
205223 co_await nc->async_send (asio::error_code{}, " ok" );
206224 // 在当前协程运行
207- auto https_listener = [&executor, c = channel_, cr_ps_map, ca_subject = ca_subject_]() -> awaitable<void >
225+ auto https_listener = [c = channel_, cr_ps_map, ca_subject = ca_subject_]() -> awaitable<void >
208226 {
227+ http_handler hh;
209228 for (;;)
210229 {
211230 try
@@ -253,7 +272,7 @@ namespace hcpp
253272 self.set_mem (co_await std::move (*cc).make (std::move (si)));
254273 };
255274
256- co_spawn (executor, http_do (std::move (hsc), std::move (sk)), detached);
275+ co_spawn (co_await this_coro:: executor, hh. http_do (std::move (hsc), std::move (sk)), detached);
257276 }
258277 catch (const std::exception &e)
259278 {
@@ -280,7 +299,8 @@ namespace hcpp
280299 }
281300 spdlog::debug (" mimt https server线程退出成功" );
282301 };
283- std::jthread t (https_service);
302+ std::thread t (https_service);
303+ t.detach ();
284304
285305 std::unique_ptr<int , std::function<void (int *)>> ptr (new int (0 ), [&work_guard, &executor](auto &&p)
286306 {
@@ -291,7 +311,7 @@ namespace hcpp
291311 delete p; });
292312
293313 co_await co_spawn (executor, https_listener (), use_awaitable);
294- // XXX 不会执行到这里
314+ // XXX 不会执行到这里
295315 co_return ;
296316 }
297317
0 commit comments