Skip to content

Commit 9f027f1

Browse files
authored
feat all: add mongo and grpc (#1)
Relates: userver-framework/userver#551 Co-authored-by: Alexander Kuvaev <vinatorul@yandex-team.ru>
1 parent a346393 commit 9f027f1

23 files changed

+468
-87
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ jobs:
4949
sudo apt install --allow-downgrades -y $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
5050
python3 -m pip install -r requirements.txt
5151
52+
- name: Install mongo
53+
run: |
54+
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
55+
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" \
56+
| sudo tee -a /etc/apt/sources.list.d/mongodb-org-7.0.list
57+
sudo apt update
58+
sudo apt install mongodb-org
59+
sudo apt install mongodb-mongosh
60+
5261
- name: Setup ccache
5362
run: |
5463
ccache -M 2.0GB
@@ -67,17 +76,17 @@ jobs:
6776
- name: Test run after install
6877
if: matrix.make == 'test-release'
6978
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
79+
./local_installation/bin/mongo_grpc_service_template
80+
--config=./local_installation/etc/mongo_grpc_service_template/static_config.yaml
81+
--config_vars=./local_installation/etc/mongo_grpc_service_template/config_vars.yaml
7382
&
7483
7584
- name: Check work run service
7685
if: matrix.make == 'test-release'
7786
run: |
78-
ps aux | grep service_template | grep config && curl http://localhost:8080/ping -v
87+
ps aux | grep mongo_grpc_service_template | grep config && curl http://localhost:8080/ping -v
7988
8089
- name: Stop all
8190
if: matrix.make == 'test-release'
8291
run: |
83-
killall service_template
92+
killall mongo_grpc_service_template

.github/workflows/docker.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ name: Docker build
1212

1313
jobs:
1414
tests:
15+
if: false
1516
runs-on: ubuntu-latest
1617
steps:
1718
- uses: actions/checkout@v4
@@ -33,7 +34,7 @@ jobs:
3334
sudo apt install --allow-downgrades -y docker-compose
3435
3536
- name: Setup ccache
36-
run: docker-compose run --rm service_template-container bash -c 'ccache -M 2.0GB && ccache -s'
37+
run: docker-compose run --rm mongo_grpc_service_template-container bash -c 'ccache -M 2.0GB && ccache -s'
3738

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

CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(service_template CXX)
2+
project(mongo_grpc_service_template CXX)
33

44

55
# Adding userver dependency
6-
find_package(userver COMPONENTS core postgresql QUIET)
6+
find_package(userver COMPONENTS core mongo grpc QUIET)
77
if(NOT userver_FOUND) # Fallback to subdirectory usage
8+
# Enable userver libraries that are needed in this project
9+
set(USERVER_FEATURE_MONGODB ON CACHE BOOL "" FORCE)
10+
set(USERVER_FEATURE_GRPC ON CACHE BOOL "" FORCE)
11+
812
# Compatibility mode: some systems don't support these features
913
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
1014
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
@@ -25,8 +29,14 @@ userver_setup_environment()
2529
add_library(${PROJECT_NAME}_objs OBJECT
2630
src/hello.hpp
2731
src/hello.cpp
32+
src/hello_client.hpp
33+
src/hello_client.cpp
2834
)
29-
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core)
35+
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::mongo userver::grpc)
36+
37+
# Create a proto library with userver extensions
38+
userver_add_grpc_library(${PROJECT_NAME}_proto PROTOS handlers/hello.proto)
39+
target_link_libraries(${PROJECT_NAME}_objs PUBLIC ${PROJECT_NAME}_proto)
3040

3141

3242
# The Service

Makefile

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,20 @@ build_release/CMakeCache.txt: cmake-release
2929
# Build using cmake
3030
.PHONY: build-debug build-release
3131
build-debug build-release: build-%: build_%/CMakeCache.txt
32-
cmake --build build_$* -j $(NPROCS) --target service_template
32+
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_service_template
3333

3434
# Test
3535
.PHONY: test-debug test-release
3636
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
37+
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_service_template_unittest
38+
cmake --build build_$* -j $(NPROCS) --target mongo_grpc_service_template_benchmark
3939
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
4040
pycodestyle tests
4141

4242
# Start the service (via testsuite service runner)
4343
.PHONY: start-debug start-release
4444
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-%
45+
cmake --build build_$* -v --target=start-mongo_grpc_service_template
4946

5047
# Cleanup data
5148
.PHONY: clean-debug clean-release
@@ -61,7 +58,7 @@ dist-clean:
6158
# Install
6259
.PHONY: install-debug install-release
6360
install-debug install-release: install-%: build-%
64-
cmake --install build_$* -v --component service_template
61+
cmake --install build_$* -v --component mongo_grpc_service_template
6562

6663
.PHONY: install
6764
install: install-release
@@ -75,22 +72,19 @@ format:
7572
# Internal hidden targets that are used only in docker environment
7673
.PHONY: --in-docker-start-debug --in-docker-start-release
7774
--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
75+
/home/user/.local/bin/mongo_grpc_service_template \
76+
--config /home/user/.local/etc/mongo_grpc_service_template/static_config.yaml \
77+
--config_vars /home/user/.local/etc/mongo_grpc_service_template/config_vars.yaml
8178

8279
# Build and run service in docker environment
8380
.PHONY: docker-start-debug docker-start-release
8481
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-%
82+
$(DOCKER_COMPOSE) run -p 8080:8080 -p 8081:8081 --rm mongo_grpc_service_template-container make -- --in-docker-start-$*
8983

9084
# Start specific target in docker environment
9185
.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
9286
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 $*
87+
$(DOCKER_COMPOSE) run --rm mongo_grpc_service_template-container make $*
9488

9589
# Stop docker container and cleanup data
9690
.PHONY: docker-clean-data

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# service_template
1+
# mongo_grpc_service_template
22

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

55

66
## Download and Build
@@ -9,7 +9,7 @@ To create your own userver-based service follow the following steps:
99

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

1515

configs/config_vars.docker.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
worker-threads: 4
2+
worker-fs-threads: 2
3+
worker-grpc-threads: 2
4+
logger-level: debug
5+
6+
is_testing: false
7+
8+
server-port: 8080
9+
server-grpc-port: 8081
10+
11+
hello-endpoint: '[::1]:8081'
12+
13+
dbconnection: 'mongodb://localhost:27017/admin'

configs/config_vars.testing.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
worker-threads: 4
22
worker-fs-threads: 2
3+
worker-grpc-threads: 2
34
logger-level: debug
45

56
is_testing: true
67

78
server-port: 8080
9+
server-grpc-port: 8081
10+
11+
hello-endpoint: '[::1]:8081'
12+
13+
dbconnection: 'mongodb://localhost:27217/admin'

configs/config_vars.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
worker-threads: 4
22
worker-fs-threads: 2
3+
worker-grpc-threads: 2
34
logger-level: info
45

56
is_testing: false
67

78
server-port: 8080
9+
server-grpc-port: 8081
10+
11+
hello-endpoint: '[::1]:8081'
12+
13+
dbconnection: 'mongodb://localhost:27017/admin'

configs/static_config.yaml

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,48 @@ components_manager:
77
fs-task-processor: # Make a separate task processor for filesystem bound tasks.
88
worker_threads: $worker-fs-threads
99

10+
grpc-blocking-task-processor:
11+
worker_threads: $worker-grpc-threads
12+
thread_name: grpc-worker
13+
1014
default_task_processor: main-task-processor
1115

1216
components: # Configuring components that were registered via component_list
17+
# Settings common to all gRPC client factories
18+
grpc-client-common:
19+
# The TaskProcessor for blocking connection initiation
20+
blocking-task-processor: grpc-blocking-task-processor
21+
22+
grpc-client-factory:
23+
# Optional channel parameters for gRPC Core
24+
# https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
25+
channel-args: {}
26+
middlewares:
27+
- grpc-client-logging
28+
- grpc-client-deadline-propagation
29+
30+
grpc-client-logging:
31+
grpc-client-deadline-propagation:
32+
33+
handler-hello:
34+
35+
hello-client:
36+
endpoint: $hello-endpoint
37+
38+
grpc-server:
39+
port: 8081
40+
service-defaults:
41+
middlewares:
42+
- grpc-server-logging
43+
- grpc-server-deadline-propagation
44+
- grpc-server-congestion-control
45+
task-processor: main-task-processor
46+
47+
congestion-control:
48+
grpc-server-logging:
49+
grpc-server-deadline-propagation:
50+
grpc-server-congestion-control:
51+
1352
server:
1453
listener: # configuring the main listening socket...
1554
port: $server-port # ...to listen on this port and...
@@ -32,25 +71,24 @@ components_manager:
3271
testsuite-support: {}
3372

3473
http-client:
35-
load-enabled: $is_testing
36-
fs-task-processor: fs-task-processor
37-
38-
dns-client:
74+
load-enabled: $is-testing
3975
fs-task-processor: fs-task-processor
4076

4177
tests-control:
42-
load-enabled: $is_testing
78+
load-enabled: $is-testing
4379
path: /tests/{action}
4480
method: POST
4581
task_processor: main-task-processor
82+
4683
handler-ping:
4784
path: /ping
4885
method: GET
4986
task_processor: main-task-processor
5087
throttling_enabled: false
5188
url_trailing_slash: strict-match
5289

53-
handler-hello: # Finally! Our handler.
54-
path: /hello # Registering handler by URL '/hello'.
55-
method: GET,POST # It will only reply to GET (HEAD) and POST requests.
56-
task_processor: main-task-processor # Run it on CPU bound task processor
90+
mongo-db-1:
91+
dbconnection: $dbconnection
92+
93+
dns-client:
94+
fs-task-processor: fs-task-processor

docker-compose.yml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
version: "2.3"
22

33
services:
4-
service_template-container:
4+
mongo:
5+
container_name: service-mongo
6+
image: mongo
7+
ports:
8+
- 27017
9+
networks:
10+
- mongo
11+
12+
mongo_grpc_service_template-container:
513
image: ghcr.io/userver-framework/ubuntu-22.04-userver-pg:latest
614
privileged: true
7-
network_mode: bridge
815
environment:
916
- PREFIX=${PREFIX:-~/.local}
10-
- CCACHE_DIR=/service_template/.ccache
17+
- CCACHE_DIR=/mongo_grpc_service_template/.ccache
1118
- CORES_DIR=/cores
1219
volumes:
13-
- .:/service_template:rw
20+
- .:/mongo_grpc_service_template:rw
1421
ports:
1522
- 8080:8080
16-
working_dir: /service_template
23+
- 8081:8081
24+
working_dir: /mongo_grpc_service_template
1725
entrypoint:
1826
- ./tests/run_as_user.sh
27+
depends_on:
28+
- mongo
29+
networks:
30+
- mongo
31+
- dockerbridge
32+
33+
networks:
34+
mongo:
35+
driver: bridge
36+
dockerbridge:
37+
enable_ipv6: true
38+
driver: bridge
39+
driver_opts:
40+
com.docker.network.enable_ipv6: "true"
41+
ipam:
42+
driver: default
43+
config:
44+
- subnet: 2001:3984:3989::/64
45+
gateway: 2001:3984:3989::1

0 commit comments

Comments
 (0)