To follow along with this post it is recommended that you have a fairly recent C++ compiler available and that you've installed build2 (which is the build system used). Installing build2 is as easy as running a shell-script. See https://build2.org/install.xhtml for details.
Creating executable project
First we need to create a new build2 project, thankfully this is easy using the bdep new command. Issue the following commands in your terminal:
Second, we need to instruct build2 where to find our dependencies, in this case libruntime-io and libseafire-server.
libruntime-io is a thin polymorphic wrapper around ASIO.
Add the following lines to ~/workspace/basics/repositories.manifest:
and the following two lines to ~/workspace/basics/manifest:
Link with libruntime-io and libseafire-server
Third, we need to link our executable with libruntime-io and libseafire-server. To accomplish this, we add the following two lines to ~/workspace/basics/basics/buildfile, just after the first line.
Include required header files
Now it's time to do some coding. To be able to use the libruntime-io and libseafire-server libraries, we need to add a few include directives to our executable. Replace the include-directives in ~/workspace/basics/basics/basics.cxx with the following:
Implement the request handler
Before we can implement the server listening for connections, we need a request handler. Our very simple handler simply responds with hello, world to every request. Sent with the text/plain content-type. Implement the request handler as follows:
Implement the server
Creating and running the server requires three things, a scheduler, an acceptor set, and the above implemented request handler. Replace the pre-generated main-function with the following code:
Building and running the project
Finally! It's time to build our project, which is easily accomplished with the bpkg and b commands, shipped with build2. First we create an out-of-source build directory using bpkg and then build the project using b. Run the following commands in your terminal:
Once the above commands have finished, you should have an executable in ~/workspace/basics/basics, which when run will listen for HTTP connections on port 8080.
Let's run it:
That's it! By following the steps in this post you've successfully learned how to setup a new project using build2 and link with libruntime-io and libseafire-server. You've learned how to implement a simple HTTP request handler and setup an HTTP-server using the request handler.
To finish things off you also learned how to build and run the server. Congratulations! Now let's go build something amazing.
The complete source code can be found at: https://code.helloryan.se/foss/libseafire-examples.
In a future post I will dive deeper into the libseafire family of libraries, in particular we'll explore how to implement resource-handlers instead of request-handlers using libseafire-resources and add routing capabilities to our server using libseafire-routing.
Did you know I also do consulting (mainly C++, C# and TypeScript) for a living? If you need my help or if you're interested in knowing more about my open-source projects, drop me a mail at firstname.lastname@example.org or use the form below.