Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton3 authored Dec 28, 2024
0 parents commit a346393
Show file tree
Hide file tree
Showing 27 changed files with 629 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: google
DerivePointerAlignment: false
IncludeBlocks: Preserve
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI

'on':
schedule:
- cron: '30 5 * * 1' # Every Monday at 5:30
pull_request:
push:
branches:
- develop

env:
UBSAN_OPTIONS: print_stacktrace=1

jobs:
posix:
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-22.04
make: test-debug
info: g++-11 + test-debug

- os: ubuntu-22.04
make: test-release
info: g++-11 + test-release

name: '${{matrix.os}}: ${{matrix.info}}'
runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Reuse ccache directory
uses: actions/cache@v4
with:
path: ~/.cache/ccache
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
restore-keys: |
${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-'
${{matrix.os}} ${{matrix.info}} ccache-
- name: Install packages
run: |
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
sudo apt update
sudo apt install --allow-downgrades -y $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
python3 -m pip install -r requirements.txt
- name: Setup ccache
run: |
ccache -M 2.0GB
ccache -s
- name: Run ${{matrix.make}}
run: |
make ${{matrix.make}}
- name: Test install ${{matrix.make}}
if: matrix.make == 'test-release'
run: |
make dist-clean
make install PREFIX=`pwd`/local_installation/
- name: Test run after install
if: matrix.make == 'test-release'
run: >-
./local_installation/bin/service_template
--config=./local_installation/etc/service_template/static_config.yaml
--config_vars=./local_installation/etc/service_template/config_vars.yaml
&
- name: Check work run service
if: matrix.make == 'test-release'
run: |
ps aux | grep service_template | grep config && curl http://localhost:8080/ping -v
- name: Stop all
if: matrix.make == 'test-release'
run: |
killall service_template
45 changes: 45 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Docker build

'on':
schedule:
- cron: '30 5 * * 1' # Every Monday at 5:30
pull_request:
push:
branches:
- master
- develop
- feature/**

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Reuse ccache directory
uses: actions/cache@v4
with:
path: .ccache
key: 'ccache-dir-${{github.ref}}_run-${{github.run_number}}'
restore-keys: |
ccache-dir-${{github.ref}}_run-
ccache-
- name: Install docker-compose
run: |
sudo apt update
sudo apt install --allow-downgrades -y docker-compose
- name: Setup ccache
run: docker-compose run --rm service_template-container bash -c 'ccache -M 2.0GB && ccache -s'

- name: Cmake
run: make docker-cmake-release

- name: Build
run: make docker-build-release

- name: Run tests
run: make docker-test-release
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
__pycache__
build*/
compile_commands.json
.cache/
.ccache/
.idea/
.vscode/
!.vscode/c_cpp_properties.json
!.vscode/cmake-variants.yaml
!.vscode/README.md
.cores/
cmake-build-*
Testing/
.DS_Store
Makefile.local
2 changes: 2 additions & 0 deletions .vscode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the minimal configuration for VSCode IDE.
If you don't use VSCode, feel free to delete this directory.
10 changes: 10 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CMake",
"compileCommands": "${config:cmake.buildDirectory}/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
17 changes: 17 additions & 0 deletions .vscode/cmake-variants.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
buildType:
default: debug
choices:
debug:
short: Debug
long: Unoptimized debug build with sanitizers
buildType: Debug
settings:
CMAKE_EXPORT_COMPILE_COMMANDS: ON
USERVER_SANITIZE: addr ub

release:
short: Release
long: Optimized build
buildType: Release
settings:
CMAKE_EXPORT_COMPILE_COMMANDS: ON
67 changes: 67 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
cmake_minimum_required(VERSION 3.12)
project(service_template CXX)


# Adding userver dependency
find_package(userver COMPONENTS core postgresql QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Compatibility mode: some systems don't support these features
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)

if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
message(STATUS "Using userver framework from third_party/userver")
add_subdirectory(third_party/userver)
else()
message(FATAL_ERROR "Either install the userver or provide a path to it")
endif()
endif()

userver_setup_environment()


# Common sources
add_library(${PROJECT_NAME}_objs OBJECT
src/hello.hpp
src/hello.cpp
)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core)


# The Service
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_objs)


# Unit Tests
add_executable(${PROJECT_NAME}_unittest
src/hello_test.cpp
)
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver::utest)
add_google_tests(${PROJECT_NAME}_unittest)


# Benchmarks
add_executable(${PROJECT_NAME}_benchmark
src/hello_benchmark.cpp
)
target_link_libraries(${PROJECT_NAME}_benchmark PRIVATE ${PROJECT_NAME}_objs userver::ubench)
add_google_benchmark_tests(${PROJECT_NAME}_benchmark)

# Functional testing
userver_testsuite_add_simple()

# Install
include(GNUInstallDirs)

if(DEFINED ENV{PREFIX})
message(STATUS "Set install prefix: $ENV{PREFIX}")
file(TO_CMAKE_PATH "$ENV{PREFIX}" PREFIX_PATH)
set(CMAKE_INSTALL_PREFIX "${PREFIX_PATH}")
endif()

file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})
install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})
98 changes: 98 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
CMAKE_RELEASE_FLAGS ?=
NPROCS ?= $(shell nproc)
CLANG_FORMAT ?= clang-format
DOCKER_COMPOSE ?= docker-compose

# NOTE: use Makefile.local to override the options defined above.
-include Makefile.local

CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)

.PHONY: all
all: test-debug test-release

# Run cmake
.PHONY: cmake-debug
cmake-debug:
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)

.PHONY: cmake-release
cmake-release:
cmake -B build_release $(CMAKE_RELEASE_FLAGS)

build_debug/CMakeCache.txt: cmake-debug
build_release/CMakeCache.txt: cmake-release

# Build using cmake
.PHONY: build-debug build-release
build-debug build-release: build-%: build_%/CMakeCache.txt
cmake --build build_$* -j $(NPROCS) --target service_template

# Test
.PHONY: test-debug test-release
test-debug test-release: test-%: build-%
cmake --build build_$* -j $(NPROCS) --target service_template_unittest
cmake --build build_$* -j $(NPROCS) --target service_template_benchmark
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
pycodestyle tests

# Start the service (via testsuite service runner)
.PHONY: start-debug start-release
start-debug start-release: start-%:
cmake --build build_$* -v --target=start-service_template

.PHONY: service-start-debug service-start-release
service-start-debug service-start-release: service-start-%: start-%

# Cleanup data
.PHONY: clean-debug clean-release
clean-debug clean-release: clean-%:
cmake --build build_$* --target clean

.PHONY: dist-clean
dist-clean:
rm -rf build_*
rm -rf tests/__pycache__/
rm -rf tests/.pytest_cache/

# Install
.PHONY: install-debug install-release
install-debug install-release: install-%: build-%
cmake --install build_$* -v --component service_template

.PHONY: install
install: install-release

# Format the sources
.PHONY: format
format:
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
find tests -name '*.py' -type f | xargs autopep8 -i

# Internal hidden targets that are used only in docker environment
.PHONY: --in-docker-start-debug --in-docker-start-release
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
/home/user/.local/bin/service_template \
--config /home/user/.local/etc/service_template/static_config.yaml \
--config_vars /home/user/.local/etc/service_template/config_vars.yaml

# Build and run service in docker environment
.PHONY: docker-start-debug docker-start-release
docker-start-debug docker-start-release: docker-start-%:
$(DOCKER_COMPOSE) run -p 8080:8080 --rm service_template-container make -- --in-docker-start-$*

.PHONY: docker-start-service-debug docker-start-service-release
docker-start-service-debug docker-start-service-release: docker-start-service-%: docker-start-%

# Start specific target in docker environment
.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
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-%:
$(DOCKER_COMPOSE) run --rm service_template-container make $*

# Stop docker container and cleanup data
.PHONY: docker-clean-data
docker-clean-data:
$(DOCKER_COMPOSE) down -v
Loading

0 comments on commit a346393

Please sign in to comment.