@@ -8,20 +8,24 @@ export const EXAMPLES = {
88 id : "http-ping" ,
99 title : "HTTP: /api/ping" ,
1010 desc : "A minimal JSON ping route." ,
11- code : `#include <vix/app/App.hpp>
12- using vix::App;
11+ code : `#include <vix.hpp>
1312
14- int main() {
13+ using namespace vix;
14+
15+ int main()
16+ {
1517 App app;
1618
17- app.get("/api/ping", [](auto& req, auto& res) {
19+ app.get("/api/ping", [](Request &, Response &res)
20+ {
1821 res.json({
1922 "ok", true,
2023 "message", "pong"
2124 });
2225 });
2326
24- return app.listen(8080);
27+ app.run(8080);
28+ return 0;
2529}` ,
2630 run : `vix run server.cpp` ,
2731 out : `http :8080/api/ping
@@ -36,25 +40,29 @@ HTTP/1.1 200 OK
3640 id : "query-params" ,
3741 title : "HTTP: query params" ,
3842 desc : "Read query values with defaults." ,
39- code : `#include <vix/app/App .hpp>
43+ code : `#include <vix.hpp>
4044#include <string>
41- using vix::App;
4245
43- int main() {
46+ using namespace vix;
47+
48+ int main()
49+ {
4450 App app;
4551
46- app.get("/search", [](auto& req, auto& res) {
52+ app.get("/search", [](Request &req, Response &res)
53+ {
4754 const std::string q = req.query_value("q", "");
48- const std::string pageStr = req.query_value("page", "1");
55+ const std::string page = req.query_value("page", "1");
4956
5057 res.json({
5158 "ok", true,
5259 "q", q,
53- "page", pageStr
60+ "page", page
5461 });
5562 });
5663
57- return app.listen(8080);
64+ app.run(8080);
65+ return 0;
5866}` ,
5967 run : `vix run server.cpp` ,
6068 out : `http ":8080/search?q=vix&page=2"
@@ -70,33 +78,52 @@ HTTP/1.1 200 OK
7078 id : "middleware-required-header" ,
7179 title : "Middleware: require header (401 demo)" ,
7280 desc : "Reject requests unless a required header is present." ,
73- code : `#include <vix/app/App.hpp>
74- using vix::App;
81+ code : `#include <vix.hpp>
82+ #include <vix/middleware/app/adapter.hpp>
83+
84+ using namespace vix;
7585
76- int main() {
86+ int main()
87+ {
7788 App app;
7889
79- // Pseudo middleware (concept)
80- // In your real code, plug your middleware function in the proper pipeline.
81- app.use([](auto& req, auto& res, auto next) {
82- const auto v = req.header("x-demo");
83- if (!v || v->empty()) {
90+ // ------------------------------------------------------------
91+ // Middleware inline: require header x-demo: 1
92+ // ------------------------------------------------------------
93+ auto require_demo_header =
94+ middleware::HttpMiddleware([](Request &req,
95+ Response &res,
96+ middleware::Next next)
97+ {
98+ const std::string value = req.header("x-demo");
99+
100+ if (value.empty() || value != "1")
101+ {
84102 res.status(401).json({
85- "ok", false,
86103 "error", "unauthorized",
87104 "hint", "Missing or invalid header",
105+ "ok", false,
88106 "required_header", "x-demo"
89107 });
90- return;
108+ return; // block request
91109 }
92- next();
93- });
94110
95- app.get("/api/ping", [](auto& req, auto& res) {
96- res.json({ "ok", true, "message", "pong" });
97- });
111+ next(); });
98112
99- return app.listen(8080);
113+ // Attach middleware ONLY to /api/ping
114+ middleware::app::install_exact(
115+ app,
116+ "/api/ping",
117+ middleware::app::adapt(require_demo_header));
118+
119+ // ------------------------------------------------------------
120+ // Route
121+ // ------------------------------------------------------------
122+ app.get("/api/ping", [](Request &, Response &res)
123+ { res.json({"ok", true, "message", "pong"}); });
124+
125+ app.run(8080);
126+ return 0;
100127}` ,
101128 run : `vix run server.cpp` ,
102129 out : `http :8080/api/ping
@@ -114,7 +141,7 @@ HTTP/1.1 200 OK
114141 "ok": true,
115142 "message": "pong"
116143}` ,
117- note : "This matches your 401 output. Keep the error shape stable: ok/error/hint/required_header." ,
144+ note : "Keep the error shape stable: ok/error/hint/required_header. This matches your 401 output ." ,
118145 } ,
119146
120147 {
@@ -123,38 +150,49 @@ HTTP/1.1 200 OK
123150 desc : "Structured logs, useful for debugging and CI." ,
124151 code : `// Nothing special in code.
125152// Use the CLI flags to control log format.` ,
126- run : `vix run server.cpp --log-format=json --log-level=debug
127-
128- or
129- vix run server.cpp --log-format=json-pretty --log-level=debug --log-color=always` ,
153+ run : `vix run server.cpp --log-format=json --log-level=debug` ,
130154 out : `{"level":"debug","msg":"server listening","port":8080,"ts":"..."}` ,
155+ note : `Alternative (pretty + colored):
156+ vix run server.cpp --log-format=json-pretty --log-level=debug --log-color=always` ,
131157 } ,
132158
133159 {
134160 id : "http-ws-together" ,
135161 title : "HTTP + WebSocket in one process" ,
136- desc : "Serve HTTP routes and WebSocket events from a single Vix app." ,
137- code : `#include <vix/app/App.hpp>
138- // pseudo: websocket headers depend on your module layout
139- // #include <vix/ws/WebSocket.hpp>
162+ desc : "Serve HTTP routes and typed WebSocket events from a single runtime." ,
163+ code : `#include <vix.hpp>
164+ #include <vix/websocket/AttachedRuntime.hpp>
140165
141- using vix::App ;
166+ using namespace vix;
142167
143- int main() {
144- App app;
168+ int main()
169+ {
170+ // Default config path "config/config.json" and port 8080
171+ vix::serve_http_and_ws([](auto &app, auto &ws)
172+ {
173+ // Minimal HTTP route
174+ app.get("/api/ping", [](auto&, auto& res) {
175+ res.json({
176+ "ok", true,
177+ "message", "pong"
178+ });
179+ });
145180
146- app.get("/api/ping", [](auto& req, auto& res) {
147- res.json({ "ok", true, "message", "pong" });
181+ // Minimal WebSocket handler: echo/broadcast chat.message
182+ ws.on_typed_message(
183+ [&ws](auto& session,
184+ const std::string& type,
185+ const vix::json::kvs& payload)
186+ {
187+ (void)session;
188+
189+ if (type == "chat.message") {
190+ ws.broadcast_json("chat.message", payload);
191+ }
192+ });
148193 });
149194
150- // app.ws("/ws", [](auto& ws) {
151- // ws.on_open([](auto& ctx){});
152- // ws.on_message([](auto& ctx, std::string_view msg){
153- // ctx.send(msg); // echo
154- // });
155- // });
156-
157- return app.listen(8080);
195+ return 0;
158196}` ,
159197 run : `vix run server.cpp` ,
160198 out : `http :8080/api/ping
@@ -163,7 +201,7 @@ HTTP/1.1 200 OK
163201 "ok": true,
164202 "message": "pong"
165203}` ,
166- note : "Keep the example simple and aligned with your actual ws API names once stabilized ." ,
204+ note : "Keep this aligned with the stable ws API names (typed messages + broadcast_json) ." ,
167205 } ,
168206
169207 {
@@ -178,7 +216,7 @@ vix publish 0.7.0 --notes "Add tree helper"`,
178216 run : `vix add <author>/<pkg>@<version>` ,
179217 out : `✔ added: gaspardkirira/tree@0.7.0
180218✔ configured: include paths + CMake integration` ,
181- note : "The web UI will move to registry.vixcpp.com on the VPS ." ,
219+ note : "Keep outputs short and stable; avoid promising features not shipped yet ." ,
182220 } ,
183221 ] ,
184222} ;
0 commit comments