Make sure that you can compile and run core tests as described at Configure, Build and Install.
Note that there is a ready to use opensource service template to ease the development of your userver based services. The template already has a preconfigured CI, build and install scripts, testsuite and unit-tests setups.
Typical HTTP server application in userver consists of the following parts:
Let's write a simple server that responds with "Hello world!\n" on every request to /hello
URL.
HTTP handlers must derive from server::handlers::HttpHandlerBase
and have a name, that is obtainable at compile time via kName
variable and is obtainable at runtime via HandlerName()
.
The primary functionality of the handler should be located in HandleRequestThrow
function. Return value of this function is the HTTP response body. If an exception exc
derived from server::handlers::CustomHandlerException
is thrown from the function then the HTTP response code will be set to exc.GetCode()
and exc.GetExternalErrorBody()
would be used for HTTP response body. Otherwise if an exception exc
derived from std::exception
is thrown from the function then the HTTP response code will be set to 500
.
Handle*
functions are invoked concurrently on the same instance of the handler class. Use synchronization primitives or do not modify shared data in Handle*
.Now we have to configure the service by providing task_processors
and default_task_processor
options for the components::ManagerControllerComponent and configuring each component in components
section:
Note that all the components and handlers have their static options additionally described in docs.
Finally, we add our component to the components::MinimalServerComponentList()
, and start the server with static configuration file passed from command line.
To build the sample, execute the following build steps at the userver root directory:
The sample could be started by running make start-userver-samples-hello_service
. The command would invoke testsuite start target that sets proper paths in the configuration files and starts the service.
To start the service manually run ./samples/hello_service/userver-samples-hello_service -c </path/to/static_config.yaml>
.
static_config.yaml
userver-samples-hello_service
will look for a file with name config_dev.yaml
static_config.yaml
and file from samples
directory into build directory.Now you can send a request to your server from another terminal:
Functional tests for the service could be implemented using the service_client fixture from pytest_userver.plugins.core in the following way:
Do not forget to add the plugin in conftest.py:
See the full example at: