Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ jobs:
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: Install mongo
run: |
wget -qO- https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mongodb-server-7.0.gpg >/dev/null
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" \
| sudo tee -a /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update
sudo apt install mongodb-org
sudo apt install mongodb-mongosh

- name: Setup ccache
run: |
ccache -M 2.0GB
Expand All @@ -67,17 +76,17 @@ jobs:
- 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
./local_installation/bin/mongo_grpc_service_template
--config=./local_installation/etc/mongo_grpc_service_template/static_config.yaml
--config_vars=./local_installation/etc/mongo_grpc_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
ps aux | grep mongo_grpc_service_template | grep config && curl http://localhost:8080/ping -v

- name: Stop all
if: matrix.make == 'test-release'
run: |
killall service_template
killall mongo_grpc_service_template
3 changes: 2 additions & 1 deletion .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ name: Docker build

jobs:
tests:
if: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -33,7 +34,7 @@ jobs:
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'
run: docker-compose run --rm mongo_grpc_service_template-container bash -c 'ccache -M 2.0GB && ccache -s'

- name: Cmake
run: make docker-cmake-release
Expand Down
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
cmake_minimum_required(VERSION 3.12)
project(service_template CXX)
project(mongo_grpc_service_template CXX)


# Adding userver dependency
find_package(userver COMPONENTS core postgresql QUIET)
find_package(userver COMPONENTS core mongo grpc QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Enable userver libraries that are needed in this project
set(USERVER_FEATURE_MONGODB ON CACHE BOOL "" FORCE)
set(USERVER_FEATURE_GRPC ON CACHE BOOL "" FORCE)

# 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)
Expand All @@ -25,8 +29,14 @@ userver_setup_environment()
add_library(${PROJECT_NAME}_objs OBJECT
src/hello.hpp
src/hello.cpp
src/hello_client.hpp
src/hello_client.cpp
)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::mongo userver::grpc)

# Create a proto library with userver extensions
userver_add_grpc_library(${PROJECT_NAME}_proto PROTOS handlers/hello.proto)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC ${PROJECT_NAME}_proto)


# The Service
Expand Down
26 changes: 10 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ 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
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_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
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_service_template_unittest
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_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-%
cmake --build build_$* -v --target=start-mongo_grpc_service_template

# Cleanup data
.PHONY: clean-debug clean-release
Expand All @@ -61,7 +58,7 @@ dist-clean:
# Install
.PHONY: install-debug install-release
install-debug install-release: install-%: build-%
cmake --install build_$* -v --component service_template
cmake --install build_$* -v --component mongo_grpc_service_template

.PHONY: install
install: install-release
Expand All @@ -75,22 +72,19 @@ format:
# 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
/home/user/.local/bin/mongo_grpc_service_template \
--config /home/user/.local/etc/mongo_grpc_service_template/static_config.yaml \
--config_vars /home/user/.local/etc/mongo_grpc_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-%
$(DOCKER_COMPOSE) run -p 8080:8080 -p 8081:8081 --rm mongo_grpc_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 $*
$(DOCKER_COMPOSE) run --rm mongo_grpc_service_template-container make $*

# Stop docker container and cleanup data
.PHONY: docker-clean-data
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# service_template
# mongo_grpc_service_template

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


## Download and Build
Expand All @@ -9,7 +9,7 @@ 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
3. Give a proper name to your service and replace all the occurences of "mongo_grpc_service_template" string with that name
4. Feel free to tweak, adjust or fully rewrite the source code of your service.


Expand Down
13 changes: 13 additions & 0 deletions configs/config_vars.docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
worker-threads: 4
worker-fs-threads: 2
worker-grpc-threads: 2
logger-level: debug

is_testing: false

server-port: 8080
server-grpc-port: 8081

hello-endpoint: '[::1]:8081'

dbconnection: 'mongodb://localhost:27017/admin'
6 changes: 6 additions & 0 deletions configs/config_vars.testing.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
worker-threads: 4
worker-fs-threads: 2
worker-grpc-threads: 2
logger-level: debug

is_testing: true

server-port: 8080
server-grpc-port: 8081

hello-endpoint: '[::1]:8081'

dbconnection: 'mongodb://localhost:27217/admin'
6 changes: 6 additions & 0 deletions configs/config_vars.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
worker-threads: 4
worker-fs-threads: 2
worker-grpc-threads: 2
logger-level: info

is_testing: false

server-port: 8080
server-grpc-port: 8081

hello-endpoint: '[::1]:8081'

dbconnection: 'mongodb://localhost:27017/admin'
56 changes: 47 additions & 9 deletions configs/static_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,48 @@ components_manager:
fs-task-processor: # Make a separate task processor for filesystem bound tasks.
worker_threads: $worker-fs-threads

grpc-blocking-task-processor:
worker_threads: $worker-grpc-threads
thread_name: grpc-worker

default_task_processor: main-task-processor

components: # Configuring components that were registered via component_list
# Settings common to all gRPC client factories
grpc-client-common:
# The TaskProcessor for blocking connection initiation
blocking-task-processor: grpc-blocking-task-processor

grpc-client-factory:
# Optional channel parameters for gRPC Core
# https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
channel-args: {}
middlewares:
- grpc-client-logging
- grpc-client-deadline-propagation

grpc-client-logging:
grpc-client-deadline-propagation:

handler-hello:

hello-client:
endpoint: $hello-endpoint

grpc-server:
port: 8081
service-defaults:
middlewares:
- grpc-server-logging
- grpc-server-deadline-propagation
- grpc-server-congestion-control
task-processor: main-task-processor

congestion-control:
grpc-server-logging:
grpc-server-deadline-propagation:
grpc-server-congestion-control:

server:
listener: # configuring the main listening socket...
port: $server-port # ...to listen on this port and...
Expand All @@ -32,25 +71,24 @@ components_manager:
testsuite-support: {}

http-client:
load-enabled: $is_testing
fs-task-processor: fs-task-processor

dns-client:
load-enabled: $is-testing
fs-task-processor: fs-task-processor

tests-control:
load-enabled: $is_testing
load-enabled: $is-testing
path: /tests/{action}
method: POST
task_processor: main-task-processor

handler-ping:
path: /ping
method: GET
task_processor: main-task-processor
throttling_enabled: false
url_trailing_slash: strict-match

handler-hello: # 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
mongo-db-1:
dbconnection: $dbconnection

dns-client:
fs-task-processor: fs-task-processor
37 changes: 32 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
version: "2.3"

services:
service_template-container:
mongo:
container_name: service-mongo
image: mongo
ports:
- 27017
networks:
- mongo

mongo_grpc_service_template-container:
image: ghcr.io/userver-framework/ubuntu-22.04-userver-pg:latest
privileged: true
network_mode: bridge
environment:
- PREFIX=${PREFIX:-~/.local}
- CCACHE_DIR=/service_template/.ccache
- CCACHE_DIR=/mongo_grpc_service_template/.ccache
- CORES_DIR=/cores
volumes:
- .:/service_template:rw
- .:/mongo_grpc_service_template:rw
ports:
- 8080:8080
working_dir: /service_template
- 8081:8081
working_dir: /mongo_grpc_service_template
entrypoint:
- ./tests/run_as_user.sh
depends_on:
- mongo
networks:
- mongo
- dockerbridge

networks:
mongo:
driver: bridge
dockerbridge:
enable_ipv6: true
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "true"
ipam:
driver: default
config:
- subnet: 2001:3984:3989::/64
gateway: 2001:3984:3989::1
15 changes: 15 additions & 0 deletions proto/handlers/hello.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package handlers.api;

service HelloService {
rpc SayHello(HelloRequest) returns(HelloResponse) {}
}

message HelloRequest {
string name = 1;
}

message HelloResponse {
string text = 1;
}
Loading