Building a Simple Pastebin Service with Userver Framework

Introduction

Hello, tech enthusiasts! I’m Vasily Kulikov, a lead developer at Yandex’s Tech Platform for Ecom and RideTech. Over the past five years, I’ve been developing the Userver framework, a C++ web framework that enables the creation of high-load, fault-tolerant services. Today, I’ll guide you through building a simple yet functional pastebin service using Userver. This service will act as the backend (and partially the frontend) for a website where users can post text notes and receive a link to them. Through this example, you’ll see how to write a service on Userver and the API used for everyday backend tasks.

Overall Architecture

Let’s start by defining the overall architecture of our site. We’ll create a single stateless service called upastebin with its own PostgreSQL database. This service will serve static content (HTML, JS, CSS) from memory. In more complex scenarios with heavy static content and dynamic pages, an Nginx server with a frontend framework might be necessary, but we’ll keep it simple for this example.

Building a Simple Pastebin Service with Userver Framework

Key Components of upastebin:

  • Endpoints:
    • Serve a page with a specific note.
    • Render the page in the browser for static content.
  • Pages:
    • Create a new note.
  • API Endpoints:
    • POST /api/v1/posts — Create a new note.
    • GET /api/v1/posts/{id} — Retrieve the text of a specific note.
    • GET /api/v1/latest — Get a list of the latest notes.

Static Content Delivery:

  • /{id} — Serve the page with the note.
  • / — Write a new post.
  • /r/* — Serve other static content (JS, CSS).

Getting Started

Let’s dive into the development process. You can start from scratch or use a ready-made service template from the Userver GitHub repository. We’ll use one of these templates to create our project:

git clone https://github.com/segoon/upastebin/ && cd upastebin

To rename the service from pg_service_template to upastebin, use the following command:

find . -not -path "./third_party/" -not -path ".git/" -not -path './build_*' -type f | xargs sed -i 's/pg_service_template/upastebin/g'

Integrating Userver

To integrate Userver into your project, you have several options:

  • Download the latest release from GitHub.
  • Build Userver yourself and install it using cmake -install or cpack + dpkg -i.
  • Add Userver as a separate directory and build it as part of your project.
  • Use a prepared Docker image with Userver pre-installed.

We’ll choose the separate directory approach:

cd third_party
git clone --depth 1 https://github.com/userver-framework/userver.git

Don’t forget to add third_party/userver to your .gitignore file!

Building the Service

Now, let’s build the service:

make build-debug

This command will automatically run cmake in the build_debug directory and compile the service binary build_debug/upastebin. To build a release version, replace debug with release.

Testing the Service

Run unit tests using:

make test-debug

To quickly start the service locally and test endpoints with curl or open the local site in a browser, use:

make service-start-debug

This command will launch the service instance locally with a temporary PostgreSQL database. The service will run with a static configuration specifying the temporary PostgreSQL daemon port. When you terminate the command with Ctrl + C, the service, PostgreSQL, and the environment will be cleaned up.

Conclusion

Congratulations! You’ve successfully built a simple pastebin service using the Userver framework. This example demonstrates the basics of creating a service with Userver, but the framework is capable of much more complex services. If you’re interested in exploring further, visit the Userver website or join the Userver Telegram chat for more insights and support.

You can download the source code for this example from GitHub. Happy coding!