Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
simple_http_server.cpp File Reference
#include <internal/http/http_server.h>
#include <iostream>
#include <chrono>
#include <thread>
Include dependency graph for simple_http_server.cpp:

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 20 of file simple_http_server.cpp.

21{
22 std::cout << "=== Simple HTTP Server Demo ===" << std::endl;
23
24 // Create HTTP server directly for HTTP-specific features
25 auto server = std::make_shared<core::http_server>("simple_http_server");
26
27 // Register routes
28 server->get("/", [](const core::http_request_context&)
29 {
31 response.status_code = 200;
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
37 server->get("/api/hello", [](const core::http_request_context& ctx)
38 {
40 response.status_code = 200;
41
42 auto name = ctx.get_query_param("name").value_or("Guest");
43 response.set_body_string("Hello, " + name + "!");
44 response.set_header("Content-Type", "text/plain");
45 return response;
46 });
47
48 server->get("/users/:id", [](const core::http_request_context& ctx)
49 {
51 response.status_code = 200;
52
53 auto user_id = ctx.get_path_param("id").value_or("unknown");
54 response.set_body_string("User ID: " + user_id);
55 response.set_header("Content-Type", "text/plain");
56 return response;
57 });
58
59 server->post("/api/echo", [](const core::http_request_context& ctx)
60 {
62 response.status_code = 200;
63
64 auto body = ctx.request.get_body_string();
65 response.set_body_string("Echo: " + body);
66 response.set_header("Content-Type", "text/plain");
67 return response;
68 });
69
70 // Start server on port 8080
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 // Wait for server to stop
87 server->wait_for_stop();
88
89 return 0;
90}
@ server
Server-side handling of a request.
Context for an HTTP request with parsed components.
Definition http_server.h:71
auto get_path_param(const std::string &name) const -> std::optional< std::string >
Get path parameter value.
Definition http_server.h:95
auto get_query_param(const std::string &name) const -> std::optional< std::string >
Get query parameter value.
Definition http_server.h:80
auto get_body_string() const -> std::string
Get body as string.
Represents an HTTP response message.
Definition http_types.h:162
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.

References kcenon::network::internal::http_request::get_body_string(), kcenon::network::core::http_request_context::get_path_param(), kcenon::network::core::http_request_context::get_query_param(), kcenon::network::core::http_request_context::request, kcenon::network::internal::http_response::set_body_string(), kcenon::network::internal::http_response::set_header(), and kcenon::network::internal::http_response::status_code.

Here is the call graph for this function: