{"id":440,"date":"2019-11-28T19:01:44","date_gmt":"2019-11-29T03:01:44","guid":{"rendered":"http:\/\/modulesafari.com\/?p=440"},"modified":"2019-12-06T18:17:38","modified_gmt":"2019-12-07T02:17:38","slug":"served","status":"publish","type":"post","link":"http:\/\/modulesafari.com\/served\/","title":{"rendered":"Served; Build RESTful C++ servers"},"content":{"rendered":"\n
Served<\/a> is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface, drastically reducing the amount of boiler-plate code that would normally be needed. Overall, it looks very promising for when you want everything to just work, without compromising on performance. Now, let’s dive right into it. <\/p>\n\n\n\n Getting started is fairly standard for a from-source installation, you can also opt to compile it into a deb or rpm package via the build flags. Running the following commands installs served on your system. This installation requires Boost 1.53 or newer, if you do not have Boost installed on your system, you can install it using your favorite package manager or by following these instructions<\/a>.<\/p>\n\n\n\n Now, that you have it installed, it is time to build a simple webserver. This server will just give back “Hello world!” when you query against the endpoint To compile your program, you will need to link against the pthread, boost_system, and served shared objects, and use at least C++-11. On Linux, this would look roughly like the following.<\/p>\n\n\n\n Just run the binary and visit the URL http:\/\/localhost:8080\/hello<\/a> in your browser, and you have successfully reached the hello-world. All of the code for this demo is available on github<\/a>.<\/p>\n\n\n\nGetting To Hello World<\/h2>\n\n\n\n
git clone https:\/\/github.com\/meltwater\/served.git\nmkdir served\/served.build && cd served\/served.build\ncmake ..\/served && make\nsudo make install<\/pre>\n\n\n\n
GET \/hello.<\/code><\/p>\n\n\n\n
#include <served\/served.hpp>\n\nint main() {\n\tserved::multiplexer mux;\n\n\tmux.handle(\"\/hello\")\n\t\t.get([](served::response & res, const served::request & req) {\n\t\t\tres << \"Hello world!\\n\";\n\t\t});\n\n\tserved::net::server server(\"0.0.0.0\", \"8080\", mux);\n\tserver.run(10);\n\n\treturn 0;\n}<\/pre>\n\n\n\n
g++ main.cpp -o demo -std=c++17 -pthread -lboost_system -lserved<\/pre>\n\n\n\n
Performance<\/h2>\n\n\n\n