-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat core: allow embedded file in cmake/yamake
commit_hash:b0da7c4dfc838f50b993e81a731a24c1ede5ff2c
- Loading branch information
Showing
20 changed files
with
339 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
include_guard(GLOBAL) | ||
|
||
function(userver_embed_file TARGET) | ||
set(OPTIONS) | ||
set(ONE_VALUE_ARGS NAME FILEPATH HPP_FILENAME) | ||
set(MULTI_VALUE_ARGS SQL_FILES) | ||
cmake_parse_arguments( | ||
ARG "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN} | ||
) | ||
if(NOT ARG_HPP_FILENAME) | ||
# new cmake: cmake_path(GET ARG_FILEPATH FILENAME ARG_HPP_FILENAME) | ||
string(REGEX REPLACE ".*/" "" ARG_HPP_FILENAME "${ARG_FILEPATH}") | ||
endif() | ||
|
||
string(SUBSTRING "${ARG_FILEPATH}" 0 1 START) | ||
if(NOT START STREQUAL /) | ||
set(ARG_FILEPATH "${CMAKE_CURRENT_SOURCE_DIR}/${ARG_FILEPATH}") | ||
endif() | ||
|
||
set(CONFIG_HPP ${CMAKE_CURRENT_BINARY_DIR}/embedded/include/generated/${ARG_HPP_FILENAME}.hpp) | ||
add_custom_command( | ||
OUTPUT | ||
${CONFIG_HPP} | ||
DEPENDS | ||
${USERVER_ROOT_DIR}/cmake/embedded_config.cmake | ||
COMMAND | ||
${CMAKE_COMMAND} | ||
-DUSERVER_ROOT_DIR=${USERVER_ROOT_DIR} | ||
-DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} | ||
-DFILEPATH=${ARG_FILEPATH} | ||
-DOUTPUT=${CONFIG_HPP} | ||
-DNAME=${ARG_NAME} | ||
-P ${USERVER_ROOT_DIR}/cmake/embedded_config.cmake | ||
) | ||
add_library(${TARGET} STATIC ${CONFIG_HPP}) | ||
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/embedded/include) | ||
target_link_libraries(${TARGET} PUBLIC userver::universal) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
cmake_policy(SET CMP0053 NEW) | ||
|
||
set(NAMESPACE userver) | ||
set(FILE_IN ${CMAKE_CURRENT_BINARY_DIR}/embedded.h.in) | ||
set(TEMPLATE " | ||
#pragma once | ||
#include <string_view> | ||
#include <userver/utils/resources.hpp> | ||
__asm__(R\"( | ||
.section .rodata | ||
.align 16 | ||
@NAME@_begin: | ||
.incbin \"${FILEPATH}\" | ||
@NAME@_end: | ||
.byte 0 | ||
@NAME@_size: | ||
.int @NAME@_end - @NAME@_begin | ||
.section .text | ||
)\"); | ||
extern \"C\" const char @NAME@_begin[]; | ||
extern \"C\" const char @NAME@_end; | ||
extern \"C\" const int @NAME@_size; | ||
__attribute__((constructor)) void @NAME@_call() { | ||
utils::RegisterResource(\"@NAME@\", std::string_view{@NAME@_begin, static_cast<size_t>(@NAME@_size)}); | ||
} | ||
") | ||
string(CONFIGURE "${TEMPLATE}" RENDERED) | ||
file(WRITE "${OUTPUT}" "${RENDERED}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(userver-samples-embedded_files CXX) | ||
|
||
find_package(userver COMPONENTS core REQUIRED) | ||
|
||
add_library(${PROJECT_NAME}_objs OBJECT | ||
src/say_hello.hpp | ||
src/say_hello.cpp | ||
src/hello_handler.hpp | ||
src/hello_handler.cpp | ||
) | ||
target_link_libraries(${PROJECT_NAME}_objs userver::core) | ||
target_include_directories(${PROJECT_NAME}_objs PUBLIC src) | ||
|
||
add_executable(${PROJECT_NAME} main.cpp) | ||
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_objs) | ||
|
||
# /// [embedded] | ||
userver_embed_file(${PROJECT_NAME}_config | ||
NAME static_config_yaml | ||
FILEPATH static_config.yaml | ||
) | ||
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_config) | ||
# /// [embedded] | ||
|
||
userver_testsuite_add_simple() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <userver/components/minimal_server_component_list.hpp> | ||
#include <userver/utest/using_namespace_userver.hpp> | ||
#include <userver/utils/daemon_run.hpp> | ||
|
||
// Note: this is for the purposes of tests/samples only | ||
#include <userver/utest/using_namespace_userver.hpp> | ||
|
||
#include <hello_handler.hpp> | ||
|
||
#include <generated/static_config.yaml.hpp> | ||
|
||
// [embedded usage] | ||
int main(int, char*[]) { | ||
auto component_list = components::MinimalServerComponentList().Append<samples::hello::HelloHandler>(); | ||
|
||
return utils::DaemonMain(components::InMemoryConfig{utils::FindResource("static_config_yaml")}, component_list); | ||
} | ||
// [embedded usage] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "hello_handler.hpp" | ||
|
||
#include "say_hello.hpp" | ||
|
||
namespace samples::hello { | ||
|
||
std::string HelloHandler:: | ||
HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& /*request_context*/) | ||
const { | ||
// Setting Content-Type: text/plain in a microservice response ensures | ||
// the client interprets it as plain text, preventing misinterpretation or | ||
// errors. Without this header, the client might assume a different format, | ||
// such as JSON, HTML or XML, leading to potential processing issues or | ||
// incorrect handling of the data. | ||
request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); | ||
return samples::hello::SayHelloTo(request.GetArg("name")); | ||
} | ||
|
||
} // namespace samples::hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <userver/components/component_list.hpp> | ||
#include <userver/server/handlers/http_handler_base.hpp> | ||
|
||
// Note: this is for the purposes of tests/samples only | ||
#include <userver/utest/using_namespace_userver.hpp> | ||
|
||
namespace samples::hello { | ||
|
||
class HelloHandler final : public server::handlers::HttpHandlerBase { | ||
public: | ||
// `kName` is used as the component name in static config | ||
static constexpr std::string_view kName = "handler-hello-sample"; | ||
|
||
// Component is valid after construction and is able to accept requests | ||
using HttpHandlerBase::HttpHandlerBase; | ||
|
||
std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) | ||
const override; | ||
}; | ||
|
||
} // namespace samples::hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "say_hello.hpp" | ||
|
||
#include <fmt/format.h> | ||
|
||
namespace samples::hello { | ||
|
||
std::string SayHelloTo(std::string_view name) { | ||
if (name.empty()) { | ||
name = "unknown user"; | ||
} | ||
|
||
return fmt::format("Hello, {}!\n", name); | ||
} | ||
|
||
} // namespace samples::hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
namespace samples::hello { | ||
|
||
std::string SayHelloTo(std::string_view name); | ||
|
||
} // namespace samples::hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# yaml | ||
components_manager: | ||
task_processors: # Task processor is an executor for coroutine tasks | ||
main-task-processor: # Make a task processor for CPU-bound coroutine tasks. | ||
worker_threads: 4 # Process tasks in 4 threads. | ||
|
||
fs-task-processor: # Make a separate task processor for filesystem bound tasks. | ||
worker_threads: 1 | ||
|
||
default_task_processor: main-task-processor # Task processor in which components start. | ||
|
||
components: # Configuring components that were registered via component_list | ||
server: | ||
listener: # configuring the main listening socket... | ||
port: 8096 # ...to listen on this port and... | ||
task_processor: main-task-processor # ...process incoming requests on this task processor. | ||
logging: | ||
fs-task-processor: fs-task-processor | ||
loggers: | ||
default: | ||
file_path: '@stderr' | ||
level: debug | ||
overflow_behavior: discard # Drop logs if the system is too busy to write them down. | ||
|
||
handler-hello-sample: # Finally! Our handler. | ||
path: /hello # Registering handler by URL '/hello'. | ||
method: GET,POST # It will only reply to GET (HEAD) and POST requests. | ||
task_processor: main-task-processor # Run it on CPU bound task processor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest_plugins = ['pytest_userver.plugins.core'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
|
||
# forced port is required for embedded config | ||
@pytest.fixture(scope='session') | ||
def service_port() -> int: | ||
return 8096 | ||
|
||
|
||
# /// [Functional test] | ||
async def test_hello_base(service_client): | ||
response = await service_client.get('/hello') | ||
assert response.status == 200 | ||
assert 'text/plain' in response.headers['Content-Type'] | ||
assert response.text == 'Hello, unknown user!\n' | ||
assert 'X-RequestId' not in response.headers.keys(), 'Unexpected header' | ||
# /// [Functional test] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.