Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sabudilovskiy authored Mar 22, 2023
0 parents commit 29a4f70
Show file tree
Hide file tree
Showing 27 changed files with 631 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
77 changes: 77 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: CI

'on':
pull_request:
push:
branches:
- develop

env:
UBSAN_OPTIONS: print_stacktrace=1

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

- os: ubuntu-20.04
make: test-release
info: g++-9 + test-release

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

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

- name: Reuse ccache directory
uses: actions/cache@v2
with:
path: ~/.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: |
sudo apt update
sudo apt install --allow-downgrades -y pep8 $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
- 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 &
- 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
38 changes: 38 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Docker build

'on':
pull_request:
push:
branches:
- master
- develop
- feature/**

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

- name: Reuse ccache directory
uses: actions/cache@v2
with:
path: .ccache
key: 'ccache-dir-${{github.ref}}_run-${{github.run_number}}'
restore-keys: |
ccache-dir-${{github.ref}}_run-
ccache-
- 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
22 changes: 22 additions & 0 deletions .github/workflows/update-submodules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Update submodules

on:
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Update submodules
run: |
git submodule update --init
- name: Commit & push changes
run: |
git config user.email "[email protected]"
git config user.name "GitHub Actions - update submodules"
git commit -am "Update submodules"
git push
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__pycache__
build*
compile_commands.json
.cache/
.idea/
.vscode/
.cores/
.ccache/
cmake-build-*
Testing/
configs/static_config.yaml
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "third_party/userver"]
path = third_party/userver
url = https://github.com/userver-framework/userver.git
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.12)
project(service_template CXX)

include(third_party/userver/cmake/SetupEnvironment.cmake)
include(GNUInstallDirs)

add_subdirectory(third_party/userver)


# 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 Tests
add_subdirectory(tests)

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()

set(CONFIG_VAR_PATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}/config_vars.yaml)
set(CONFIG_FALLBACK_PATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}/dynamic_config_fallback.json)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml.in ${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml)

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})
96 changes: 96 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
CMAKE_RELEASE_FLAGS ?=
CMAKE_OS_FLAGS ?= -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 -DUSERVER_FEATURE_REDIS_HI_MALLOC=1
NPROCS ?= $(shell nproc)
CLANG_FORMAT ?= clang-format

# NOTE: use Makefile.local for customization
-include Makefile.local

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

# Debug cmake configuration
build_debug/Makefile:
@git submodule update --init
@mkdir -p build_debug
@cd build_debug && \
cmake -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS) $(CMAKE_DEBUG_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..

# Release cmake configuration
build_release/Makefile:
@git submodule update --init
@mkdir -p build_release
@cd build_release && \
cmake -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS) $(CMAKE_RELEASE_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..

# Run cmake
.PHONY: cmake-debug cmake-release
cmake-debug cmake-release: cmake-%: build_%/Makefile

# Build using cmake
.PHONY: build-debug build-release
build-debug build-release: build-%: cmake-%
@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)
@pep8 tests

# Start the service (via testsuite service runner)
.PHONY: service-start-debug service-start-release
service-start-debug service-start-release: service-start-%: build-%
@cd ./build_$* && $(MAKE) start-service_template

# Cleanup data
.PHONY: clean-debug clean-release
clean-debug clean-release: clean-%:
cd build_$* && $(MAKE) clean

.PHONY: dist-clean
dist-clean:
@rm -rf build_*
@rm -f ./configs/static_config.yaml
@rm -rf tests/__pycache__/
@rm -rf tests/.pytest_cache/

# Install
.PHONY: install-debug install-release
install-debug install-release: install-%: build-%
@cd build_$* && \
cmake --install . -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

# Build and run service in docker environment
.PHONY: docker-start-service-debug docker-start-service-release
docker-start-service-debug docker-start-service-release: docker-start-service-%:
@docker-compose run -p 8080:8080 --rm service_template-container $(MAKE) -- --in-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
1 change: 1 addition & 0 deletions Makefile.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CMAKE_COMMON_FLAGS += -DUSERVER_FEATURE_CRYPTOPP_BLAKE=0 -DUSERVER_FEATURE_GRPC_CHANNELZ=0
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# service_template

Template of a C++ service that uses [userver framework](https://github.com/userver-framework/userver).


## Download and Build

To create your own userver-based service follow the following steps:

1. Press the green "Use this template button" at the top of this github page
2. Clone the service `git clone your-service-repo && cd your-service-repo && git submodule update --init`
3. Give a proper name to your service and replace all the occurences of "service_template" string with that name
4. Feel free to tweak, adjust or fully rewrite the source code of your service.


## Makefile

Makefile contains typicaly useful targets for development:

* `make build-debug` - debug build of the service with all the assertions and sanitizers enabled
* `make build-release` - release build of the service with LTO
* `make test-debug` - does a `make build-debug` and runs all the tests on the result
* `make test-release` - does a `make build-release` and runs all the tests on the result
* `make service-start-debug` - builds the service in debug mode and starts it
* `make service-start-release` - builds the service in release mode and starts it
* `make` or `make all` - builds and runs all the tests in release and debug modes
* `make format` - autoformat all the C++ and Python sources
* `make clean-` - cleans the object files
* `make dist-clean` - clean all, including the CMake cached configurations
* `make install` - does a `make build-release` and run install in directory set in environment `PREFIX`
* `make install-debug` - does a `make build-debug` and runs install in directory set in environment `PREFIX`
* `make docker-COMMAND` - run `make COMMAND` in docker environment
* `make docker-build-debug` - debug build of the service with all the assertions and sanitizers enabled in docker environment
* `make docker-test-debug` - does a `make build-debug` and runs all the tests on the result in docker environment
* `make docker-start-service-release` - does a `make install-release` and runs service in docker environment
* `make docker-start-service-debug` - does a `make install-debug` and runs service in docker environment
* `make docker-clean-data` - stop docker containers

Edit `Makefile.local` to change the default configuration and build options.


## License

The original template is distributed under the [Apache-2.0 License](https://github.com/userver-framework/userver/blob/develop/LICENSE)
and [CLA](https://github.com/userver-framework/userver/blob/develop/CONTRIBUTING.md). Services based on the template may change
the license and CLA.
7 changes: 7 additions & 0 deletions configs/config_vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
worker-threads: 4
worker-fs-threads: 2
logger-level: debug

is_testing: false

server-port: 8080
7 changes: 7 additions & 0 deletions configs/config_vars_testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
worker-threads: 4
worker-fs-threads: 2
logger-level: debug

is_testing: true

server-port: 8080
Loading

0 comments on commit 29a4f70

Please sign in to comment.