Ver3.0 ๐ C++ ์น ๊ฐ๋ฐ์ ์๋ก์ด ์งํ

๐ C++ ์น ๊ฐ๋ฐ์ ์๋ก์ด ์งํ
์๋ ! ์ค๋์ C++๋ก ์น ์๋ฒ๋ฅผ ๋ง๋๋ ์ด์ผ๊ธฐ๋ฅผ ํด๋ณผ๊น ํด. "C++๋ก ์น ์๋ฒ๋ฅผ?" ํ๊ณ ์์ํดํ ์๋ ์์ด. ๋ณดํต ์น ๊ฐ๋ฐ์ด๋ผ๊ณ ํ๋ฉด Node.js, Python Flask, Java Spring ๊ฐ์ ๊ฑธ ๋ ์ฌ๋ฆฌ์์? ๊ทธ๋ฐ๋ฐ ๋ง์ด์ผ, C++๋ ์น ๊ฐ๋ฐ์์ ์์ฒญ๋ ๊ฐ์ ์ ๊ฐ์ง๊ณ ์์ด. ํนํ ๊ณ ์ฑ๋ฅ์ด ํ์ํ ์๋น์ค, ์ค์๊ฐ ์ฒ๋ฆฌ๊ฐ ์ค์ํ ์์คํ ์์๋ C++๋งํ ๊ฒ ์์ง! ๐
์์ฆ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ C++ ์น ๊ฐ๋ฐ ๊ด๋ จ ์ฌ๋ฅ์ ์ฐพ๋ ์ฌ๋๋ค์ด ๋์ด๋๊ณ ์์ด. ์๋ํ๋ฉด ๋ง์ดํฌ๋ก์๋น์ค ์ํคํ ์ฒ๊ฐ ๋์ธ๊ฐ ๋๋ฉด์, ํน์ ๊ธฐ๋ฅ๋ง ๋ด๋นํ๋ ์ด๊ณ ์ ์๋ฒ๊ฐ ํ์ํ ๊ฒฝ์ฐ๊ฐ ๋ง์์ก๊ฑฐ๋ . ๊ทธ๋ด ๋ C++๋ก ๋ง๋ ์๋ฒ๋ ์ ๋ง ๋น์ ๋ฐํด!
๐ฏ ์ C++๋ก ์น ์๋ฒ๋ฅผ ๋ง๋ค๊น?
๋จผ์ ๊ทผ๋ณธ์ ์ธ ์ง๋ฌธ๋ถํฐ ์์ํด๋ณด์. ์ ๊ตณ์ด C++๋ก ์น ์๋ฒ๋ฅผ ๋ง๋ค์ด์ผ ํ ๊น? ์์งํ ๋งํ๋ฉด, ๊ฐ๋จํ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ผ๋ฉด Python์ด๋ Node.js๊ฐ ํจ์ฌ ํธํด. ๊ฐ๋ฐ ์๋๋ ๋น ๋ฅด๊ณ , ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํ๋ถํ๊ณ . ๊ทธ๋ฐ๋ฐ ๋ง์ด์ผ, ์ธ์์๋ "๊ฐ๋จํ" ๋ฌธ์ ๋ง ์๋ ๊ฒ ์๋์์? ๐ค
๐ก C++ ์น ์๋ฒ๊ฐ ๋น๋๋ ์๊ฐ๋ค:
1. ์ด๋น ์๋ง ๊ฑด์ ์์ฒญ์ ์ฒ๋ฆฌํด์ผ ํ ๋
๊ฒ์ ์๋ฒ, ๊ธ์ต ๊ฑฐ๋ ์์คํ
, ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์๋น์ค์ฒ๋ผ ์์ฒญ๋ ํธ๋ํฝ์ ๊ฐ๋นํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด. ์ด๋ด ๋ C++์ ์ฑ๋ฅ์ ์ ๋ง ์๋์ ์ด์ผ.
2. ๋ ์ดํด์(์ง์ฐ์๊ฐ)๊ฐ ์๋ช
์ธ ์๋น์ค
๋ฐ๋ฆฌ์ด ๋จ์์ ์๋ต ์๊ฐ์ด ์ค์ํ ์๋น์ค์์๋ C++๋งํ ๊ฒ ์์ด. ๊ฐ๋น์ง ์ปฌ๋ ์
์ด ์์ด์ ์์ธก ๊ฐ๋ฅํ ์ฑ๋ฅ์ ๋ณด์ฅํ๊ฑฐ๋ .
3. ์์คํ
๋ฆฌ์์ค๋ฅผ ์ต๋ํ ํ์ฉํด์ผ ํ ๋
๋ฉ๋ชจ๋ฆฌ์ CPU๋ฅผ ์ง์ ์ ์ดํ ์ ์์ด์, ํ๋์จ์ด ์ฑ๋ฅ์ 100% ๋์ด๋ผ ์ ์์ด. ํด๋ผ์ฐ๋ ๋น์ฉ์ ์ ๊ฐํ๋ ๋ฐ๋ ํฐ ๋์์ด ๋ผ!
4. ๊ธฐ์กด C++ ์ฝ๋๋ฒ ์ด์ค์ ํตํฉํด์ผ ํ ๋
์ด๋ฏธ C++๋ก ์์ฑ๋ ํต์ฌ ๋ก์ง์ด ์๋ค๋ฉด, ์น ์ธํฐํ์ด์ค๋ C++๋ก ๋ง๋๋ ๊ฒ ๊ฐ์ฅ ์์ฐ์ค๋ฝ์ง.
์ค์ ๋ก ๋ง์ ๋ํ ์๋น์ค๋ค์ด ํต์ฌ ๋ถ๋ถ์ C++๋ฅผ ์ฌ์ฉํ๊ณ ์์ด. Facebook์ ์ผ๋ถ ๋ฐฑ์๋, Google์ ์ฌ๋ฌ ์๋น์ค, ๊ฒ์ ํ์ฌ๋ค์ ์๋ฒ ์ธํ๋ผ... ์ด๋ฐ ๊ณณ๋ค์ ๋ชจ๋ ์ฑ๋ฅ์ด ๊ณง ๊ฒฝ์๋ ฅ์ด๊ฑฐ๋ ! ๐ช
๐ฆ Beast ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ ์ ๋ณต
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก Beast์ ๋ํด ์์๋ณด์! Beast๋ Boost ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ผ๋ถ๋ก, ์ ํํ ์ด๋ฆ์ Boost.Beast์ผ. ์ด ์น๊ตฌ๋ HTTP์ WebSocket ํ๋กํ ์ฝ์ ๊ตฌํํ๊ธฐ ์ํ C++ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ๋ฐ, Boost.Asio ์์์ ๋์ํด. ๐พ
Beast์ ํต์ฌ ์ฒ ํ
Beast๋ฅผ ๋ง๋ ๊ฐ๋ฐ์๋ค์ ๋ช ๊ฐ์ง ๋ช ํํ ๋ชฉํ๋ฅผ ๊ฐ์ง๊ณ ์์์ด:
๐จ ํค๋ ์จ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
๋ณ๋์ ์ปดํ์ผ์ด๋ ๋งํน์ด ํ์ ์์ด. ํค๋ ํ์ผ๋ง ํฌํจํ๋ฉด ๋ฐ๋ก ์ฌ์ฉํ ์ ์์ง. ์ด๊ฒ ์ผ๋ง๋ ํธํ์ง ๋ชฐ๋ผ!
โก ์ ๋ก ์ค๋ฒํค๋ ์ถ์ํ
๊ณ ์์ค API๋ฅผ ์ ๊ณตํ๋ฉด์๋ ์ฑ๋ฅ ์์ค์ด ๊ฑฐ์ ์์ด. ์ง์ ์์ผ ํ๋ก๊ทธ๋๋ฐํ๋ ๊ฒ๊ณผ ๋น์ทํ ์ฑ๋ฅ์ ๋ด๋ฉด์, ์ฝ๋๋ ํจ์ฌ ๊น๋ํ๊ฒ ์์ฑํ ์ ์์ด.
๐ง ์ ์ฐ์ฑ
๋๊ธฐ/๋น๋๊ธฐ ๋ชจ๋ ์ง์ํ๊ณ , ๋ค์ํ ์ฌ์ฉ ์ฌ๋ก์ ๋ง์ถฐ ์ปค์คํฐ๋ง์ด์งํ ์ ์์ด. ํ๋ ์์ํฌ๊ฐ ์๋๋ผ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ์, ๋ค๊ฐ ์ํ๋ ๋๋ก ์กฐํฉํด์ ์ธ ์ ์์ง.
๐ ํ์ค ์ค์
HTTP/1.1๊ณผ WebSocket RFC๋ฅผ ์ ํํ๊ฒ ๊ตฌํํ์ด. ์น ํ์ค์ ์ ๋๋ก ๋ฐ๋ฅด๋๊น ๋ค๋ฅธ ํด๋ผ์ด์ธํธ๋ค๊ณผ์ ํธํ์ฑ๋ ์๋ฒฝํด.
Beast ์ค์นํ๊ธฐ
Beast๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋จผ์ Boost ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํด. Boost 1.70 ์ด์ ๋ฒ์ ์ด๋ฉด Beast๊ฐ ํฌํจ๋์ด ์์ด. ์ค์น ๋ฐฉ๋ฒ์ ์ด์์ฒด์ ๋ง๋ค ์กฐ๊ธ์ฉ ๋ฌ๋ผ:
# Ubuntu/Debian
sudo apt-get install libboost-all-dev
# macOS (Homebrew)
brew install boost
# Windows (vcpkg)
vcpkg install boost-beast
๋๋ Boost ๊ณต์ ์น์ฌ์ดํธ์์ ์ง์ ๋ค์ด๋ก๋ํด์ ์ค์นํ ์๋ ์์ด. ํค๋ ์จ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ์ ์ค์น๊ฐ ๋๋๋ฉด ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํด! ๐
Beast๋ก ๊ฐ๋จํ HTTP ์๋ฒ ๋ง๋ค๊ธฐ
์ด์ ์ค์ ๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ณด์! ๋จผ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋๊ธฐ์ HTTP ์๋ฒ๋ถํฐ ์์ํ ๊ฒ. ์ด ์๋ฒ๋ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ์์ ๊ฐ๋จํ ์๋ต์ ๋๋ ค์ฃผ๋ ์ญํ ์ ํด.
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <iostream>
#include <string>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
// HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํจ์
template<class Body, class Allocator>
http::message_generator
handle_request(http::request<Body, http::basic_fields<Allocator>>&& req)
{
// ์๋ต ๊ฐ์ฒด ์์ฑ
http::response<http::string_body> res{http::status::ok, req.version()};
res.set(http::field::server, "Beast HTTP Server");
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
// ์๋ต ๋ณธ๋ฌธ ์์ฑ
res.body() = "<html><body><h1>Hello from Beast!</h1></body></html>";
res.prepare_payload();
return res;
}
int main()
{
try
{
auto const address = net::ip::make_address("0.0.0.0");
auto const port = static_cast<unsigned short>(8080);
net::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
std::cout << "์๋ฒ๊ฐ ํฌํธ " << port << "์์ ์์๋์์ต๋๋ค!" << std::endl;
while(true)
{
tcp::socket socket{ioc};
acceptor.accept(socket);
beast::flat_buffer buffer;
http::request<http::string_body> req;
http::read(socket, buffer, req);
auto response = handle_request(std::move(req));
beast::write(socket, std::move(response));
socket.shutdown(tcp::socket::shutdown_send);
}
}
catch(std::exception const& e)
{
std::cerr << "์๋ฌ: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
์ด ์ฝ๋๋ฅผ ํ๋์ฉ ๋ฏ์ด๋ณด์! ๐
๐ ์ฝ๋ ๋ถ์:
1. ๋ค์์คํ์ด์ค ๋ณ์นญ
namespace beast = boost::beast; ์ด๋ฐ ์์ผ๋ก ๋ณ์นญ์ ๋ง๋ค์ด์ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค์์ด. ๋งค๋ฒ boost::beast๋ฅผ ํ์ดํํ๋ ๊ฑด ๋๋ฌด ๊ท์ฐฎ์์?
2. handle_request ํจ์
ํ
ํ๋ฆฟ ํจ์๋ก ๋ง๋ค์ด์ ๋ค์ํ ํ์
์ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ด. ์ฌ๊ธฐ์๋ ๊ฐ๋จํ๊ฒ HTML ์๋ต์ ๋ฐํํ๊ณ ์์ง.
3. io_context
Boost.Asio์ ํต์ฌ ๊ฐ์ฒด์ผ. ๋ชจ๋ I/O ์์
์ ์ด ์ปจํ
์คํธ๋ฅผ ํตํด ์ด๋ฃจ์ด์ ธ. ์ฌ๊ธฐ์๋ ์ค๋ ๋ 1๊ฐ๋ง ์ฌ์ฉํ๋๋ก ์ค์ ํ์ด.
4. acceptor
ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ์ ๋ฐ์๋ค์ด๋ ์ญํ ์ ํด. TCP ์์ผ์ ๋ฆฌ์ค๋ํ๊ณ ์๋ค๊ฐ ์ฐ๊ฒฐ ์์ฒญ์ด ์ค๋ฉด ์๋ก์ด ์์ผ์ ์์ฑํ์ง.
5. ๋ฉ์ธ ๋ฃจํ
๋ฌดํ ๋ฃจํ๋ฅผ ๋๋ฉด์ ์ฐ๊ฒฐ์ ๋ฐ๊ณ , ์์ฒญ์ ์ฝ๊ณ , ์๋ต์ ๋ณด๋ด๋ ๊ณผ์ ์ ๋ฐ๋ณตํด. ๋๊ธฐ์์ด๋ผ์ ํ ๋ฒ์ ํ๋์ ์์ฒญ๋ง ์ฒ๋ฆฌํ ์ ์์ด.
๋น๋๊ธฐ HTTP ์๋ฒ๋ก ์ ๊ทธ๋ ์ด๋
์์ ๋๊ธฐ์ ์๋ฒ๋ ์ดํดํ๊ธฐ ์ฝ์ง๋ง, ์ค์ ํ๋ก๋์ ํ๊ฒฝ์์๋ ๋นํจ์จ์ ์ด์ผ. ํ ๋ฒ์ ํ๋์ ์์ฒญ๋ง ์ฒ๋ฆฌํ ์ ์์ผ๋๊น. ์ด์ ๋น๋๊ธฐ ๋ฒ์ ์ผ๋ก ์ ๊ทธ๋ ์ด๋ํด๋ณด์! ๐
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/strand.hpp>
#include <memory>
#include <iostream>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
// HTTP ์ธ์
ํด๋์ค
class http_session : public std::enable_shared_from_this<http_session>
{
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
http::request<http::string_body> req_;
public:
http_session(tcp::socket&& socket)
: stream_(std::move(socket))
{
}
void run()
{
net::dispatch(stream_.get_executor(),
beast::bind_front_handler(&http_session::do_read,
shared_from_this()));
}
private:
void do_read()
{
req_ = {};
stream_.expires_after(std::chrono::seconds(30));
http::async_read(stream_, buffer_, req_,
beast::bind_front_handler(&http_session::on_read,
shared_from_this()));
}
void on_read(beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec == http::error::end_of_stream)
return do_close();
if(ec)
{
std::cerr << "์ฝ๊ธฐ ์๋ฌ: " << ec.message() << std::endl;
return;
}
send_response();
}
void send_response()
{
auto response = std::make_shared<http::response<http::string_body>>(
http::status::ok, req_.version());
response->set(http::field::server, "Beast Async Server");
response->set(http::field::content_type, "application/json");
response->keep_alive(req_.keep_alive());
response->body() = R"({"message": "Hello from async Beast!", "status": "success"})";
response->prepare_payload();
auto sp = response;
http::async_write(stream_, *sp,
[self = shared_from_this(), sp](beast::error_code ec, std::size_t bytes)
{
self->on_write(ec, bytes, sp->need_eof());
});
}
void on_write(beast::error_code ec, std::size_t bytes_transferred,
bool close)
{
boost::ignore_unused(bytes_transferred);
if(ec)
{
std::cerr << "์ฐ๊ธฐ ์๋ฌ: " << ec.message() << std::endl;
return;
}
if(close)
return do_close();
do_read();
}
void do_close()
{
beast::error_code ec;
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
}
};
// ๋ฆฌ์ค๋ ํด๋์ค
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
public:
listener(net::io_context& ioc, tcp::endpoint endpoint)
: ioc_(ioc)
, acceptor_(net::make_strand(ioc))
{
beast::error_code ec;
acceptor_.open(endpoint.protocol(), ec);
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
acceptor_.bind(endpoint, ec);
acceptor_.listen(net::socket_base::max_listen_connections, ec);
}
void run()
{
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(&listener::on_accept,
shared_from_this()));
}
void on_accept(beast::error_code ec, tcp::socket socket)
{
if(!ec)
{
std::make_shared<http_session>(std::move(socket))->run();
}
do_accept();
}
};
int main()
{
try
{
auto const address = net::ip::make_address("0.0.0.0");
auto const port = static_cast<unsigned short>(8080);
auto const threads = std::max<int>(1, 4);
net::io_context ioc{threads};
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
std::cout << "๋น๋๊ธฐ ์๋ฒ๊ฐ ํฌํธ " << port << "์์ ์์๋์์ต๋๋ค!" << std::endl;
std::cout << "์ค๋ ๋ ์: " << threads << std::endl;
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back([&ioc] { ioc.run(); });
ioc.run();
for(auto& t : v)
t.join();
}
catch(std::exception const& e)
{
std::cerr << "์๋ฌ: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
์! ์ฝ๋๊ฐ ํจ์ฌ ๋ณต์กํด์ก์ง? ๐ ํ์ง๋ง ๊ฑฑ์ ๋ง. ํ๋์ฉ ๋ฏ์ด๋ณด๋ฉด ์ดํดํ ์ ์์ด!
๐ฏ ๋น๋๊ธฐ ์๋ฒ์ ํต์ฌ ๊ฐ๋ :
์ธ์
๊ธฐ๋ฐ ์ค๊ณ
๊ฐ ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ์ http_session ๊ฐ์ฒด๋ก ๊ด๋ฆฌํด. ์ด๋ ๊ฒ ํ๋ฉด ์ฌ๋ฌ ํด๋ผ์ด์ธํธ๋ฅผ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด. shared_ptr์ ์ฌ์ฉํด์ ์ธ์
์ ์๋ช
์ฃผ๊ธฐ๋ฅผ ์๋์ผ๋ก ๊ด๋ฆฌํ์ง.
์ฝ๋ฐฑ ์ฒด์ธ
do_read โ on_read โ send_response โ on_write ์ด๋ฐ ์์ผ๋ก ์ฝ๋ฐฑ์ด ์ฐ๊ฒฐ๋์ด ์์ด. ๊ฐ ๋น๋๊ธฐ ์์
์ด ์๋ฃ๋๋ฉด ๋ค์ ์ฝ๋ฐฑ์ด ํธ์ถ๋๋ ๊ตฌ์กฐ์ผ.
์ค๋ ๋ ํ
io_context๋ฅผ ์ฌ๋ฌ ์ค๋ ๋์์ ์คํํด์ CPU ์ฝ์ด๋ฅผ ์ต๋ํ ํ์ฉํด. 4๊ฐ ์ค๋ ๋๋ก ์ค์ ํ์ผ๋ 4๊ฐ์ ์์ฒญ์ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด!
ํ์์์ ์ฒ๋ฆฌ
stream_.expires_after(std::chrono::seconds(30))๋ก ํ์์์์ ์ค์ ํ์ด. ํด๋ผ์ด์ธํธ๊ฐ ์๋ตํ์ง ์์ผ๋ฉด ์๋์ผ๋ก ์ฐ๊ฒฐ์ ๋์ง.
๐ฆ cpp-httplib: ์ฌํํจ์ ๋ฏธํ
Beast๊ฐ ๊ฐ๋ ฅํ๊ธด ํ์ง๋ง, ์์งํ ์ข ๋ณต์กํ์ง? ํนํ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ์ต์ํ์ง ์๋ค๋ฉด ์ง์ ์ฅ๋ฒฝ์ด ๋์ ์ ์์ด. ๊ทธ๋์ ๋ฑ์ฅํ ๊ฒ ๋ฐ๋ก cpp-httplib์ผ! ์ด ์น๊ตฌ๋ ์ ๋ง ์ฌ์ฉํ๊ธฐ ์ฌ์. ๋ง์น Python์ Flask์ฒ๋ผ ๊ฐ๋จํ๊ฒ HTTP ์๋ฒ๋ฅผ ๋ง๋ค ์ ์๊ฑฐ๋ . ๐
cpp-httplib์ ํน์ง
๐ ํค๋ ์จ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Beast์ฒ๋ผ cpp-httplib๋ ํค๋ ํ์ผ ํ๋๋ง ํฌํจํ๋ฉด ๋! httplib.h ํ์ผ ํ๋๋ฉด ๋ชจ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ด.
๐ฐ ์ง๊ด์ ์ธ API
๋ผ์ฐํ
, ๋ฏธ๋ค์จ์ด, ํ์ผ ์
๋ก๋ ๋ฑ ์น ๊ฐ๋ฐ์ ํ์ํ ๊ธฐ๋ฅ๋ค์ด ์์ฃผ ์ง๊ด์ ์ธ API๋ก ์ ๊ณต๋ผ. ์ฝ๋๋ฅผ ๋ณด๋ฉด ๋ฐ๋ก ์ดํดํ ์ ์์ ์ ๋์ผ.
๐ ์์กด์ฑ ์ ๋ก
์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ํ ํ์ ์์ด! C++11 ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ง ์์ผ๋ฉด ๋ผ. ์ค์น๋ ๋น๋ ์ค์ ์ด ์ ๋ง ๊ฐ๋จํ์ง.
๐ HTTP/HTTPS ์ง์
OpenSSL๊ณผ ํจ๊ป ์ปดํ์ผํ๋ฉด HTTPS๋ ์ง์ํด. ๋ณด์์ด ์ค์ํ ์๋น์ค์๋ ์ฌ์ฉํ ์ ์์ด.
๐ฑ ํด๋ผ์ด์ธํธ ๊ธฐ๋ฅ
์๋ฒ๋ฟ๋ง ์๋๋ผ HTTP ํด๋ผ์ด์ธํธ ๊ธฐ๋ฅ๋ ์ ๊ณตํด. REST API๋ฅผ ํธ์ถํ๊ฑฐ๋ ๋ค๋ฅธ ์๋น์ค์ ํต์ ํ ๋ ์ ์ฉํ์ง.
cpp-httplib ์์ํ๊ธฐ
์ค์น๋ ์ ๋ง ๊ฐ๋จํด. GitHub์์ httplib.h ํ์ผ์ ๋ค์ด๋ก๋ํ๊ฑฐ๋, ํจํค์ง ๋งค๋์ ๋ฅผ ์ฌ์ฉํ ์ ์์ด:
# vcpkg
vcpkg install cpp-httplib
# Homebrew (macOS)
brew install cpp-httplib
# ๋๋ ์ง์ ๋ค์ด๋ก๋
wget https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h
ํ์ผ์ ๋ค์ด๋ก๋ํ๋ค๋ฉด, ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ์ ๋ฃ๊ณ ์ธํด๋ฃจ๋ํ๊ธฐ๋ง ํ๋ฉด ๋ผ!
cpp-httplib๋ก RESTful API ์๋ฒ ๋ง๋ค๊ธฐ
์ด์ ์ค์ ๋ก RESTful API ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด์. ๊ฐ๋จํ TODO ์ฑ์ ๋ฐฑ์๋๋ฅผ ๊ตฌํํด๋ณผ๊ฒ! ๐
#include "httplib.h"
#include <iostream>
#include <vector>
#include <mutex>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// TODO ์์ดํ
๊ตฌ์กฐ์ฒด
struct TodoItem {
int id;
std::string title;
bool completed;
json to_json() const {
return json{
{"id", id},
{"title", title},
{"completed", completed}
};
}
};
// ์ ์ญ ๋ฐ์ดํฐ (์ค์ ๋ก๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํด์ผ ํด!)
std::vector<TodoItem> todos;
std::mutex todos_mutex;
int next_id = 1;
int main() {
httplib::Server svr;
// CORS ํค๋ ์ถ๊ฐ (ํ๋ก ํธ์๋์ ํต์ ํ๊ธฐ ์ํด)
svr.set_default_headers({
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
{"Access-Control-Allow-Headers", "Content-Type"}
});
// GET /todos - ๋ชจ๋ TODO ์กฐํ
svr.Get("/todos", [](const httplib::Request& req, httplib::Response& res) {
std::lock_guard<std::mutex> lock(todos_mutex);
json response = json::array();
for (const auto& todo : todos) {
response.push_back(todo.to_json());
}
res.set_content(response.dump(), "application/json");
});
// GET /todos/:id - ํน์ TODO ์กฐํ
svr.Get(R"(/todos/(\d+))", [](const httplib::Request& req, httplib::Response& res) {
int id = std::stoi(req.matches[1]);
std::lock_guard<std::mutex> lock(todos_mutex);
auto it = std::find_if(todos.begin(), todos.end(),
[id](const TodoItem& item) { return item.id == id; });
if (it != todos.end()) {
res.set_content(it->to_json().dump(), "application/json");
} else {
res.status = 404;
res.set_content(R"({"error": "Todo not found"})", "application/json");
}
});
// POST /todos - ์ TODO ์์ฑ
svr.Post("/todos", [](const httplib::Request& req, httplib::Response& res) {
try {
auto body = json::parse(req.body);
std::lock_guard<std::mutex> lock(todos_mutex);
TodoItem new_todo{
next_id++,
body["title"].get<std::string>(),
false
};
todos.push_back(new_todo);
res.status = 201;
res.set_content(new_todo.to_json().dump(), "application/json");
} catch (const std::exception& e) {
res.status = 400;
res.set_content(R"({"error": "Invalid request body"})", "application/json");
}
});
// PUT /todos/:id - TODO ์์
svr.Put(R"(/todos/(\d+))", [](const httplib::Request& req, httplib::Response& res) {
int id = std::stoi(req.matches[1]);
try {
auto body = json::parse(req.body);
std::lock_guard<std::mutex> lock(todos_mutex);
auto it = std::find_if(todos.begin(), todos.end(),
[id](const TodoItem& item) { return item.id == id; });
if (it != todos.end()) {
if (body.contains("title"))
it->title = body["title"].get<std::string>();
if (body.contains("completed"))
it->completed = body["completed"].get<bool>();
res.set_content(it->to_json().dump(), "application/json");
} else {
res.status = 404;
res.set_content(R"({"error": "Todo not found"})", "application/json");
}
} catch (const std::exception& e) {
res.status = 400;
res.set_content(R"({"error": "Invalid request body"})", "application/json");
}
});
// DELETE /todos/:id - TODO ์ญ์
svr.Delete(R"(/todos/(\d+))", [](const httplib::Request& req, httplib::Response& res) {
int id = std::stoi(req.matches[1]);
std::lock_guard<std::mutex> lock(todos_mutex);
auto it = std::find_if(todos.begin(), todos.end(),
[id](const TodoItem& item) { return item.id == id; });
if (it != todos.end()) {
todos.erase(it);
res.status = 204;
} else {
res.status = 404;
res.set_content(R"({"error": "Todo not found"})", "application/json");
}
});
// ์๋ฌ ํธ๋ค๋ฌ
svr.set_error_handler([](const httplib::Request& req, httplib::Response& res) {
json error{
{"error", "Not Found"},
{"path", req.path}
};
res.set_content(error.dump(), "application/json");
});
std::cout << "TODO API ์๋ฒ๊ฐ http://localhost:8080 ์์ ์คํ ์ค์
๋๋ค!" << std::endl;
svr.listen("0.0.0.0", 8080);
return 0;
}
์! ์ด๊ฒ C++ ์ฝ๋๋ผ๊ณ ? ์ ๋ง ๊ฐ๋จํ์ง ์์? ๐ Python Flask๋ Node.js Express์ ๋น์ทํ ๋๋์ด์ผ!
๐ก ์ฝ๋ ํ์ด๋ผ์ดํธ:
๋ผ์ฐํ
svr.Get(), svr.Post() ๊ฐ์ ๋ฉ์๋๋ก ๋ผ์ฐํธ๋ฅผ ์ ์ํด. ์ ๊ทํํ์๋ ์ง์ํด์ R"(/todos/(\d+))" ์ด๋ฐ ์์ผ๋ก ๋์ ๊ฒฝ๋ก๋ฅผ ๋ง๋ค ์ ์์ด.
JSON ์ฒ๋ฆฌ
์ฌ๊ธฐ์๋ nlohmann/json ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ด. C++์์ JSON์ ๋ค๋ฃจ๋ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ผ. ํ์ฑ๊ณผ ์ง๋ ฌํ๊ฐ ์ ๋ง ์ฌ์!
์ค๋ ๋ ์์ ์ฑ
std::mutex๋ก ๋ฐ์ดํฐ ์ ๊ทผ์ ๋๊ธฐํํ์ด. cpp-httplib๋ ๋ฉํฐ์ค๋ ๋๋ก ๋์ํ๊ธฐ ๋๋ฌธ์ ๊ณต์ ๋ฐ์ดํฐ๋ฅผ ๋ณดํธํด์ผ ํด.
CORS ์ค์
set_default_headers()๋ก ๋ชจ๋ ์๋ต์ CORS ํค๋๋ฅผ ์ถ๊ฐํ์ด. ํ๋ก ํธ์๋ ์ฑ๊ณผ ํต์ ํ ๋ ํ์์ผ!
ํ์ผ ์ ๋ก๋ ์ฒ๋ฆฌํ๊ธฐ
cpp-httplib๋ ํ์ผ ์ ๋ก๋๋ ์์ฃผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์ด. ๋ฉํฐํํธ ํผ ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ํ์ฑํด์ฃผ๊ฑฐ๋ ! ๐ค
#include "httplib.h"
#include <iostream>
#include <fstream>
int main() {
httplib::Server svr;
// ํ์ผ ์
๋ก๋ ์๋ํฌ์ธํธ
svr.Post("/upload", [](const httplib::Request& req, httplib::Response& res) {
auto file = req.get_file_value("file");
if (file.filename.empty()) {
res.status = 400;
res.set_content("ํ์ผ์ด ์์ต๋๋ค", "text/plain");
return;
}
// ํ์ผ ์ ์ฅ
std::string save_path = "./uploads/" + file.filename;
std::ofstream ofs(save_path, std::ios::binary);
ofs << file.content;
ofs.close();
std::cout << "ํ์ผ ์ ์ฅ๋จ: " << save_path << std::endl;
std::cout << "ํ์ผ ํฌ๊ธฐ: " << file.content.size() << " bytes" << std::endl;
res.set_content("ํ์ผ ์
๋ก๋ ์ฑ๊ณต: " + file.filename, "text/plain");
});
// ์ฌ๋ฌ ํ์ผ ์
๋ก๋
svr.Post("/upload-multiple", [](const httplib::Request& req, httplib::Response& res) {
auto files = req.files;
int count = 0;
for (const auto& file : files) {
std::string save_path = "./uploads/" + file.second.filename;
std::ofstream ofs(save_path, std::ios::binary);
ofs << file.second.content;
ofs.close();
count++;
}
res.set_content(std::to_string(count) + "๊ฐ์ ํ์ผ์ด ์
๋ก๋๋์์ต๋๋ค", "text/plain");
});
std::cout << "ํ์ผ ์
๋ก๋ ์๋ฒ ์์!" << std::endl;
svr.listen("0.0.0.0", 8080);
return 0;
}
์ ๋ง ๊ฐ๋จํ์ง? req.get_file_value() ํ๋๋ฉด ํ์ผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ด! ๐
โ๏ธ Beast vs cpp-httplib: ๋ฌด์์ ์ ํํ ๊น?
์, ์ด์ ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ชจ๋ ์ดํด๋ดค์ผ๋ ๋น๊ต๋ฅผ ํด๋ณด์! ์ด๋ค ์ํฉ์์ ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ํํด์ผ ํ ๊น? ๐ค
๐ฏ Beast๋ฅผ ์ ํํด์ผ ํ๋ ๊ฒฝ์ฐ:
โ
์ด๊ณ ์ฑ๋ฅ์ด ํ์ํ ๋
์ด๋น ์๋ง~์์ญ๋ง ๊ฑด์ ์์ฒญ์ ์ฒ๋ฆฌํด์ผ ํ๋ค๋ฉด Beast๊ฐ ๋ต์ด์ผ. ๋น๋๊ธฐ I/O๋ฅผ ์ต๋ํ ํ์ฉํด์ ํ๋์จ์ด ์ฑ๋ฅ์ ๋์ด๋ผ ์ ์์ด.
โ
WebSocket์ด ํ์ํ ๋
์ค์๊ฐ ํต์ , ์ฑํ
, ๊ฒ์ ์๋ฒ์ฒ๋ผ WebSocket์ด ํ์์ธ ๊ฒฝ์ฐ Beast๋ฅผ ์จ์ผ ํด. cpp-httplib๋ WebSocket์ ์ง์ํ์ง ์๊ฑฐ๋ .
โ
์ธ๋ฐํ ์ ์ด๊ฐ ํ์ํ ๋
ํ์์์, ๋ฒํผ ํฌ๊ธฐ, ์ฐ๊ฒฐ ํ ๊ด๋ฆฌ ๋ฑ์ ์ง์ ์ ์ดํ๊ณ ์ถ๋ค๋ฉด Beast๊ฐ ๋ ์ ํฉํด. ๋ชจ๋ ๊ฑธ ์ปค์คํฐ๋ง์ด์งํ ์ ์์ด.
โ
์ด๋ฏธ Boost๋ฅผ ์ฌ์ฉ ์ค์ผ ๋
ํ๋ก์ ํธ์์ ์ด๋ฏธ Boost๋ฅผ ์ฐ๊ณ ์๋ค๋ฉด Beast๋ฅผ ์ถ๊ฐํ๋ ๊ฒ ์์ฐ์ค๋ฌ์. ๋ค๋ฅธ Boost ์ปดํฌ๋ํธ๋ค๊ณผ ์ ํตํฉ๋๊ฑฐ๋ .
๐ cpp-httplib๋ฅผ ์ ํํด์ผ ํ๋ ๊ฒฝ์ฐ:
โ
๋น ๋ฅธ ํ๋กํ ํ์ดํ์ด ๋ชฉํ์ผ ๋
์์ด๋์ด๋ฅผ ๋น ๋ฅด๊ฒ ๊ฒ์ฆํ๊ณ ์ถ๋ค๋ฉด cpp-httplib๊ฐ ์ต๊ณ ์ผ. ๋ช ์ค์ ์ฝ๋๋ก ๋ฐ๋ก ์๋ฒ๋ฅผ ๋์ธ ์ ์์ด.
โ
์ค์๊ท๋ชจ ์๋น์ค๋ฅผ ๋ง๋ค ๋
์ด๋น ์์ฒ ๊ฑด ์ ๋์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์๋น์ค๋ผ๋ฉด cpp-httplib๋ก ์ถฉ๋ถํด. ์ฑ๋ฅ๋ ๋์์ง ์๊ณ ์ฝ๋๋ ๊น๋ํ์ง.
โ
์์กด์ฑ์ ์ต์ํํ๊ณ ์ถ์ ๋
์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด ๊น๋ํ๊ฒ ํ๋ก์ ํธ๋ฅผ ๊ตฌ์ฑํ๊ณ ์ถ๋ค๋ฉด cpp-httplib๊ฐ ๋ต์ด์ผ. ํค๋ ํ์ผ ํ๋๋ฉด ๋!
โ
RESTful API๋ง ํ์ํ ๋
๋จ์ํ REST API ์๋ฒ๋ฅผ ๋ง๋ ๋ค๋ฉด cpp-httplib์ ์ง๊ด์ ์ธ ๋ผ์ฐํ
์์คํ
์ด ์ ๋ง ํธํด. Flask๋ Express ์ฐ๋ ๋๋์ด์ผ.
โ ๏ธ ์ค์ ํ:
์ฌ์ค ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ๋ ์ข์ ์ ๋ต์ด์ผ! ์๋ฅผ ๋ค์ด, ํ๋กํ ํ์ ์ cpp-httplib๋ก ๋น ๋ฅด๊ฒ ๋ง๋ค๊ณ , ์ฑ๋ฅ์ด ์ค์ํ ๋ถ๋ถ๋ง ๋์ค์ Beast๋ก ์ฌ์์ฑํ๋ ๊ฑฐ์ง. ๋๋ ๋ด๋ถ ๊ด๋ฆฌ API๋ cpp-httplib๋ก, ์ธ๋ถ ๊ณต๊ฐ API๋ Beast๋ก ๋ง๋๋ ์์ผ๋ก ์ญํ ์ ๋๋ ์๋ ์์ด. ๐จ
๐ง ์ค์ ํ๋ก์ ํธ: ์์ ํ ์น ์๋ฒ ๊ตฌ์ถํ๊ธฐ
์ด๋ก ๋ง ๋ฐฐ์ฐ๋ฉด ์ฌ๋ฏธ์์์? ์ด์ ์ค์ ๋ก ์ฌ์ฉํ ์ ์๋ ์์ ํ ์น ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด์! ์ฌ๊ธฐ์๋ cpp-httplib๋ฅผ ์ฌ์ฉํด์ ์ ์ ํ์ผ ์๋น, API ์๋ํฌ์ธํธ, ์ธ์ฆ, ๋ก๊น ๋ฑ์ ๋ชจ๋ ๊ตฌํํด๋ณผ ๊ฑฐ์ผ. ๐
ํ๋ก์ ํธ ๊ตฌ์กฐ
my-web-server/
โโโ main.cpp
โโโ handlers/
โ โโโ auth_handler.h
โ โโโ api_handler.h
โ โโโ static_handler.h
โโโ middleware/
โ โโโ logger.h
โ โโโ cors.h
โโโ models/
โ โโโ user.h
โโโ public/
โ โโโ index.html
โ โโโ style.css
โ โโโ app.js
โโโ CMakeLists.txt
์ธ์ฆ ๋ฏธ๋ค์จ์ด ๊ตฌํ
๋จผ์ JWT ๊ธฐ๋ฐ ์ธ์ฆ์ ๊ตฌํํด๋ณด์. ์ค์ ํ๋ก๋์ ์์๋ JWT ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ฒ ์ง๋ง, ์ฌ๊ธฐ์๋ ๊ฐ๋จํ ํ ํฐ ์์คํ ์ ๋ง๋ค์ด๋ณผ๊ฒ.
// middleware/auth.h
#pragma once
#include <string>
#include <unordered_map>
#include <random>
#include <sstream>
#include <iomanip>
class AuthMiddleware {
private:
std::unordered_map<std::string, std::string> tokens; // token -> username
std::string generate_token() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 255);
std::stringstream ss;
for (int i = 0; i < 32; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << dis(gen);
}
return ss.str();
}
public:
std::string create_token(const std::string& username) {
std::string token = generate_token();
tokens[token] = username;
return token;
}
bool validate_token(const std::string& token, std::string& username) {
auto it = tokens.find(token);
if (it != tokens.end()) {
username = it->second;
return true;
}
return false;
}
void revoke_token(const std::string& token) {
tokens.erase(token);
}
std::string extract_token(const std::string& auth_header) {
if (auth_header.substr(0, 7) == "Bearer ") {
return auth_header.substr(7);
}
return "";
}
};
๋ก๊น ๋ฏธ๋ค์จ์ด
๋ชจ๋ ์์ฒญ์ ๋ก๊น ํ๋ ๋ฏธ๋ค์จ์ด๋ ๋ง๋ค์ด๋ณด์. ๋๋ฒ๊น ํ ๋ ์ ๋ง ์ ์ฉํด! ๐
// middleware/logger.h
#pragma once
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
class Logger {
public:
static std::string get_timestamp() {
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
static void log_request(const std::string& method, const std::string& path,
int status, long duration_ms) {
std::cout << "[" << get_timestamp() << "] "
<< method << " " << path << " "
<< status << " " << duration_ms << "ms"
<< std::endl;
}
static void log_error(const std::string& message) {
std::cerr << "[" << get_timestamp() << "] ERROR: "
<< message << std::endl;
}
static void log_info(const std::string& message) {
std::cout << "[" << get_timestamp() << "] INFO: "
<< message << std::endl;
}
};
์์ ํ ์๋ฒ ๊ตฌํ
์ด์ ๋ชจ๋ ๊ฑธ ํฉ์ณ์ ์์ ํ ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด์!
#include "httplib.h"
#include "middleware/auth.h"
#include "middleware/logger.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <chrono>
using json = nlohmann::json;
// ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค (์ค์ ๋ก๋ DB ์ฌ์ฉ)
std::unordered_map<std::string, std::string> users = {
{"admin", "password123"},
{"user1", "pass456"}
};
int main() {
httplib::Server svr;
AuthMiddleware auth;
// ๋ก๊น
๋ฏธ๋ค์จ์ด
svr.set_logger([](const httplib::Request& req, const httplib::Response& res) {
Logger::log_request(req.method, req.path, res.status, 0);
});
// CORS ์ค์
svr.set_default_headers({
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
{"Access-Control-Allow-Headers", "Content-Type, Authorization"}
});
// ์ ์ ํ์ผ ์๋น
svr.set_mount_point("/", "./public");
// ํฌ์ค ์ฒดํฌ
svr.Get("/health", [](const httplib::Request&, httplib::Response& res) {
json response{
{"status", "healthy"},
{"timestamp", Logger::get_timestamp()}
};
res.set_content(response.dump(), "application/json");
});
// ๋ก๊ทธ์ธ ์๋ํฌ์ธํธ
svr.Post("/api/login", [&auth](const httplib::Request& req, httplib::Response& res) {
try {
auto body = json::parse(req.body);
std::string username = body["username"];
std::string password = body["password"];
auto it = users.find(username);
if (it != users.end() && it->second == password) {
std::string token = auth.create_token(username);
json response{
{"success", true},
{"token", token},
{"username", username}
};
Logger::log_info("User logged in: " + username);
res.set_content(response.dump(), "application/json");
} else {
res.status = 401;
json response{
{"success", false},
{"error", "Invalid credentials"}
};
res.set_content(response.dump(), "application/json");
}
} catch (const std::exception& e) {
res.status = 400;
json response{{"error", "Invalid request"}};
res.set_content(response.dump(), "application/json");
}
});
// ๋ณดํธ๋ ์๋ํฌ์ธํธ (์ธ์ฆ ํ์)
svr.Get("/api/profile", [&auth](const httplib::Request& req, httplib::Response& res) {
std::string auth_header = req.get_header_value("Authorization");
std::string token = auth.extract_token(auth_header);
std::string username;
if (auth.validate_token(token, username)) {
json response{
{"username", username},
{"email", username + "@example.com"},
{"role", "user"}
};
res.set_content(response.dump(), "application/json");
} else {
res.status = 401;
json response{{"error", "Unauthorized"}};
res.set_content(response.dump(), "application/json");
}
});
// ๋ก๊ทธ์์
svr.Post("/api/logout", [&auth](const httplib::Request& req, httplib::Response& res) {
std::string auth_header = req.get_header_value("Authorization");
std::string token = auth.extract_token(auth_header);
auth.revoke_token(token);
json response{{"success", true}};
res.set_content(response.dump(), "application/json");
});
// ์๋ฌ ํธ๋ค๋ฌ
svr.set_error_handler([](const httplib::Request& req, httplib::Response& res) {
json error{
{"error", "Not Found"},
{"path", req.path}
};
res.set_content(error.dump(), "application/json");
});
// ์์ธ ํธ๋ค๋ฌ
svr.set_exception_handler([](const httplib::Request&, httplib::Response& res,
std::exception_ptr ep) {
try {
std::rethrow_exception(ep);
} catch (std::exception& e) {
Logger::log_error(e.what());
res.status = 500;
json error{{"error", "Internal Server Error"}};
res.set_content(error.dump(), "application/json");
}
});
Logger::log_info("์๋ฒ ์์: http://localhost:8080");
svr.listen("0.0.0.0", 8080);
return 0;
}
์! ์ด์ ์ ๋ง ์ค์ ์์ ์ฌ์ฉํ ์ ์๋ ์๋ฒ๊ฐ ์์ฑ๋์ด! ๐ ์ธ์ฆ, ๋ก๊น , ์๋ฌ ํธ๋ค๋ง, ์ ์ ํ์ผ ์๋น๊น์ง ๋ชจ๋ ๊ตฌํํ์ง. ์ด ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ ๋ง์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์์ด!
๐ ์ฑ๋ฅ ์ต์ ํ ํ
C++๋ก ์น ์๋ฒ๋ฅผ ๋ง๋๋ ์ด์ ๊ฐ ์ฑ๋ฅ ๋๋ฌธ์ด์์? ๊ทธ๋ผ ์ฑ๋ฅ์ ์ต๋ํ ๋์ด์ฌ๋ฆฌ๋ ๋ฐฉ๋ฒ๋ค์ ์์๋ณด์! ๐ช
1. ์ฐ๊ฒฐ ํ ์ฌ์ฉํ๊ธฐ
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์ธ๋ถ ์๋น์ค์ ํต์ ํ ๋๋ ์ฐ๊ฒฐ ํ์ ์ฌ์ฉํด์ผ ํด. ๋งค๋ฒ ์๋ก์ด ์ฐ๊ฒฐ์ ๋ง๋๋ ๊ฑด ๋น์ฉ์ด ํฌ๊ฑฐ๋ .
class ConnectionPool {
private:
std::queue<std::unique_ptr<Connection>> pool;
std::mutex mutex;
std::condition_variable cv;
size_t max_size;
public:
ConnectionPool(size_t size) : max_size(size) {
for (size_t i = 0; i < size; ++i) {
pool.push(std::make_unique<Connection>());
}
}
std::unique_ptr<Connection> acquire() {
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this] { return !pool.empty(); });
auto conn = std::move(pool.front());
pool.pop();
return conn;
}
void release(std::unique_ptr<Connection> conn) {
std::lock_guard<std::mutex> lock(mutex);
pool.push(std::move(conn));
cv.notify_one();
}
};
2. ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ต์ํ
๋ถํ์ํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ์ฑ๋ฅ์ ์ ์ด์ผ. ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํ๊ณ , move semantics๋ฅผ ์ ๊ทน ํ์ฉํ์!
๐ก ์ฑ๋ฅ ํ:
๋ฌธ์์ด ์ฐ์ฐ ์ต์ ํ
std::string์ += ์ฐ์ฐ์๋ณด๋ค std::stringstream์ด๋ fmt ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ ๋น ๋ฅผ ์ ์์ด.
JSON ํ์ฑ ์บ์ฑ
๊ฐ์ JSON์ ๋ฐ๋ณตํด์ ํ์ฑํ์ง ๋ง๊ณ , ํ ๋ฒ ํ์ฑํ ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํด๋์.
์ค๋ ๋ ๋ก์ปฌ ์คํ ๋ฆฌ์ง
์ค๋ ๋๋ง๋ค ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ฅผ thread_local๋ก ์ ์ธํ๋ฉด ๋ฝ ์์ด ์ฌ์ฉํ ์ ์์ด.
3. ๋น๋๊ธฐ I/O ํ์ฉ
Beast๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ๋น๋๊ธฐ I/O๋ฅผ ์ต๋ํ ํ์ฉํด์ผ ํด. ๋ธ๋กํน ์์ ์ ์ฑ๋ฅ์ ๋ณ๋ชฉ์ด ๋๊ฑฐ๋ !
// ๋์ ์: ๋๊ธฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฟผ๋ฆฌ
void handle_request(const Request& req, Response& res) {
auto result = db.query("SELECT * FROM users"); // ๋ธ๋กํน!
res.send(result);
}
// ์ข์ ์: ๋น๋๊ธฐ ์ฟผ๋ฆฌ
void handle_request(const Request& req, Response& res) {
db.async_query("SELECT * FROM users", [res](auto result) {
res.send(result);
});
}
4. ์ปดํ์ผ ์ต์ ํ ์ต์
์ปดํ์ผ๋ฌ ์ต์ ํ ์ต์ ๋ ์ค์ํด! ๋ฆด๋ฆฌ์ฆ ๋น๋์์๋ ๋ฐ๋์ ์ต์ ํ๋ฅผ ์ผ์ผ ํด.
# CMakeLists.txt
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -DNDEBUG")
# ๋๋ ์ง์ ์ปดํ์ผ
g++ -O3 -march=native -std=c++17 main.cpp -o server -lpthread
๐ฏ ๋ฒค์น๋งํน ๋๊ตฌ:
์ฑ๋ฅ์ ์ธก์ ํ๋ ค๋ฉด ์ ์ ํ ๋๊ตฌ๊ฐ ํ์ํด. wrk, ab(Apache Bench), hey ๊ฐ์ HTTP ๋ฒค์น๋งํน ๋๊ตฌ๋ฅผ ์ฌ์ฉํด๋ณด์. ์๋ฅผ ๋ค์ด:
wrk -t12 -c400 -d30s http://localhost:8080/api/users
์ด ๋ช ๋ น์ 12๊ฐ ์ค๋ ๋๋ก 400๊ฐ ๋์ ์ฐ๊ฒฐ์ ์ ์งํ๋ฉด์ 30์ด ๋์ ํ ์คํธ๋ฅผ ์คํํด.
๐ ๋ณด์ ๊ณ ๋ ค์ฌํญ
์น ์๋ฒ๋ฅผ ๋ง๋ค ๋ ๋ณด์์ ์ ๋ง ์ค์ํด! C++๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ง์ ๋ค๋ฃจ๊ธฐ ๋๋ฌธ์ ๋์ฑ ์กฐ์ฌํด์ผ ํ์ง. ์ฃผ์ ๋ณด์ ์ด์๋ค์ ์ดํด๋ณด์. ๐ก๏ธ
1. ๋ฒํผ ์ค๋ฒํ๋ก์ฐ ๋ฐฉ์ง
โ ๏ธ ์ํํ ์ฝ๋:
char buffer[100];
strcpy(buffer, user_input.c_str()); // ์ํ!
โ ์์ ํ ์ฝ๋:
std::string buffer = user_input; // ์์
// ๋๋
char buffer[100];
strncpy(buffer, user_input.c_str(), sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';
2. SQL ์ธ์ ์ ๋ฐฉ์ง
์ฌ์ฉ์ ์ ๋ ฅ์ ์ง์ SQL ์ฟผ๋ฆฌ์ ๋ฃ์ผ๋ฉด ์ ๋ผ! ํญ์ prepared statement๋ฅผ ์ฌ์ฉํ์.
// ๋์ ์
std::string query = "SELECT * FROM users WHERE username = '" + username + "'";
// ์ข์ ์ (prepared statement)
auto stmt = db.prepare("SELECT * FROM users WHERE username = ?");
stmt.bind(1, username);
auto result = stmt.execute();
3. HTTPS ์ค์
ํ๋ก๋์ ํ๊ฒฝ์์๋ ๋ฐ๋์ HTTPS๋ฅผ ์ฌ์ฉํด์ผ ํด. cpp-httplib์์ HTTPS๋ฅผ ํ์ฑํํ๋ ๋ฐฉ๋ฒ:
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
int main() {
httplib::SSLServer svr("cert.pem", "key.pem");
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("Secure connection!", "text/plain");
});
svr.listen("0.0.0.0", 443);
}
4. Rate Limiting
DDoS ๊ณต๊ฒฉ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์์ฒญ ์๋ ์ ํ์ ๊ตฌํํ์!
class RateLimiter {
private:
std::unordered_map<std::string, std::deque<std::chrono::steady_clock::time_point>> requests;
std::mutex mutex;
int max_requests;
std::chrono::seconds window;
public:
RateLimiter(int max_req, int window_sec)
: max_requests(max_req), window(window_sec) {}
bool allow_request(const std::string& client_ip) {
std::lock_guard<std::mutex> lock(mutex);
auto now = std::chrono::steady_clock::now();
auto& req_times = requests[client_ip];
// ์ค๋๋ ์์ฒญ ์ ๊ฑฐ
while (!req_times.empty() &&
now - req_times.front() > window) {
req_times.pop_front();
}
if (req_times.size() >= max_requests) {
return false;
}
req_times.push_back(now);
return true;
}
};
๐ ํ์ต ๋ฆฌ์์ค์ ์ปค๋ฎค๋ํฐ
C++ ์น ๊ฐ๋ฐ์ ๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด? ์ฌ๊ธฐ ์ ์ฉํ ๋ฆฌ์์ค๋ค์ ๋ชจ์๋ดค์ด! ๐
๐ ๊ณต์ ๋ฌธ์:
โข Boost.Beast ๋ฌธ์: https://www.boost.org/doc/libs/release/libs/beast/
โข cpp-httplib GitHub: https://github.com/yhirose/cpp-httplib
โข Boost.Asio ํํ ๋ฆฌ์ผ: https://think-async.com/Asio/
๐ฌ ์ปค๋ฎค๋ํฐ:
โข C++ Slack: cpplang.slack.com
โข Reddit r/cpp: ์ต์ C++ ์์๊ณผ ํ ๋ก
โข Stack Overflow: ๊ตฌ์ฒด์ ์ธ ๋ฌธ์ ํด๊ฒฐ
๐ฅ ์จ๋ผ์ธ ๊ฐ์:
โข CppCon ์ ํ๋ธ ์ฑ๋: ๊ณ ๊ธ C++ ๊ธฐ๋ฒ
โข Udemy C++ ์น ๊ฐ๋ฐ ๊ฐ์๋ค
โข ์ฌ๋ฅ๋ท์์๋ C++ ์น ๊ฐ๋ฐ ๊ด๋ จ ์ฌ๋ฅ์ ๊ฐ์ง ์ ๋ฌธ๊ฐ๋ค์ ์ฐพ์ ์ ์์ด!
๐ ์ค์ ํ๋ก์ ํธ ์์ด๋์ด:
1. ์ค์๊ฐ ์ฑํ
์๋ฒ
Beast์ WebSocket์ ํ์ฉํด์ ์ค์๊ฐ ์ฑํ
์ฑ์ ๋ง๋ค์ด๋ณด์. ๋ฉ์์ง ๋ธ๋ก๋์บ์คํ
, ๋ฐฉ ๊ด๋ฆฌ, ์ฌ์ฉ์ ์ธ์ฆ ๋ฑ์ ๊ตฌํํ๋ฉด ์ข์ ํฌํธํด๋ฆฌ์ค๊ฐ ๋ ๊ฑฐ์ผ!
2. ํ์ผ ๊ณต์ ์๋น์ค
cpp-httplib๋ก ํ์ผ ์
๋ก๋/๋ค์ด๋ก๋ ์๋น์ค๋ฅผ ๋ง๋ค์ด๋ณด์. ์ฒญํฌ ์
๋ก๋, ์ด์ด๋ฐ๊ธฐ, ๊ถํ ๊ด๋ฆฌ ๋ฑ์ ์ถ๊ฐํ๋ฉด ๋ ์ข์.
3. ๋ง์ดํฌ๋ก์๋น์ค API ๊ฒ์ดํธ์จ์ด
์ฌ๋ฌ ๋ฐฑ์๋ ์๋น์ค๋ฅผ ํ๋์ ์๋ํฌ์ธํธ๋ก ํตํฉํ๋ ๊ฒ์ดํธ์จ์ด๋ฅผ ๋ง๋ค์ด๋ณด์. ๋ก๋ ๋ฐธ๋ฐ์ฑ, ์บ์ฑ, ์ธ์ฆ ๋ฑ์ ๊ตฌํํ ์ ์์ด.
4. IoT ๋ฐ์ดํฐ ์์ง ์๋ฒ
์ผ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฒ๋ฆฌํ๋ ๊ณ ์ฑ๋ฅ ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด์. ์๊ณ์ด ๋ฐ์ดํฐ ์ฒ๋ฆฌ, ์ค์๊ฐ ๋ถ์ ๋ฑ์ด ํฌํจ๋ ์ ์์ด.
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด. C++ ์น ๊ฐ๋ฐ์ด ์ฒ์์๋ ์ด๋ ค์ ๋ณด์ผ ์ ์์ง๋ง, ํ ๋ฒ ์ต์ํด์ง๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ๋๊ตฌ๊ฐ ๋ผ. ํนํ ์ฑ๋ฅ์ด ์ค์ํ ์๋น์ค๋ฅผ ๋ง๋ค ๋๋ C++๋งํ ๊ฒ ์์ด! ๐ช
Beast์ cpp-httplib, ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋ ๊ฐ์์ ์ฅ์ ์ด ์์ด. Beast๋ ์ต๊ณ ์ ์ฑ๋ฅ๊ณผ ์ ์ฐ์ฑ์, cpp-httplib๋ ๊ฐํธํจ๊ณผ ๋น ๋ฅธ ๊ฐ๋ฐ์ ์ ๊ณตํ์ง. ํ๋ก์ ํธ์ ์๊ตฌ์ฌํญ์ ๋ง์ถฐ์ ์ ํํ๋ฉด ๋ผ!
๐ ๋ค์ ๋จ๊ณ:
1. ์์ ํ๋ก์ ํธ๋ถํฐ ์์
๊ฐ๋จํ REST API ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด์ ๊ฐ์ ์ก์๋ณด์. ์๋ฒฝํ์ง ์์๋ ๊ด์ฐฎ์!
2. ์ฑ๋ฅ ์ธก์ ์ต๊ดํ
๋ฒค์น๋งํน ๋๊ตฌ๋ฅผ ์ฌ์ฉํด์ ์ฑ๋ฅ์ ์ธก์ ํ๊ณ ๊ฐ์ ํ๋ ์ต๊ด์ ๋ค์ด์.
3. ์คํ์์ค ๊ธฐ์ฌ
Beast๋ cpp-httplib ๊ฐ์ ์คํ์์ค ํ๋ก์ ํธ์ ๊ธฐ์ฌํด๋ณด๋ ๊ฒ๋ ์ข์ ํ์ต ๋ฐฉ๋ฒ์ด์ผ.
4. ์ปค๋ฎค๋ํฐ ์ฐธ์ฌ
๋ค๋ฅธ ๊ฐ๋ฐ์๋ค๊ณผ ๊ฒฝํ์ ๊ณต์ ํ๊ณ ๋ฐฐ์ฐ์. ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์์ ์ ์ง์์ ๋๋๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ!
C++ ์น ๊ฐ๋ฐ์ ๊ณ์ ๋ฐ์ ํ๊ณ ์์ด. HTTP/3, QUIC ๊ฐ์ ์๋ก์ด ํ๋กํ ์ฝ๋ค๋ ๋ฑ์ฅํ๊ณ ์๊ณ , C++20, C++23์ ์๋ก์ด ๊ธฐ๋ฅ๋ค๋ ์น ๊ฐ๋ฐ์ ๋ ์ฝ๊ฒ ๋ง๋ค์ด์ฃผ๊ณ ์์ง. ์์ผ๋ก๊ฐ ๋ ๊ธฐ๋๋๋ ๋ถ์ผ์ผ! ๐
์, ์ด์ ์ง์ ์ฝ๋๋ฅผ ์์ฑํด๋ณผ ์๊ฐ์ด์ผ! ์ด๋ก ๋ง ์ฝ๋ ๊ฒ๋ณด๋ค ์ง์ ๋ง๋ค์ด๋ณด๋ ๊ฒ ๋ฐฑ๋ฐฐ ๋ซ๊ฑฐ๋ . ์๋ฌ๊ฐ ๋๋ ๊ด์ฐฎ์. ๊ทธ๊ฒ ๋ฐ๋ก ๋ฐฐ์ฐ๋ ๊ณผ์ ์ด๋๊น! ํ์ดํ ! ๐ปโจ
์ฆ๊ฑฐ์ด C++ ์น ๊ฐ๋ฐ ๋์ธ์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.