-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
121 lines (102 loc) · 3.63 KB
/
example.cpp
File metadata and controls
121 lines (102 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <csignal>
#include <logging/log.h>
#include <logging/sink.h>
#include <web/env.h>
#include <web/loop.h>
#include <web/response.h>
#include <web/routing.h>
#include <json/json.h>
#include <coro/thread.h>
http::response::msg make_success_msg(std::string&& content_type, std::string&& body) {
return {
{http::response::status_code::ok},
{
{"Content-Type", std::move(content_type)},
{"Content-Length", std::to_string(body.size())}
},
std::move(body)
};
}
int main(){
// Register signal handler for graceful shutdown
web::loop::reg_stop_signal(SIGINT);
// Initialize the thread pool
if (!coro::thread::init(4)) {
std::println("Failed to initialize thread pool");
return 1;
}
// Configure the logging system
logging::add_sink(
std::make_unique<logging::sink::file>("app.log")
);
// Configure the web server environment
web::env::chain()
.set_listen_addr(web::ip::v4::from_string("127.0.0.1:8080"))
.set_root_path("www")
.set_max_worker_conn(128)
.set_worker_count(16)
.set_index_files({"index.html", "index.htm"})
/* Options for custom error page provider:
.set_error_page_provider([](http::response::status_code) -> std::string_view {})
*/
;
// Simple static route
web::routing::get("/hello", [](const http::request::msg&) -> web::response::task {
return web::response::msg(
make_success_msg("text/plain", "Hello, World!")
);
});
// Dynamic route with parameter
web::routing::dynamic::get("/user/{id}", [](const http::request::msg&, const std::unordered_map<std::string, std::string>& params) -> web::response::task {
return web::response::msg(
make_success_msg(
"text/plain",
std::format("User ID: {}", params.at("id"))
)
);
});
// JSON response route
web::routing::get("/data", [](const http::request::msg&) -> web::response::task {
Json::object obj;
obj["message"] = "Hello, JSON!";
obj["value"] = 42;
obj["array"] = Json::array{1, 2, 3};
return web::response::msg(
make_success_msg("application/json", std::format("{}", obj))
);
});
// POST route handling JSON body
web::routing::post("/submit", [](const http::request::msg& req) -> web::response::task {
auto body = req.body;
auto json = Json::parse(body);
if (!json) {
return web::response::error(http::response::status_code::bad_request);
} else {
auto object = json->as<Json::object>();
if (!object) {
return web::response::error(http::response::status_code::bad_request);
}
object->get().emplace("status", "received");
return web::response::msg(
make_success_msg(
"application/json",
std::format("{}", object->get())
)
);
}
});
// Async
web::routing::get("/async", [](const http::request::msg&) -> web::response::task {
using namespace std::literals;
// Simulate async operation with timeout
co_await io::awaiter::sleep{1s};
// Optional: get settings like fd, client_addr, timeout
auto _ = co_await web::response::task::get_settings{};
// Return response
co_return co_await web::response::msg(
make_success_msg("text/plain", "This is an async response!")
);
});
// Start the event loop
web::loop::run();
}