Skip to content

Commit 525566e

Browse files
authored
Merge pull request #131 from cong1920/subapi_endpoint_separation
[http_server] Make default hanlder for the base path in the "add_subapi"
2 parents 25aefc0 + 65cbd76 commit 525566e

File tree

9 files changed

+58
-39
lines changed

9 files changed

+58
-39
lines changed

libraries/http_client/http_client/http_client.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
2+
#ifndef CURL_STATICLIB
23
#define CURL_STATICLIB
4+
#endif // CURL_STATICLIB
35
#pragma comment(lib, "crypt32")
46
#pragma comment(lib, "ws2_32.lib")
57
#pragma comment(lib, "Wldap32.lib")

libraries/http_server/http_server/api.hh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ template <typename Req, typename Resp> struct api {
2424

2525
typedef api<Req, Resp> self;
2626

27-
api(): is_global_handler(false), global_handler_(nullptr) { }
27+
api() : is_global_handler(false), global_handler_(nullptr) {}
2828

2929
using H = std::function<void(Req&, Resp&)>;
3030
struct VH {
@@ -65,7 +65,9 @@ template <typename Req, typename Resp> struct api {
6565

6666
void add_subapi(std::string prefix, const self& subapi) {
6767
subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) {
68-
if (!r.empty() && r.back() == '/')
68+
if (r.empty() || r == "/")
69+
h.url_spec = prefix;
70+
else if (r.back() == '/')
6971
h.url_spec = prefix + r;
7072
else
7173
h.url_spec = prefix + '/' + r;
@@ -83,13 +85,11 @@ template <typename Req, typename Resp> struct api {
8385
std::cout << std::endl;
8486
}
8587
auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const {
86-
if(is_global_handler)
87-
{
88-
global_handler_(request, response);
89-
return;
88+
if (is_global_handler) {
89+
global_handler_(request, response);
90+
return;
9091
}
91-
if (route == last_called_route_)
92-
{
92+
if (route == last_called_route_) {
9393
if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) {
9494
request.url_spec = last_handler_.url_spec;
9595
last_handler_.handler(request, response);
@@ -100,7 +100,8 @@ template <typename Req, typename Resp> struct api {
100100

101101
// skip the last / of the url and trim spaces.
102102
std::string_view route2(route);
103-
while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
103+
while (route2.size() > 1 and
104+
(route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
104105
route2 = route2.substr(0, route2.size() - 1);
105106

106107
auto it = routes_map_.find(route2);

libraries/http_server/tests/subapi.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ using namespace li;
66

77
int main() {
88

9-
http_api my_api;
10-
my_api.get("/") = [&](http_request& request, http_response& response) {
11-
response.write("hello world.");
9+
http_api root;
10+
root.get("/") = [&](http_request& request, http_response& response) { response.write("root"); };
11+
12+
http_api subapi;
13+
subapi.get("/") = [&](http_request& request, http_response& response) {
14+
response.write("hello");
1215
};
1316

14-
http_api my_api2;
15-
my_api2.get("/hello_world") = [&](http_request& request, http_response& response) {
16-
response.write("hello world2.");
17+
subapi.get("/world") = [&](http_request& request, http_response& response) {
18+
response.write("hello world");
1719
};
1820

19-
my_api.add_subapi("/sub", my_api2);
21+
root.add_subapi("/hello", subapi);
2022

21-
http_serve(my_api, 12334, s::non_blocking);
22-
assert(http_get("http://localhost:12334").body == "hello world.");
23-
assert(http_get("http://localhost:12334/").body == "hello world.");
24-
assert(http_get("http://localhost:12334/sub/hello_world").body == "hello world2.");
23+
http_serve(root, 12334, s::non_blocking);
24+
assert(http_get("http://localhost:12334").body == "root");
25+
assert(http_get("http://localhost:12334/").body == "root");
26+
assert(http_get("http://localhost:12334/hello").body == "hello");
27+
assert(http_get("http://localhost:12334/hello/").body == "hello");
28+
assert(http_get("http://localhost:12334/hello/world").body == "hello world");
2529
}

libraries/sql/sql/sql_database.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <deque>
4+
#include <functional>
45
#include <unordered_map>
56

67
namespace li {
@@ -211,4 +212,4 @@ template <typename I> struct sql_database {
211212
inline auto connect() { active_yield yield; return this->connect(yield); }
212213
};
213214

214-
}
215+
}

single_headers/lithium.hh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,9 @@ struct sqlite_database {
18211821

18221822
#ifndef LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH
18231823
#define LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH
1824+
#ifndef CURL_STATICLIB
18241825
#define CURL_STATICLIB
1826+
#endif // CURL_STATICLIB
18251827
#pragma comment(lib, "crypt32")
18261828
#pragma comment(lib, "ws2_32.lib")
18271829
#pragma comment(lib, "Wldap32.lib")
@@ -5490,6 +5492,7 @@ template <typename I> struct sql_database {
54905492
};
54915493

54925494
}
5495+
54935496
#endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH
54945497

54955498

@@ -6834,7 +6837,7 @@ template <typename Req, typename Resp> struct api {
68346837

68356838
typedef api<Req, Resp> self;
68366839

6837-
api(): is_global_handler(false), global_handler_(nullptr) { }
6840+
api() : is_global_handler(false), global_handler_(nullptr) {}
68386841

68396842
using H = std::function<void(Req&, Resp&)>;
68406843
struct VH {
@@ -6875,7 +6878,9 @@ template <typename Req, typename Resp> struct api {
68756878

68766879
void add_subapi(std::string prefix, const self& subapi) {
68776880
subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) {
6878-
if (!r.empty() && r.back() == '/')
6881+
if (r.empty() || r == "/")
6882+
h.url_spec = prefix;
6883+
else if (r.back() == '/')
68796884
h.url_spec = prefix + r;
68806885
else
68816886
h.url_spec = prefix + '/' + r;
@@ -6893,13 +6898,11 @@ template <typename Req, typename Resp> struct api {
68936898
std::cout << std::endl;
68946899
}
68956900
auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const {
6896-
if(is_global_handler)
6897-
{
6898-
global_handler_(request, response);
6899-
return;
6901+
if (is_global_handler) {
6902+
global_handler_(request, response);
6903+
return;
69006904
}
6901-
if (route == last_called_route_)
6902-
{
6905+
if (route == last_called_route_) {
69036906
if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) {
69046907
request.url_spec = last_handler_.url_spec;
69056908
last_handler_.handler(request, response);
@@ -6910,7 +6913,8 @@ template <typename Req, typename Resp> struct api {
69106913

69116914
// skip the last / of the url and trim spaces.
69126915
std::string_view route2(route);
6913-
while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
6916+
while (route2.size() > 1 and
6917+
(route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
69146918
route2 = route2.substr(0, route2.size() - 1);
69156919

69166920
auto it = routes_map_.find(route2);

single_headers/lithium_http_client.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
#ifndef LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH
3434
#define LITHIUM_SINGLE_HEADER_GUARD_LI_HTTP_CLIENT_HTTP_CLIENT_HH
35+
#ifndef CURL_STATICLIB
3536
#define CURL_STATICLIB
37+
#endif // CURL_STATICLIB
3638
#pragma comment(lib, "crypt32")
3739
#pragma comment(lib, "ws2_32.lib")
3840
#pragma comment(lib, "Wldap32.lib")

single_headers/lithium_http_server.hh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,7 @@ template <typename Req, typename Resp> struct api {
36543654

36553655
typedef api<Req, Resp> self;
36563656

3657-
api(): is_global_handler(false), global_handler_(nullptr) { }
3657+
api() : is_global_handler(false), global_handler_(nullptr) {}
36583658

36593659
using H = std::function<void(Req&, Resp&)>;
36603660
struct VH {
@@ -3695,7 +3695,9 @@ template <typename Req, typename Resp> struct api {
36953695

36963696
void add_subapi(std::string prefix, const self& subapi) {
36973697
subapi.routes_map_.for_all_routes([this, prefix](auto r, VH h) {
3698-
if (!r.empty() && r.back() == '/')
3698+
if (r.empty() || r == "/")
3699+
h.url_spec = prefix;
3700+
else if (r.back() == '/')
36993701
h.url_spec = prefix + r;
37003702
else
37013703
h.url_spec = prefix + '/' + r;
@@ -3713,13 +3715,11 @@ template <typename Req, typename Resp> struct api {
37133715
std::cout << std::endl;
37143716
}
37153717
auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const {
3716-
if(is_global_handler)
3717-
{
3718-
global_handler_(request, response);
3719-
return;
3718+
if (is_global_handler) {
3719+
global_handler_(request, response);
3720+
return;
37203721
}
3721-
if (route == last_called_route_)
3722-
{
3722+
if (route == last_called_route_) {
37233723
if (last_handler_.verb == ANY or parse_verb(method) == last_handler_.verb) {
37243724
request.url_spec = last_handler_.url_spec;
37253725
last_handler_.handler(request, response);
@@ -3730,7 +3730,8 @@ template <typename Req, typename Resp> struct api {
37303730

37313731
// skip the last / of the url and trim spaces.
37323732
std::string_view route2(route);
3733-
while (route2.size() > 1 and (route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
3733+
while (route2.size() > 1 and
3734+
(route2[route2.size() - 1] == '/' || route2[route2.size() - 1] == ' '))
37343735
route2 = route2.substr(0, route2.size() - 1);
37353736

37363737
auto it = routes_map_.find(route2);

single_headers/lithium_mysql.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cassert>
1414
#include <cstring>
1515
#include <deque>
16+
#include <functional>
1617
#include <iostream>
1718
#include <map>
1819
#include <memory>
@@ -2518,6 +2519,7 @@ template <typename I> struct sql_database {
25182519
};
25192520

25202521
}
2522+
25212523
#endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH
25222524

25232525

single_headers/lithium_pgsql.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cassert>
1515
#include <cstring>
1616
#include <deque>
17+
#include <functional>
1718
#include <iostream>
1819
#if __APPLE__
1920
#include <libkern/OSByteOrder.h>
@@ -2192,6 +2193,7 @@ template <typename I> struct sql_database {
21922193
};
21932194

21942195
}
2196+
21952197
#endif // LITHIUM_SINGLE_HEADER_GUARD_LI_SQL_SQL_DATABASE_HH
21962198

21972199

0 commit comments

Comments
 (0)