@@ -445,45 +445,69 @@ using namespace httplib;
445445
446446// ll::thread::TickSyncTaskPool taskPool;
447447
448- #define ADD_CALLBACK (METHOD, path, func ) \
449- callbacks.emplace(make_pair( \
450- path, \
451- HttpServerCallback{ \
452- EngineScope::currentEngine (), \
453- script::Global<Function>{func}, \
454- HttpRequestType::METHOD, \
455- path \
456- } \
457- )); \
458- svr->METHOD (path.c_str(), [this, engine = EngineScope::currentEngine()](const Request& req, Response& resp) { \
459- if ((ll::getGamingStatus () == ll::GamingStatus::Stopping) || !EngineManager::isValid (engine) \
460- || engine->isDestroying ()) \
461- return ; \
462- ll::coro::keepThis ([this , engine, req, &resp]() -> ll::coro::CoroTask<> { \
463- if ((ll::getGamingStatus () == ll::GamingStatus::Stopping) || !EngineManager::isValid (engine) \
464- || engine->isDestroying ()) \
465- co_return ; \
466- \
467- EngineScope enter (engine); \
468- try { \
469- for (auto & [k, v] : this ->callbacks ) { \
470- if (v.type != HttpRequestType::METHOD) continue ; \
471- std::regex rgx (k); \
472- std::smatch matches; \
473- if (std::regex_match (req.path , matches, rgx)) { \
474- if (matches == req.matches ) { \
475- auto reqObj = new HttpRequestClass (req); \
476- auto respObj = new HttpResponseClass (resp); \
477- v.func .get ().call ({}, reqObj, respObj); \
478- resp = *respObj->get (); \
479- break ; \
480- } \
481- } \
482- } \
483- } \
484- CATCH_CALLBACK_IN_CORO (" Fail in NetworkAPI callback" ) \
485- }).launch (ll::thread::ThreadPoolExecutor::getDefault ()); \
486- });
448+ void ADD_CALLBACK (
449+ std::shared_ptr<httplib::Server> svr,
450+ std::multimap<std::string, HttpServerCallback>& callbacks,
451+ HttpRequestType const & method,
452+ std::string const & path,
453+ Local<Function> const & func
454+ ) {
455+ callbacks.emplace (
456+ make_pair (path, HttpServerCallback{EngineScope::currentEngine (), script::Global<Function>{func}, method, path})
457+ );
458+ auto receiveMethod =
459+ [engine = EngineScope::currentEngine (), method, callbacks](const Request& req, Response& resp) {
460+ if ((ll::getGamingStatus () == ll::GamingStatus::Stopping) || !EngineManager::isValid (engine)
461+ || engine->isDestroying ())
462+ return ;
463+ ll::coro::keepThis ([engine, req, &resp, method, callbacks]() -> ll::coro::CoroTask<> {
464+ if ((ll::getGamingStatus () == ll::GamingStatus::Stopping) || !EngineManager::isValid (engine)
465+ || engine->isDestroying ())
466+ co_return ;
467+
468+ EngineScope enter (engine);
469+ try {
470+ for (auto & [k, v] : callbacks) {
471+ if (v.type != method) continue ;
472+ std::regex rgx (k);
473+ std::smatch matches;
474+ if (std::regex_match (req.path , matches, rgx)) {
475+ if (matches == req.matches ) {
476+ auto reqObj = new HttpRequestClass (req);
477+ auto respObj = new HttpResponseClass (resp);
478+ v.func .get ().call ({}, reqObj, respObj);
479+ resp = *respObj->get ();
480+ break ;
481+ }
482+ }
483+ }
484+ }
485+ CATCH_CALLBACK_IN_CORO (" Fail in NetworkAPI callback" )
486+ }).launch (ll::thread::ThreadPoolExecutor::getDefault ());
487+ };
488+ switch (method) {
489+ case HttpRequestType::Get:
490+ svr->Get (path.c_str (), receiveMethod);
491+ break ;
492+ case HttpRequestType::Post:
493+ svr->Post (path.c_str (), receiveMethod);
494+ break ;
495+ case HttpRequestType::Put:
496+ svr->Put (path.c_str (), receiveMethod);
497+ break ;
498+ case HttpRequestType::Delete:
499+ svr->Delete (path.c_str (), receiveMethod);
500+ break ;
501+ case HttpRequestType::Options:
502+ svr->Options (path.c_str (), receiveMethod);
503+ break ;
504+ case HttpRequestType::Patch:
505+ svr->Patch (path.c_str (), receiveMethod);
506+ break ;
507+ default :
508+ break ;
509+ }
510+ }
487511
488512HttpServerClass::HttpServerClass (const Local<Object>& scriptObj) : ScriptClass(scriptObj), svr(new Server) {}
489513HttpServerClass::HttpServerClass () : ScriptClass(ScriptClass::ConstructFromCpp<HttpServerClass>{}), svr(new Server) {}
@@ -500,7 +524,7 @@ Local<Value> HttpServerClass::onGet(const Arguments& args) {
500524 try {
501525 auto path = args[0 ].asString ().toString ();
502526 auto func = args[1 ].asFunction ();
503- ADD_CALLBACK (Get, path, func);
527+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Get, path, func);
504528 return this ->getScriptObject ();
505529 }
506530 CATCH (" Fail in onGet" )
@@ -514,7 +538,7 @@ Local<Value> HttpServerClass::onPut(const Arguments& args) {
514538 try {
515539 auto path = args[0 ].asString ().toString ();
516540 auto func = args[1 ].asFunction ();
517- ADD_CALLBACK (Put, path, func);
541+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Put, path, func);
518542 return this ->getScriptObject ();
519543 }
520544 CATCH (" Fail in onPut!" );
@@ -528,7 +552,7 @@ Local<Value> HttpServerClass::onPost(const Arguments& args) {
528552 try {
529553 auto path = args[0 ].asString ().toString ();
530554 auto func = args[1 ].asFunction ();
531- ADD_CALLBACK (Post, path, func);
555+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Post, path, func);
532556 return this ->getScriptObject ();
533557 }
534558 CATCH (" Fail in onPost!" );
@@ -542,7 +566,7 @@ Local<Value> HttpServerClass::onPatch(const Arguments& args) {
542566 try {
543567 auto path = args[0 ].asString ().toString ();
544568 auto func = args[1 ].asFunction ();
545- ADD_CALLBACK (Patch, path, func);
569+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Patch, path, func);
546570 return this ->getScriptObject ();
547571 }
548572 CATCH (" Fail in onPatch!" );
@@ -556,7 +580,7 @@ Local<Value> HttpServerClass::onDelete(const Arguments& args) {
556580 try {
557581 auto path = args[0 ].asString ().toString ();
558582 auto func = args[1 ].asFunction ();
559- ADD_CALLBACK (Delete, path, func);
583+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Delete, path, func);
560584 return this ->getScriptObject ();
561585 }
562586 CATCH (" Fail in onDelete!" );
@@ -570,7 +594,7 @@ Local<Value> HttpServerClass::onOptions(const Arguments& args) {
570594 try {
571595 auto path = args[0 ].asString ().toString ();
572596 auto func = args[1 ].asFunction ();
573- ADD_CALLBACK (Options, path, func);
597+ ADD_CALLBACK (svr, callbacks, HttpRequestType:: Options, path, func);
574598 return this ->getScriptObject ();
575599 }
576600 CATCH (" Fail in onOptions!" );
0 commit comments