๐๐ป C++์ httplib๋ฅผ ์ฌ์ฉํ ๋งค์ฐ ๊ฐ๋จํ ์น์๋ฒ ์
๋๋ค.
This is a very simple web server using C++’s httplib.
๐๐ป httplib ์น์๋ฒ์ ๋ฆฌ์กํธ๋ฅผ ์ถ๊ฐํ์ต๋๋ค.
I added React to the httplib web server.
โญ๏ธ ๋ฆฌ์กํธ ๋๋ ํ ๋ฆฌ๋ ๋ฆฌ์กํธ ํ๋ก์ ํธ๋ฅผ ์ค์นํ๋ฉด ์์ฑ๋ฉ๋๋ค. www ๋๋ ํ ๋ฆฌ๋ ์ง์ ์์ฑํด์ผ ํฉ๋๋ค.
The React directory is created when you install a React project. You must create the www directory manually.
๐๐ป ๋จผ์ Vite+React ์์ฑ ๋ฐ ์คํ ํฉ๋๋ค.
First, create and run Vite+React.
# ์ต์ Vite + React ์ค์น / Install the latest Vite + React
npm create vite@latest react -- --template react-js
# ํ๋ก์ ํธ ํด๋๋ก ์ด๋ / Go to project folder
cd react
# ์์กด์ฑ ์ค์น / install dependencies
npm install
# ๊ฐ๋ฐ ์๋ฒ ์คํ / Run development server
npm run dev
# ๋๋ http://localhost:5173/ ์ ์ / Or access http://localhost:5173/
๐๐ป ๋ฆฌ์กํธ ํ์ด์ง๊ฐ ์ ์์ ์ผ๋ก ๋ณด์ด๋์ง ํ์ธํฉ๋๋ค.
Check if the React page is displayed correctly.
๐๐ป react๋๋ ํ ๋ฆฌ๊ฐ ์์ฑ๋์๋ค๋ฉด react๋๋ ํ ๋ฆฌ๋ก ์ด๋ํฉ๋๋ค.
If the react directory has been created, navigate to the react directory.
๐๐ป ์๋ฒ ์คํ์ ๋ฆฌ์กํธ๋ /react๋ก ์ค์ ํฉ๋๋ค. ๊ทธ๋์ ์๋์ ๊ฐ์ด vite.config.jsํ์ผ์ ์์ ํฉ๋๋ค.
When running the server, React is configured to /react. Therefore, modify the vite.config.js file as follows.
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
base: '/react/',// Added
})
๐๐ป ๋ฆฌ์กํธ๋ฅผ ๋น๋ํ๊ณ ๊ฒฐ๊ณผ๋ฌผ(dist ๋๋ ํ ๋ฆฌ)์ www ๋๋ ํ ๋ฆฌ ์๋๋ก ๋ณต์ฌํฉ๋๋ค.
Build React and copy the output (dist directory) under the www directory.
โญ๏ธ www๋๋ ํ ๋ฆฌ๊ฐ ์๋ค๋ฉด server๋๋ ํ ๋ฆฌ ์์ ๋ง๋ค๋ฉด๋ฉ๋๋ค.
If the www directory does not exist, you can create it next to the server directory.
npm run build
rsync -a ./dist ../www/
๐๐ป์๋ฒ์ฝ๋ / Server Code
โ๏ธ httplib.h
— ์๋์ ๋ช
๋ น์ด๋ก ๋ค์ด๋ก๋ํฉ๋๋ค.
Download using the command below.
curl -L https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h -o httplib.h
โ๏ธ server.cpp
#include "httplib.h"
#include <iostream>
int main( ) {
httplib::Server svr;
// ๊ธฐ๋ณธ ๋ผ์ฐํธ / default route
svr.Get("/", [](const httplib::Request&, httplib::Response& res ) {
res.set_content("<h3>hihi! Silver Hand ~~ ๐</h3>", "text/html; charset=utf-8");
});
// ๋ฆฌ์กํธ ๊ฒฝ๋ก / react path
if (!svr.set_mount_point("/react", "../www/dist")) {
std::cerr << "๋ฆฌ์กํธ ๊ฒฝ๋ก ํด๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค! ๊ฒฝ๋ก๋ฅผ ํ์ธํ์ธ์./React path folder not found! Please check the path." << std::endl;
return 1;
}
// API ๋ผ์ฐํธ / api route
svr.Get("/api/hello", [](const httplib::Request&, httplib::Response& res ) {
res.set_content("{\"message\": \"API Server ๐\"}", "application/json; charset=utf-8");
});
// 404 ์๋ฌ ํธ๋ค๋ฌ / 404 error handler
svr.set_error_handler([](const httplib::Request& req, httplib::Response& res ) {
if (res.status == 404) {
// ์๋ฒ ํฐ๋ฏธ๋์ ์๋ฌ ๊ธฐ๋ก (๊ด๋ฆฌ์ ํ์ธ์ฉ)
std::cerr << "[404 Error] ์์ฒญ๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์: " << req.path << std::endl;
// ๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์๋ด ๋ฉ์์ง ์ ์ก (์ฌ์ฉ์ ํ์ธ์ฉ)
res.set_content(
"<h1>ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค / The page cannot be found.๐ญ</h1>"
"<p>์์ฒญํ์ '" + req.path + "' ๊ฒฝ๋ก๋ ์กด์ฌํ์ง ์์ต๋๋ค.</p>"
"<p>The requested path '" + req.path + "' does not exist.</p>",
"text/html; charset=utf-8"
);
}
});
std::cout << "Server Listen http://localhost:8080" << std::endl;
svr.listen("0.0.0.0", 8080 );
return 0;
}
๐๐ป์ปดํ์ผ / Compiling
g++ -o server server.cpp -std=c++11 -pthread
๐๐ป ์๋ฒ์คํ / Run Server
./server
๐๐ป ๋ธ๋ผ์ฐ์ ๋ก ์๋ฒ์ ์ / Connect to server with browser
โ๏ธ ๊ธฐ๋ณธ ๋ฃจํธ ๋ผ์ฐํธ ์ ์ / Access default root route

โ๏ธ ๋ฆฌ์กํธ ๋ผ์ฐํธ ์ ์ / React Route Access

โ๏ธ API๋ผ์ฐํธ ์ ์ / API route connection

โ๏ธ ์กด์ฌํ์ง์๋ ๋ผ์ฐํธ์คํ / Execute non-existent route
