21{
22 std::cout << "=== Simple HTTP Server Demo ===" << std::endl;
23
24
25 auto server = std::make_shared<core::http_server>(
"simple_http_server");
26
27
29 {
32 response.
set_body_string(
"Hello, World! Welcome to NetworkSystem HTTP Server.");
33 response.
set_header(
"Content-Type",
"text/plain");
34 return response;
35 });
36
38 {
41
44 response.
set_header(
"Content-Type",
"text/plain");
45 return response;
46 });
47
49 {
52
55 response.
set_header(
"Content-Type",
"text/plain");
56 return response;
57 });
58
60 {
63
66 response.
set_header(
"Content-Type",
"text/plain");
67 return response;
68 });
69
70
71 std::cout << "Starting HTTP server on port 8080..." << std::endl;
72 auto result =
server->start(8080);
73 if (result.is_err())
74 {
75 std::cerr << "Failed to start server: " << result.error().message << std::endl;
76 return 1;
77 }
78
79 std::cout << "Server started successfully!" << std::endl;
80 std::cout << "Try these URLs:" << std::endl;
81 std::cout << " http://localhost:8080/" << std::endl;
82 std::cout << " http://localhost:8080/api/hello?name=John" << std::endl;
83 std::cout << " http://localhost:8080/users/123" << std::endl;
84 std::cout << "\nPress Ctrl+C to stop the server..." << std::endl;
85
86
88
89 return 0;
90}
@ server
Server-side handling of a request.
Context for an HTTP request with parsed components.
auto get_path_param(const std::string &name) const -> std::optional< std::string >
Get path parameter value.
auto get_query_param(const std::string &name) const -> std::optional< std::string >
Get query parameter value.
internal::http_request request
auto get_body_string() const -> std::string
Get body as string.
Represents an HTTP response message.
auto set_body_string(const std::string &content) -> void
Set body from string.
auto set_header(const std::string &name, const std::string &value) -> void
Set a header value.