Skip to content

Commit b722552

Browse files
committed
refactor(p2p_http): reorganize examples, remove legacy files and add structured examples suite
1 parent 62abf64 commit b722552

14 files changed

Lines changed: 1303 additions & 153 deletions

README.md

Lines changed: 322 additions & 151 deletions
Large diffs are not rendered by default.

examples/01_ping_route_basic.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
*
3+
* @file 01_ping_route_basic.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/01_ping_route_basic.cpp
18+
//
19+
// Then:
20+
// curl http://127.0.0.1:8080/p2p/ping
21+
22+
#include <iostream>
23+
#include <memory>
24+
#include <string_view>
25+
26+
#include <vix/app/App.hpp>
27+
#include <vix/p2p/Node.hpp>
28+
#include <vix/p2p/P2P.hpp>
29+
#include <vix/p2p_http/P2PHttp.hpp>
30+
#include <vix/p2p_http/P2PHttpOptions.hpp>
31+
#include <vix/console.hpp>
32+
33+
namespace
34+
{
35+
std::shared_ptr<vix::p2p::Node> make_node()
36+
{
37+
vix::p2p::NodeConfig cfg;
38+
cfg.node_id = "p2p-http-ping";
39+
cfg.listen_port = 9101;
40+
cfg.on_log = [](std::string_view line)
41+
{
42+
std::cout << line << "\n";
43+
};
44+
45+
return vix::p2p::make_tcp_node(cfg);
46+
}
47+
48+
void configure_routes(vix::App &app, vix::p2p::P2PRuntime &runtime)
49+
{
50+
vix::p2p_http::P2PHttpOptions opt;
51+
opt.prefix = "/p2p";
52+
opt.enable_ping = true;
53+
opt.enable_status = false;
54+
opt.enable_logs = false;
55+
opt.enable_live_logs = false;
56+
opt.enable_peers = false;
57+
58+
vix::p2p_http::registerRoutes(app, runtime, opt);
59+
}
60+
61+
void run_example()
62+
{
63+
auto node = make_node();
64+
vix::p2p::P2PRuntime runtime(node);
65+
runtime.start();
66+
67+
vix::App app;
68+
configure_routes(app, runtime);
69+
70+
std::cout << "p2p_http ping example\n";
71+
std::cout << "GET http://127.0.0.1:8080/p2p/ping\n";
72+
73+
app.listen(8080, []()
74+
{ vix::console.info("API listening on", 8080); });
75+
76+
runtime.stop();
77+
vix::p2p_http::shutdown_live_logs();
78+
}
79+
}
80+
81+
int main()
82+
{
83+
run_example();
84+
return 0;
85+
}

examples/02_status_route_basic.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
*
3+
* @file 02_status_route_basic.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/02_status_route_basic.cpp
18+
//
19+
// Then:
20+
// curl http://127.0.0.1:8081/p2p/status
21+
22+
#include <iostream>
23+
#include <memory>
24+
#include <string_view>
25+
26+
#include <vix/app/App.hpp>
27+
#include <vix/p2p/Node.hpp>
28+
#include <vix/p2p/P2P.hpp>
29+
#include <vix/p2p_http/P2PHttp.hpp>
30+
#include <vix/p2p_http/P2PHttpOptions.hpp>
31+
#include <vix/console.hpp>
32+
33+
namespace
34+
{
35+
std::shared_ptr<vix::p2p::Node> make_node()
36+
{
37+
vix::p2p::NodeConfig cfg;
38+
cfg.node_id = "p2p-http-status";
39+
cfg.listen_port = 9102;
40+
cfg.on_log = [](std::string_view line)
41+
{
42+
std::cout << line << "\n";
43+
};
44+
45+
return vix::p2p::make_tcp_node(cfg);
46+
}
47+
48+
void run_example()
49+
{
50+
auto node = make_node();
51+
vix::p2p::P2PRuntime runtime(node);
52+
runtime.start();
53+
54+
vix::p2p_http::P2PHttpOptions opt;
55+
opt.prefix = "/p2p";
56+
opt.enable_ping = false;
57+
opt.enable_status = true;
58+
opt.enable_logs = false;
59+
opt.enable_live_logs = false;
60+
opt.enable_peers = false;
61+
62+
vix::App app;
63+
vix::p2p_http::registerRoutes(app, runtime, opt);
64+
65+
std::cout << "p2p_http status example\n";
66+
std::cout << "GET http://127.0.0.1:8081/p2p/status\n";
67+
68+
app.listen(8081, []()
69+
{ vix::console.info("API listening on", 8081); });
70+
71+
runtime.stop();
72+
vix::p2p_http::shutdown_live_logs();
73+
}
74+
}
75+
76+
int main()
77+
{
78+
run_example();
79+
return 0;
80+
}

examples/03_custom_prefix.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
*
3+
* @file 03_custom_prefix.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/03_custom_prefix.cpp
18+
//
19+
// Then:
20+
// curl http://127.0.0.1:8082/control/ping
21+
// curl http://127.0.0.1:8082/control/status
22+
23+
#include <iostream>
24+
#include <memory>
25+
#include <string_view>
26+
27+
#include <vix/app/App.hpp>
28+
#include <vix/p2p/Node.hpp>
29+
#include <vix/p2p/P2P.hpp>
30+
#include <vix/p2p_http/P2PHttp.hpp>
31+
#include <vix/p2p_http/P2PHttpOptions.hpp>
32+
#include <vix/console.hpp>
33+
34+
namespace
35+
{
36+
std::shared_ptr<vix::p2p::Node> make_node()
37+
{
38+
vix::p2p::NodeConfig cfg;
39+
cfg.node_id = "p2p-http-prefix";
40+
cfg.listen_port = 9103;
41+
cfg.on_log = [](std::string_view line)
42+
{
43+
std::cout << line << "\n";
44+
};
45+
46+
return vix::p2p::make_tcp_node(cfg);
47+
}
48+
49+
void run_example()
50+
{
51+
auto node = make_node();
52+
vix::p2p::P2PRuntime runtime(node);
53+
runtime.start();
54+
55+
vix::p2p_http::P2PHttpOptions opt;
56+
opt.prefix = "/control";
57+
opt.enable_ping = true;
58+
opt.enable_status = true;
59+
opt.enable_logs = false;
60+
opt.enable_live_logs = false;
61+
opt.enable_peers = false;
62+
63+
vix::App app;
64+
vix::p2p_http::registerRoutes(app, runtime, opt);
65+
66+
std::cout << "custom prefix example\n";
67+
std::cout << "GET http://127.0.0.1:8082/control/ping\n";
68+
std::cout << "GET http://127.0.0.1:8082/control/status\n";
69+
70+
app.listen(8082, []()
71+
{ vix::console.info("API listening on", 8082); });
72+
73+
runtime.stop();
74+
vix::p2p_http::shutdown_live_logs();
75+
}
76+
}
77+
78+
int main()
79+
{
80+
run_example();
81+
return 0;
82+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
*
3+
* @file 04_connect_route_basic.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// Terminal 1:
18+
// vix run examples/04_connect_route_basic.cpp --run target
19+
//
20+
// Terminal 2:
21+
// vix run examples/04_connect_route_basic.cpp --run api
22+
//
23+
// Then:
24+
// curl -X POST http://127.0.0.1:8083/p2p/connect \
25+
// -H "content-type: application/json" \
26+
// -d '{"host":"127.0.0.1","port":9201,"scheme":"tcp"}'
27+
28+
#include <cstdint>
29+
#include <iostream>
30+
#include <memory>
31+
#include <string>
32+
#include <string_view>
33+
34+
#include <vix/app/App.hpp>
35+
#include <vix/p2p/Node.hpp>
36+
#include <vix/p2p/P2P.hpp>
37+
#include <vix/p2p_http/P2PHttp.hpp>
38+
#include <vix/p2p_http/P2PHttpOptions.hpp>
39+
#include <vix/console.hpp>
40+
41+
namespace
42+
{
43+
constexpr std::uint16_t kTargetPort = 9201;
44+
constexpr std::uint16_t kApiNodePort = 9202;
45+
constexpr std::uint16_t kHttpPort = 8083;
46+
47+
std::shared_ptr<vix::p2p::Node> make_node(const std::string &node_id, std::uint16_t port)
48+
{
49+
vix::p2p::NodeConfig cfg;
50+
cfg.node_id = node_id;
51+
cfg.listen_port = port;
52+
cfg.on_log = [](std::string_view line)
53+
{
54+
std::cout << line << "\n";
55+
};
56+
return vix::p2p::make_tcp_node(cfg);
57+
}
58+
59+
void run_target()
60+
{
61+
auto node = make_node("connect-target", kTargetPort);
62+
node->start();
63+
64+
std::cout << "target node running on tcp://" << "127.0.0.1:" << kTargetPort << "\n";
65+
node->wait();
66+
}
67+
68+
void run_api()
69+
{
70+
auto node = make_node("connect-api", kApiNodePort);
71+
vix::p2p::P2PRuntime runtime(node);
72+
runtime.start();
73+
74+
vix::p2p_http::P2PHttpOptions opt;
75+
opt.prefix = "/p2p";
76+
opt.enable_ping = true;
77+
opt.enable_status = true;
78+
opt.enable_logs = false;
79+
opt.enable_live_logs = false;
80+
opt.enable_peers = true;
81+
82+
vix::App app;
83+
vix::p2p_http::registerRoutes(app, runtime, opt);
84+
85+
std::cout << "POST http://127.0.0.1:" << kHttpPort << "/p2p/connect\n";
86+
std::cout << R"(json: {"host":"127.0.0.1","port":9201,"scheme":"tcp"})" << "\n";
87+
88+
app.listen(kHttpPort, []()
89+
{ vix::console.info("API listening on", kHttpPort); });
90+
91+
runtime.stop();
92+
vix::p2p_http::shutdown_live_logs();
93+
}
94+
95+
void run_example(int argc, char **argv)
96+
{
97+
if (argc < 2)
98+
{
99+
std::cout << "usage:\n";
100+
std::cout << " vix run examples/p2p_http/04_connect_route_basic.cpp --run target\n";
101+
std::cout << " vix run examples/p2p_http/04_connect_route_basic.cpp --run api\n";
102+
return;
103+
}
104+
105+
const std::string mode = argv[1];
106+
if (mode == "target")
107+
{
108+
run_target();
109+
return;
110+
}
111+
112+
if (mode == "api")
113+
{
114+
run_api();
115+
return;
116+
}
117+
118+
std::cout << "unknown mode: " << mode << "\n";
119+
}
120+
}
121+
122+
int main(int argc, char **argv)
123+
{
124+
run_example(argc, argv);
125+
return 0;
126+
}

0 commit comments

Comments
 (0)