1. Press the "Use this template" button at the top right of the GitHub template page.
2. Clone the service:
3. Get userver.
4. Build and start your service. Run in the service repo root:
During the build, you can make a coffee break until approximately the following output appears:
5. Try to send a request.
The answer should be something like this:
Now you are ready for fast and comfortable creation of C++ microservices, services and utilities!
There are ready to use service templates at GitHub:
Link | Contains |
---|---|
https://github.com/userver-framework/service_template | HTTP |
https://github.com/userver-framework/pg_service_template | HTTP, PostgreSQL |
https://github.com/userver-framework/pg_grpc_service_template | HTTP, PostgreSQL, gRPC |
https://github.com/userver-framework/mongo_grpc_service_template | HTTP, MongoDB, gRPC |
To create a service:
git clone your-service-repo && cd your-service-repo
You'll need to get userver before proceeding with local development.
More information on the project directory structure can be found here.
The service templates allow to kickstart the development of your production-ready service, but there can't be a repo for each and every combination of userver libraries. To use additional userver libraries, e.g. userver::grpc
, add to the root CMakeLists.txt
:
Then add the corresponding option to cmake presets, e.g. "USERVER_FEATURE_GRPC": "ON"
.
See tutorial_services for minimal usage examples of various userver libraries.
Service templates use cmake presets for managing service's cmake options. If you DON'T use installed userver and build userver together with the service, then userver also takes its cmake options from there.
If an option has semantic meaning (should be committed to VCS and applied in CI), then it should be added to CMakePresets.json
:
If an option only configures local build (should NOT be commited to VCS and applied in CI), then it should instead be added to CMakeUserPresets.json
:
Service's Makefile
supports custom presets through additional targets like cmake-debug-custom
, cmake-release-custom
, build-debug-custom
, etc.
You can download prebuilt userver using one of the following ways:
Alternatively, install build dependencies for userver, then build userver in one of the following ways:
download_userver()
, then let the service build userver as a subdirectory.Dev Containers is the easiest and least problematic way to get prebuilt userver together with its dependencies.
Debug
cmake presetMakefile
(see the service's README.md) using the IDE's integrated terminaluserver uses CPM for downloading missing packages.
By default, we first try to find the corresponding system package. With USERVER_FORCE_DOWNLOAD_PACKAGES=ON
, no such attempt is made, and we skip straight to CPMAddPackage
. This can be useful to guarantee that the build uses the latest (known to userver) versions of third-party packages.
You can control the usage of CPM with USERVER_DOWNLOAD_*
options. See cmake_options. For example, USERVER_DOWNLOAD_PACKAGES=OFF
is useful for CI and other controlled environments to make sure that no download attempts are made, which ensures repeatability and the best CI build speed.
For details on the download options, refer to CPM documentation. Some advice:
CPM_SOURCE_CACHE
helps to avoid re-downloads with multiple userver build modes or multiple CPM-using projects;CPM_USE_NAMED_CACHE_DIRECTORIES
(which userver enables by default) avoids junk library names shown in IDEs.userver itself can be downloaded and built using CPM. In fact, this is what download_userver() function does in service templates by default.
download_userver()
just calls CPMAddPackage
with some defaults, so you can pin userver VERSION
or GIT_TAG
for reproducible builds.
When acquiring userver via CPM, you first need to install build dependencies. There are options:
Make sure to enable the CMake options to build userver libraries you need, then link to those libraries.
You can install userver globally and then use it from anywhere with find_package
. Make sure to use the same build mode as for your service, otherwise subtle linkage issues will arise.
To build libuserver-all-dev.deb
package run the following shell command:
Pass the cmake options inside BUILD_OPTIONS
. Make sure to at least:
And install the package with the following:
To install userver build it with USERVER_INSTALL=ON
flags in Debug
and Release
modes:
Next, write
in your CMakeLists.txt
. Choose only the necessary components.
Finally, link your source with userver component.
For instance:
Done! You can use installed userver.
Link mariadbclient
additionally for mysql component:
The Docker images provide a container with all the build dependencies preinstalled and with a proper setup of PPAs with databases, compilers and tools:
Image reference | Contains |
---|---|
ghcr.io/userver-framework/ubuntu-22.04-userver-pg:latest | PostgreSQL and userver preinstalled |
ghcr.io/userver-framework/ubuntu-22.04-userver:latest | userver preinstalled |
ghcr.io/userver-framework/ubuntu-22.04-userver-base:latest | only userver dependencies preinstalled |
To run it just use a command like
After that, install the databases and compiler you are planning to use via apt install
and start developing.
To install userver in ghcr.io/userver-framework/ubuntu-22.04-userver-base:latest
you should run the following command
Alternatively see userver install
scripts/docker/ubuntu-22.04-pg.dockerfile
, scripts/docker/ubuntu-22.04.dockerfile
and scripts/docker/base-ubuntu-22.04.dockerfile
respectively. See scripts/docker/
directory and Building Docker container for userver for more inspiration on building your own custom docker containers.You can download and install a .deb
package that is attached to each userver release.
The package currently targets Ubuntu 22.04, for other Ubuntu versions your mileage may vary.
Thanks to Open-Source community we have Conan support.
To build the userver Conan package run the following in the userver root directory:
Make sure to pass flags corresponding to the desired userver libraries, e.g. -o with_grpc=0
.
To use userver as a Conan package in your services add a conanfile.txt
with the required version us the framework, for example:
Run conan install .
to actually install the required package. For more information see the official Conan documentation.
Link with userver in your CMakeLists.txt
as usual:
The userver framework is available at Yandex Cloud Marketplace.
To create a VM with preinstalled userver just click the "Create VM" button and pay for the Cloud hardware usage.
After that the VM is ready to use. SSH to it and use find_package(userver REQUIRED)
in the CMakeLists.txt
to use the preinstalled userver framework.
You can start with service template.
If there a need to update the userver in the VM do the following:
PGO compilation consists of 2 compilation stages: profile collecting and compilation with PGO. You can use PGO compilation doing the following steps:
1) configure userver AND your service with cmake option -DUSERVER_PGO_GENERATE=ON, compile the service; 2) run the resulting binary under the production-like workload to collect profile; 3) run llvm-profdata to convert profraw profile data format into profdata: llvm-profdata merge -output=code.profdata default.profraw 4) configure userver AND your service with cmake option -DUSERVER_PGO_USE=<path_to_profdata>, compile the service.
The resulting binary should be 2-15% faster than without PGO, depending on the code and workload.