diff --git a/libraries/http_client/http_client/http_client.hh b/libraries/http_client/http_client/http_client.hh index 6c66ad7..a879363 100644 --- a/libraries/http_client/http_client/http_client.hh +++ b/libraries/http_client/http_client/http_client.hh @@ -1,5 +1,7 @@ #pragma once +#ifndef CURL_STATICLIB #define CURL_STATICLIB +#endif // CURL_STATICLIB #pragma comment(lib, "crypt32") #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Wldap32.lib") diff --git a/libraries/http_server/http_server/api.hh b/libraries/http_server/http_server/api.hh index 0746779..32cbcb2 100644 --- a/libraries/http_server/http_server/api.hh +++ b/libraries/http_server/http_server/api.hh @@ -24,7 +24,7 @@ template struct api { typedef api self; - api(): is_global_handler(false), global_handler_(nullptr) { } + api() : is_global_handler(false), global_handler_(nullptr) {} using H = std::function; struct VH { @@ -65,7 +65,9 @@ template struct api { void add_subapi(std::string prefix, const self& subapi) { subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) { - if (!r.empty() && r.back() == '/') + if (r.empty() || r == "/") + h.url_spec = prefix; + else if (r.back() == '/') h.url_spec = prefix + r; else h.url_spec = prefix + '/' + r; @@ -83,13 +85,11 @@ template struct api { std::cout << std::endl; } auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const { - if(is_global_handler) - { - global_handler_(request, response); - return; + if (is_global_handler) { + global_handler_(request, response); + return; } - if (route == last_called_route_) - { + if (route == last_called_route_) { if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) { request.url_spec = last_handler_.url_spec; last_handler_.handler(request, response); @@ -100,7 +100,8 @@ template struct api { // skip the last / of the url and trim spaces. std::string_view route2(route); - while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) + while (route2.size() > 1 and + (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) route2 = route2.substr(0, route2.size() - 1); auto it = routes_map_.find(route2); diff --git a/libraries/http_server/tests/subapi.cc b/libraries/http_server/tests/subapi.cc index 4141e18..666850c 100644 --- a/libraries/http_server/tests/subapi.cc +++ b/libraries/http_server/tests/subapi.cc @@ -6,20 +6,24 @@ using namespace li; int main() { - http_api my_api; - my_api.get("/") = [&](http_request& request, http_response& response) { - response.write("hello world."); + http_api root; + root.get("/") = [&](http_request& request, http_response& response) { response.write("root"); }; + + http_api subapi; + subapi.get("/") = [&](http_request& request, http_response& response) { + response.write("hello"); }; - http_api my_api2; - my_api2.get("/hello_world") = [&](http_request& request, http_response& response) { - response.write("hello world2."); + subapi.get("/world") = [&](http_request& request, http_response& response) { + response.write("hello world"); }; - my_api.add_subapi("/sub", my_api2); + root.add_subapi("/hello", subapi); - http_serve(my_api, 12334, s::non_blocking); - assert(http_get("http://localhost:12334").body == "hello world."); - assert(http_get("http://localhost:12334/").body == "hello world."); - assert(http_get("http://localhost:12334/sub/hello_world").body == "hello world2."); + http_serve(root, 12334, s::non_blocking); + assert(http_get("http://localhost:12334").body == "root"); + assert(http_get("http://localhost:12334/").body == "root"); + assert(http_get("http://localhost:12334/hello").body == "hello"); + assert(http_get("http://localhost:12334/hello/").body == "hello"); + assert(http_get("http://localhost:12334/hello/world").body == "hello world"); } diff --git a/libraries/sql/sql/sql_database.hh b/libraries/sql/sql/sql_database.hh index 12628b2..a0dabd1 100644 --- a/libraries/sql/sql/sql_database.hh +++ b/libraries/sql/sql/sql_database.hh @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace li { @@ -211,4 +212,4 @@ template struct sql_database { inline auto connect() { active_yield yield; return this->connect(yield); } }; -} \ No newline at end of file +} diff --git a/single_headers/lithium.hh b/single_headers/lithium.hh index 9e0139d..6709f9e 100644 --- a/single_headers/lithium.hh +++ b/single_headers/lithium.hh @@ -1821,7 +1821,9 @@ struct sqlite_database { #ifndef LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH #define LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH +#ifndef CURL_STATICLIB #define CURL_STATICLIB +#endif // CURL_STATICLIB #pragma comment(lib, "crypt32") #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Wldap32.lib") @@ -5490,6 +5492,7 @@ template struct sql_database { }; } + #endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH @@ -6834,7 +6837,7 @@ template struct api { typedef api self; - api(): is_global_handler(false), global_handler_(nullptr) { } + api() : is_global_handler(false), global_handler_(nullptr) {} using H = std::function; struct VH { @@ -6875,7 +6878,9 @@ template struct api { void add_subapi(std::string prefix, const self& subapi) { subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) { - if (!r.empty() && r.back() == '/') + if (r.empty() || r == "/") + h.url_spec = prefix; + else if (r.back() == '/') h.url_spec = prefix + r; else h.url_spec = prefix + '/' + r; @@ -6893,13 +6898,11 @@ template struct api { std::cout << std::endl; } auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const { - if(is_global_handler) - { - global_handler_(request, response); - return; + if (is_global_handler) { + global_handler_(request, response); + return; } - if (route == last_called_route_) - { + if (route == last_called_route_) { if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) { request.url_spec = last_handler_.url_spec; last_handler_.handler(request, response); @@ -6910,7 +6913,8 @@ template struct api { // skip the last / of the url and trim spaces. std::string_view route2(route); - while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) + while (route2.size() > 1 and + (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) route2 = route2.substr(0, route2.size() - 1); auto it = routes_map_.find(route2); diff --git a/single_headers/lithium_http_client.hh b/single_headers/lithium_http_client.hh index f22e0c8..07dda67 100644 --- a/single_headers/lithium_http_client.hh +++ b/single_headers/lithium_http_client.hh @@ -32,7 +32,9 @@ #ifndef LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH #define LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH +#ifndef CURL_STATICLIB #define CURL_STATICLIB +#endif // CURL_STATICLIB #pragma comment(lib, "crypt32") #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Wldap32.lib") diff --git a/single_headers/lithium_http_server.hh b/single_headers/lithium_http_server.hh index 1266d16..7f4700a 100644 --- a/single_headers/lithium_http_server.hh +++ b/single_headers/lithium_http_server.hh @@ -3654,7 +3654,7 @@ template struct api { typedef api self; - api(): is_global_handler(false), global_handler_(nullptr) { } + api() : is_global_handler(false), global_handler_(nullptr) {} using H = std::function; struct VH { @@ -3695,7 +3695,9 @@ template struct api { void add_subapi(std::string prefix, const self& subapi) { subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) { - if (!r.empty() && r.back() == '/') + if (r.empty() || r == "/") + h.url_spec = prefix; + else if (r.back() == '/') h.url_spec = prefix + r; else h.url_spec = prefix + '/' + r; @@ -3713,13 +3715,11 @@ template struct api { std::cout << std::endl; } auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const { - if(is_global_handler) - { - global_handler_(request, response); - return; + if (is_global_handler) { + global_handler_(request, response); + return; } - if (route == last_called_route_) - { + if (route == last_called_route_) { if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) { request.url_spec = last_handler_.url_spec; last_handler_.handler(request, response); @@ -3730,7 +3730,8 @@ template struct api { // skip the last / of the url and trim spaces. std::string_view route2(route); - while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) + while (route2.size() > 1 and + (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' ')) route2 = route2.substr(0, route2.size() - 1); auto it = routes_map_.find(route2); diff --git a/single_headers/lithium_mysql.hh b/single_headers/lithium_mysql.hh index 35f7fce..14ae02c 100644 --- a/single_headers/lithium_mysql.hh +++ b/single_headers/lithium_mysql.hh @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -2518,6 +2519,7 @@ template struct sql_database { }; } + #endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH diff --git a/single_headers/lithium_pgsql.hh b/single_headers/lithium_pgsql.hh index 0811052..1a6240b 100644 --- a/single_headers/lithium_pgsql.hh +++ b/single_headers/lithium_pgsql.hh @@ -14,6 +14,7 @@ #include #include #include +#include #include #if __APPLE__ #include @@ -2192,6 +2193,7 @@ template struct sql_database { }; } + #endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH