Can C++ Power a SaaS Backend? Trying the Drogon Framework

I’m planning to build my SaaS server in C++ using the Drogon framework… never used it before, but it looks promising. First step: build Drogon and tinker with one of their butt-ugly examples, to make it look nice and presentable.

At this point, devs everywhere will scream: ‘No! Don’t use C++ for a web app! Ruby on Rails! Next.js! Go! Use Rust!’

Shut up, everyone. A guy told me to “use what I know if I want to go fast,” and I know C++. Granted, that guy still uses PHP, & jQuery, but lets try this out with C++, shall we? Besides, I’ve got an idea for a future app where C++ might actually make sense.

Let’s see what Drogon can do…

Trying out Drogon

The first step is to build Drogon. I took one look Drogon’s installation instructions for Windows, and decided “screw it, I’ll use Linux instead.” That’s what the app will run on, anyway. So I installed Ubuntu on Windows using weasel 2 (a.k.a., WSL2), and then fetched Drogon’s code off GitHub.

Building Drogon is easy, with everything building out-of-the box. There were a few compiler warnings, but otherwise very clean.

The examples are nice and short, but butt-ugly. That’s actually a good thing, because it means that all the code demonstrates how to use specific features. But, a real SaaS app needs to look presentable.

I picked the login demo for my first project which, as I said, is functional but butt ugly. Seriously, look at it:

Drogon login example original

Time to polish this turd…

The first thing we need to make it look nice, is to be able to serve static files such as css and images. That’s a one line addition:

app().setDocumentRoot("./static/public")

This is easy.

Next, I decided that I wanted to see HTTP accesses logged to the console. My first attempt didn’t work. ChatGPT was feeding me garbage, and a few hours later it still wasn’t working right. Logging accesses is a basic feature. Why is this so hard?

Then I spotted the solution: hidden in lib/inc/drogon/plugins was a plugin called AccessLogger. Exactly what I need!

A little work and a config file later, and I could see the HTTP requests. Nice!

Guys! How to set up AccessLogger should be in the “Getting Started” documentation.

Now I need a CSS theme. After a quick search I settled on using GMX.css. Why? Because it’s a small Material 3 design system in plain CSS/JS. No need for external build tools. I just download the files, and add them to my project.

From here on, it’s mostly tweaking the template files. Drogon’s templating system is called C++ Server Pages (CSP). It’s basically HTML with special blocks to insert strings from C++ code, or even execute C++ code in the template. The documentation is very very concise, with zero examples. So, it takes a bit of work to figure things out.

The templates are compiled to machine code, which is great for performance, but sucks when it comes to tweaking them. Drogon does have the ability to dynamically rebuild templates on-the-fly, but it’s a bit slow and the server would crash when using the new template. I’m not sure what went wrong there, but it just didn’t work right for me… until I wanted to record the failure, that is. Then it suddenly worked okay.

After a bit of work, I had a nice looking login screen.

Drogon login example beautified

The form sits in a good looking window on top of a background image. I even added a bit of javascript to switch between light and dark mode. Because, you know how much software devs looooove their dark mode. ;-)

Even the background images change between light and dark! A bit extravagant and unnecessary, but, at this stage I’m only playing.

Is Drogon the Right Saas Framework?

So, is drogon the right framework for my SaaS backend? It’s a bit too early to tell. So far I’ve learnt how to

  • Register URL handlers
  • Serve static files
  • Make the frontend look nice
  • And build pages using the CSP template system

However, I haven’t really touched other important stuff, such as:

  • Page controllers
  • Filters/middleware
  • Database access

I’m going to experiment further, by building an actual login system complete with proper user accounts that you can sign up for.

Interested? Click here to join the newsletter.

1 thought on “Can C++ Power a SaaS Backend? Trying the Drogon Framework”

  1. I tried building drogon on windows too, it screwed me till I was thoroughly frustrated then I just used the pre-built one on Ubuntu (apt install drogon) and it finally worked

Leave a Comment

Your email address will not be published. Required fields are marked *

 


Shopping Cart
Scroll to Top