๐๐ป ์ฃผ์ ๋ณ๊ฒฝ์ฌํญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
The major changes are as follows.
โ๏ธ ๋ฉํฐ ์๋ฒ / multi server
— ํฌํธ ๋ฒํธ๋ฅผ ๋ฐ๊ฟ์ ์ฌ๋ฌ๊ฐ์ ์๋ฒ๋ฅผ ๋์์ ์คํ ๊ฐ๋ฅํฉ๋๋ค.
You can run multiple servers simultaneously by changing the port number.
— Main.cpp
Server server1("127.0.0.1", 5080);
server1.set_handler([&](const std::string& req) { ... }
Server server2("127.0.0.1", 6080);
server2.set_handler([&](const std::string& req) { ... }
std::thread server_thread(&Server::start, &server1);
std::thread server_thread2(&Server::start, &server2);
server1.stop();
server2.stop();
if (server_thread.joinable()) {
server_thread.join();
}
if (server_thread2.joinable()) {
server_thread2.join();
}
โ๏ธ ์๋ฒ๋ณ๋ก ์ ์ ํ์ผ ์๋น ๋ฐ ๋ผ์ฐํธ ์ค์ ๊ฐ๋ฅํฉ๋๋ค.
Static file serving and routing can be configured per server.
— Main.cpp
Router server1_router("../public1");
server1_router.add_route("/test1", "test1.html");
server1_router.add_route("/test2", "test2.html");
Router server2_router("../public2");
server2_router.add_route("/test1", "test1.html");
server2_router.add_route("/test2", "test2.html");
— Router.h
public:
void add_route(const std::string& path, const std::string& file_name) {
route_map[path] = file_name;
}
private:
std::map<std::string, std::string> route_map;
— Router.cpp
std::string target_file;
// 1. ๋ฑ๋ก๋ ๋ผ์ฐํธ๊ฐ ์๋์ง ํ์ธ
// Check if there are registered routes
if (route_map.count(path)) {
target_file = route_map[path];
}
// 2. ๊ธฐ๋ณธ ๊ฒฝ๋ก("/") ์ฒ๋ฆฌ
// Handling default path ("/")
else if (path == "/") {
target_file = "index.html";
}
// 3. ๋ฑ๋ก๋์ง ์์ ๊ฒฝ๋ก๋ ๊ทธ๋ฅ ๊ฒฝ๋ก ์ด๋ฆ๋๋ก ํ์ผ ์๋ (๋๋ 404)
// For unregistered paths, just attempt the file as the path name (or 404)
else {
target_file = path;
}
std::string full_path = doc_root + "/" + target_file;
โ๏ธ Servre listening ๋ฉ์ธ์ง ์ดํ CMD> ์ถ๋ ฅํ๋๋ก ์์ ํ์ต๋๋ค.
Modified to output CMD> after the Servre listening message.
— Main.cpp
std::this_thread::sleep_for(std::chrono::milliseconds(200));
โ๏ธ ๋๋ฒ๊น
์ฉ ๋ฉ์ธ์ง๋ ์ฝ๋ฉํธ ์ฒ๋ฆฌ ๋์ต๋๋ค.
Debugging messages have been commented out.
๐๐ป๋น๋ / Build
cd build
cmake ..
make
๐๐ป ์คํ / Run
MacBookAir build % ./main
--- Starting Server Application ---
Server listening on port 5080...
Server listening on port 6080...
CMD>
๐๐ป๋ธ๋ผ์ฐ์ ์ ์ / Access Browser
— 5080๊ณผ 6080 ํฌํธ๋ก ๋ ๊ฐ์ ์๋ฒ๊ฐ ์คํ๋ฉ๋๋ค.
Two servers are running on ports 5080 and 6080.
— ๋ธ๋ผ์ฐ์ ์์ ๊ฐ ํฌํธ๋ก ์ ์ํ๋ฉด ๋ฉ๋๋ค.
You can connect to each port in your browser.
http://localhost:5080
http://localhoset:6080