[Webserver]httplib webserver+react(MacOS)

๐Ÿ‘‰๐Ÿป 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

Screenshot

โœ”๏ธ API๋ผ์šฐํŠธ ์ ‘์† / API route connection

โœ”๏ธ ์กด์žฌํ•˜์ง€์•Š๋Š” ๋ผ์šฐํŠธ์‹คํ–‰ / Execute non-existent route

Screenshot

Leave a Reply