Tic Tac Toe: Content Cycle
In this article, we will consider the backend implementation using the C ++ Boost.Beast library using an example of a synchronous server. The same functionality as in the previous article - we get a random number from 0 to 8 inclusive from the backend. It turned out that raising the container for Beast is no more difficult than for Flask. The main thing is to immediately find a good suitable example . Here I took a Dockerfile for my project.
The picture was taken from the presentation of this report to attract attention, as well as to increase the mood and motivation for those who do not know C ++.
We clone the project on your computer:
git clone https://github.com/nomhoi/tic-tac-toe-part5.git
Launch containers:
cd tic-tac-toe-part5 docker-compose up -d
We build the web application:
cd front npm install npm run-script build
Open the browser at http: // localhost .
The flask service was replaced with the beast service.
docker-compose.yml:
version: '3.6' services: nginx: image: nginx:alpine container_name: nginx volumes: - ./front/public:/usr/share/nginx/html - ./default.conf:/etc/nginx/conf.d/default.conf:ro ports: - "80:80" depends_on: - beast networks: - backend beast: container_name: beast build: context: beast/ dockerfile: Dockerfile ports: - "8080:8080" networks: - backend networks: backend: name: backend
Here we changed only the setting in the nginx'a default.conf configuration file.
location /number { proxy_pass http://beast:8080; }
Dockerfile was taken here: https://github.com/vinniefalco/CppCon2018 .
FROM ubuntu:bionic AS build # Install tools required for the project RUN apt-get update \ && apt-get install gcc -y \ && apt-get install g++ -y \ && apt-get install cmake -y \ && apt-get install wget -y # Install Boost RUN cd /home \ && wget http://downloads.sourceforge.net/project/boost/boost/1.70.0/boost_1_70_0.tar.gz \ && tar xfz boost_1_70_0.tar.gz \ && rm boost_1_70_0.tar.gz \ && cd boost_1_70_0 \ && ./bootstrap.sh --with-libraries=system \ && ./b2 install # Copy the entire project and build it COPY ./app /cpp/src/project/ WORKDIR /cpp/src/project/ RUN g++ http_server_sync.cpp -o http_server_sync -lpthread FROM ubuntu:bionic COPY --from=build /cpp/src/project /app/ ENTRYPOINT ["/app/http_server_sync", "0.0.0.0", "8080", "/app/wwwroot"] EXPOSE 8080
As you can see, multi-stage assembly technology is used. At the first stage, the necessary packages and the Boost library are installed, the http_server_sync server is built . At the second stage, the finished server is copied to the final container and is launched there.
The source code of the synchronous server was taken here . Added a handle_number_request function to handle the request at / number .
// Return a random number template< class Body, class Allocator, class Send> void handle_number_request( beast::string_view doc_root, http::request<Body, http::basic_fields<Allocator>>&& req, Send&& send) { http::response<http::string_body> res; res.version(11); // HTTP/1.1 res.result(http::status::ok); res.set(http::field::server, "Beast"); res.body() = std::to_string(rand() % 9); res.prepare_payload(); return send(std::move(res)); }
In the response body we write a random number from 0 to 8 inclusive.
As you can see, Flask helped us quickly get a general idea of the interaction between the frontend and backend, we quickly got the framework of the entire application. When developing the backend on Beast, we already understood how the interaction worked and focused only on finding the right container and writing the backend server itself.
As your homework, you can try to remake the nginx service using a multi-stage build, so that the frontend is assembled at the first stage. Now the nginx service is configured so that it is more convenient to conduct front-end development, without unnecessary assembly.
Second homework: try to remove the nginx service, and raise the frontend in the beast service. I think it should work out.
Post your decisions in the comments.
The third task: to meditate on the subject of scalability - vertical and horizontal. How to combine these two languages, C ++ and Python, in one web framework to solve this issue.
Source: https://habr.com/ru/post/460991/
All Articles