Skip to content

Commit a346393

Browse files
authored
Initial commit
0 parents  commit a346393

27 files changed

+629
-0
lines changed

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BasedOnStyle: google
2+
DerivePointerAlignment: false
3+
IncludeBlocks: Preserve

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "github-actions" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "weekly"

.github/workflows/ci.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI
2+
3+
'on':
4+
schedule:
5+
- cron: '30 5 * * 1' # Every Monday at 5:30
6+
pull_request:
7+
push:
8+
branches:
9+
- develop
10+
11+
env:
12+
UBSAN_OPTIONS: print_stacktrace=1
13+
14+
jobs:
15+
posix:
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
include:
20+
- os: ubuntu-22.04
21+
make: test-debug
22+
info: g++-11 + test-debug
23+
24+
- os: ubuntu-22.04
25+
make: test-release
26+
info: g++-11 + test-release
27+
28+
name: '${{matrix.os}}: ${{matrix.info}}'
29+
runs-on: ${{matrix.os}}
30+
31+
steps:
32+
- uses: actions/checkout@v4
33+
with:
34+
submodules: true
35+
36+
- name: Reuse ccache directory
37+
uses: actions/cache@v4
38+
with:
39+
path: ~/.cache/ccache
40+
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
41+
restore-keys: |
42+
${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-'
43+
${{matrix.os}} ${{matrix.info}} ccache-
44+
45+
- name: Install packages
46+
run: |
47+
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
48+
sudo apt update
49+
sudo apt install --allow-downgrades -y $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
50+
python3 -m pip install -r requirements.txt
51+
52+
- name: Setup ccache
53+
run: |
54+
ccache -M 2.0GB
55+
ccache -s
56+
57+
- name: Run ${{matrix.make}}
58+
run: |
59+
make ${{matrix.make}}
60+
61+
- name: Test install ${{matrix.make}}
62+
if: matrix.make == 'test-release'
63+
run: |
64+
make dist-clean
65+
make install PREFIX=`pwd`/local_installation/
66+
67+
- name: Test run after install
68+
if: matrix.make == 'test-release'
69+
run: >-
70+
./local_installation/bin/service_template
71+
--config=./local_installation/etc/service_template/static_config.yaml
72+
--config_vars=./local_installation/etc/service_template/config_vars.yaml
73+
&
74+
75+
- name: Check work run service
76+
if: matrix.make == 'test-release'
77+
run: |
78+
ps aux | grep service_template | grep config && curl http://localhost:8080/ping -v
79+
80+
- name: Stop all
81+
if: matrix.make == 'test-release'
82+
run: |
83+
killall service_template

.github/workflows/docker.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Docker build
2+
3+
'on':
4+
schedule:
5+
- cron: '30 5 * * 1' # Every Monday at 5:30
6+
pull_request:
7+
push:
8+
branches:
9+
- master
10+
- develop
11+
- feature/**
12+
13+
jobs:
14+
tests:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
submodules: true
20+
21+
- name: Reuse ccache directory
22+
uses: actions/cache@v4
23+
with:
24+
path: .ccache
25+
key: 'ccache-dir-${{github.ref}}_run-${{github.run_number}}'
26+
restore-keys: |
27+
ccache-dir-${{github.ref}}_run-
28+
ccache-
29+
30+
- name: Install docker-compose
31+
run: |
32+
sudo apt update
33+
sudo apt install --allow-downgrades -y docker-compose
34+
35+
- name: Setup ccache
36+
run: docker-compose run --rm service_template-container bash -c 'ccache -M 2.0GB && ccache -s'
37+
38+
- name: Cmake
39+
run: make docker-cmake-release
40+
41+
- name: Build
42+
run: make docker-build-release
43+
44+
- name: Run tests
45+
run: make docker-test-release

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
__pycache__
2+
build*/
3+
compile_commands.json
4+
.cache/
5+
.ccache/
6+
.idea/
7+
.vscode/
8+
!.vscode/c_cpp_properties.json
9+
!.vscode/cmake-variants.yaml
10+
!.vscode/README.md
11+
.cores/
12+
cmake-build-*
13+
Testing/
14+
.DS_Store
15+
Makefile.local

.vscode/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the minimal configuration for VSCode IDE.
2+
If you don't use VSCode, feel free to delete this directory.

.vscode/c_cpp_properties.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "CMake",
5+
"compileCommands": "${config:cmake.buildDirectory}/compile_commands.json",
6+
"configurationProvider": "ms-vscode.cmake-tools"
7+
}
8+
],
9+
"version": 4
10+
}

.vscode/cmake-variants.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
buildType:
2+
default: debug
3+
choices:
4+
debug:
5+
short: Debug
6+
long: Unoptimized debug build with sanitizers
7+
buildType: Debug
8+
settings:
9+
CMAKE_EXPORT_COMPILE_COMMANDS: ON
10+
USERVER_SANITIZE: addr ub
11+
12+
release:
13+
short: Release
14+
long: Optimized build
15+
buildType: Release
16+
settings:
17+
CMAKE_EXPORT_COMPILE_COMMANDS: ON

CMakeLists.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(service_template CXX)
3+
4+
5+
# Adding userver dependency
6+
find_package(userver COMPONENTS core postgresql QUIET)
7+
if(NOT userver_FOUND) # Fallback to subdirectory usage
8+
# Compatibility mode: some systems don't support these features
9+
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
10+
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
11+
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)
12+
13+
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
14+
message(STATUS "Using userver framework from third_party/userver")
15+
add_subdirectory(third_party/userver)
16+
else()
17+
message(FATAL_ERROR "Either install the userver or provide a path to it")
18+
endif()
19+
endif()
20+
21+
userver_setup_environment()
22+
23+
24+
# Common sources
25+
add_library(${PROJECT_NAME}_objs OBJECT
26+
src/hello.hpp
27+
src/hello.cpp
28+
)
29+
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core)
30+
31+
32+
# The Service
33+
add_executable(${PROJECT_NAME} src/main.cpp)
34+
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_objs)
35+
36+
37+
# Unit Tests
38+
add_executable(${PROJECT_NAME}_unittest
39+
src/hello_test.cpp
40+
)
41+
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver::utest)
42+
add_google_tests(${PROJECT_NAME}_unittest)
43+
44+
45+
# Benchmarks
46+
add_executable(${PROJECT_NAME}_benchmark
47+
src/hello_benchmark.cpp
48+
)
49+
target_link_libraries(${PROJECT_NAME}_benchmark PRIVATE ${PROJECT_NAME}_objs userver::ubench)
50+
add_google_benchmark_tests(${PROJECT_NAME}_benchmark)
51+
52+
# Functional testing
53+
userver_testsuite_add_simple()
54+
55+
# Install
56+
include(GNUInstallDirs)
57+
58+
if(DEFINED ENV{PREFIX})
59+
message(STATUS "Set install prefix: $ENV{PREFIX}")
60+
file(TO_CMAKE_PATH "$ENV{PREFIX}" PREFIX_PATH)
61+
set(CMAKE_INSTALL_PREFIX "${PREFIX_PATH}")
62+
endif()
63+
64+
file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)
65+
66+
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})
67+
install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})

Makefile

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2+
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
3+
CMAKE_RELEASE_FLAGS ?=
4+
NPROCS ?= $(shell nproc)
5+
CLANG_FORMAT ?= clang-format
6+
DOCKER_COMPOSE ?= docker-compose
7+
8+
# NOTE: use Makefile.local to override the options defined above.
9+
-include Makefile.local
10+
11+
CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
12+
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)
13+
14+
.PHONY: all
15+
all: test-debug test-release
16+
17+
# Run cmake
18+
.PHONY: cmake-debug
19+
cmake-debug:
20+
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)
21+
22+
.PHONY: cmake-release
23+
cmake-release:
24+
cmake -B build_release $(CMAKE_RELEASE_FLAGS)
25+
26+
build_debug/CMakeCache.txt: cmake-debug
27+
build_release/CMakeCache.txt: cmake-release
28+
29+
# Build using cmake
30+
.PHONY: build-debug build-release
31+
build-debug build-release: build-%: build_%/CMakeCache.txt
32+
cmake --build build_$* -j $(NPROCS) --target service_template
33+
34+
# Test
35+
.PHONY: test-debug test-release
36+
test-debug test-release: test-%: build-%
37+
cmake --build build_$* -j $(NPROCS) --target service_template_unittest
38+
cmake --build build_$* -j $(NPROCS) --target service_template_benchmark
39+
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
40+
pycodestyle tests
41+
42+
# Start the service (via testsuite service runner)
43+
.PHONY: start-debug start-release
44+
start-debug start-release: start-%:
45+
cmake --build build_$* -v --target=start-service_template
46+
47+
.PHONY: service-start-debug service-start-release
48+
service-start-debug service-start-release: service-start-%: start-%
49+
50+
# Cleanup data
51+
.PHONY: clean-debug clean-release
52+
clean-debug clean-release: clean-%:
53+
cmake --build build_$* --target clean
54+
55+
.PHONY: dist-clean
56+
dist-clean:
57+
rm -rf build_*
58+
rm -rf tests/__pycache__/
59+
rm -rf tests/.pytest_cache/
60+
61+
# Install
62+
.PHONY: install-debug install-release
63+
install-debug install-release: install-%: build-%
64+
cmake --install build_$* -v --component service_template
65+
66+
.PHONY: install
67+
install: install-release
68+
69+
# Format the sources
70+
.PHONY: format
71+
format:
72+
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
73+
find tests -name '*.py' -type f | xargs autopep8 -i
74+
75+
# Internal hidden targets that are used only in docker environment
76+
.PHONY: --in-docker-start-debug --in-docker-start-release
77+
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
78+
/home/user/.local/bin/service_template \
79+
--config /home/user/.local/etc/service_template/static_config.yaml \
80+
--config_vars /home/user/.local/etc/service_template/config_vars.yaml
81+
82+
# Build and run service in docker environment
83+
.PHONY: docker-start-debug docker-start-release
84+
docker-start-debug docker-start-release: docker-start-%:
85+
$(DOCKER_COMPOSE) run -p 8080:8080 --rm service_template-container make -- --in-docker-start-$*
86+
87+
.PHONY: docker-start-service-debug docker-start-service-release
88+
docker-start-service-debug docker-start-service-release: docker-start-service-%: docker-start-%
89+
90+
# Start specific target in docker environment
91+
.PHONY: docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release
92+
docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release: docker-%:
93+
$(DOCKER_COMPOSE) run --rm service_template-container make $*
94+
95+
# Stop docker container and cleanup data
96+
.PHONY: docker-clean-data
97+
docker-clean-data:
98+
$(DOCKER_COMPOSE) down -v

0 commit comments

Comments
 (0)