Skip to content

Commit ea0313e

Browse files
Merge pull request #6 from toncenter/develop
Improvements and fixes
2 parents 1f93e78 + 50f8a9e commit ea0313e

File tree

11 files changed

+323
-103
lines changed

11 files changed

+323
-103
lines changed

Dockerfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ COPY playground/ /app/playground/
2727
COPY CMakeLists.txt /app/CMakeLists.txt
2828
COPY external/ /app/external/
2929

30-
ARG TON_REPO
31-
ARG TON_BRANCH
32-
RUN if [ -n "$TON_REPO" ]; then \
33-
echo "Using ton from ${TON_REPO}:${TON_BRANCH:-master}"; \
30+
ARG BUILD_WITH_TON_REPO
31+
ARG BUILD_WITH_TON_REPO
32+
RUN if [ -n "${BUILD_WITH_TON_REPO}" ]; then \
33+
echo "Using ton from ${BUILD_WITH_TON_REPO}:${BUILD_WITH_TON_REPO:-master}"; \
3434
rm -rf /app/external/ton/ && \
35-
git clone --recursive --branch ${TON_BRANCH:-master} ${TON_REPO} /app/external/ton; \
35+
git clone --recursive --branch ${BUILD_WITH_TON_REPO:-master} ${BUILD_WITH_TON_REPO} /app/external/ton; \
3636
else \
3737
echo "Using ton submodule"; \
3838
fi
3939

4040
WORKDIR /app/build
4141
RUN cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=1 .. && make -j$(nproc) && make install
4242
RUN mkdir -p /root/.config/gdb && echo "set auto-load safe-path /" > /root/.config/gdb/gdbinit
43-
COPY ton-http-api/static/ /app/static/
43+
COPY ton-http-api/static/ /static/
4444
COPY config/static_config.yaml /app/static_config.yaml
4545
ENTRYPOINT [ "ton-http-api-cpp" ]
4646
# end builder
@@ -51,7 +51,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt update -y \
5151
&& apt install -y curl dnsutils libcurl4 libfmt9 libsodium23 libcctz2 libatomic1 libicu74 \
5252
&& rm -rf /var/lib/apt/lists/*
5353
COPY --from=builder /app/build/ton-http-api/ton-http-api-cpp /usr/bin/
54-
COPY --from=builder /app/build/tonlib-multiclient/libtonlib_multiclient_lib.so /usr/lib
55-
COPY ton-http-api/static/ /app/static/
54+
COPY ton-http-api/static/ /static/
5655
COPY config/static_config.yaml /app/static_config.yaml
57-
ENTRYPOINT [ "ton-http-api-cpp" ]
56+
COPY scripts/entrypoint.sh /app/entrypoint.sh
57+
ENTRYPOINT [ "/app/entrypoint.sh" ]

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ There are two main ways to run TON HTTP API:
5656
```
5757
- To run testnet version or connect to your private TON node, please specify:
5858
```
59-
export THACPP_GLOBAL_CONFIG_PATH=private/testnet.json
59+
export THACPP_TONLIB_CONFIG_PATH=private/testnet.json
6060
# or
61-
echo "THACPP_GLOBAL_CONFIG_PATH=private/testnet.json" > .env
61+
echo "THACPP_TONLIB_CONFIG_PATH=private/testnet.json" > .env
6262
```
6363
- To build an image using custom TON monorepo:
6464
```
65-
export TON_REPO=https://github.com/ton-blockchain/ton
66-
export TON_BRANCH=testnet
65+
export BUILD_WITH_TON_REPO=https://github.com/ton-blockchain/ton
66+
export BUILD_WITH_TON_BRANCH=testnet
6767
# or
68-
echo "TON_REPO=https://github.com/ton-blockchain/ton" >> .env
69-
export TON_BRANCH=testnet
68+
echo "BUILD_WITH_TON_REPO=https://github.com/ton-blockchain/ton" >> .env
69+
echo "BUILD_WITH_TON_BRANCH=testnet" >> .env
7070
```
7171
- Pull or build docker image:
7272
```bash
@@ -89,6 +89,34 @@ There are two main ways to run TON HTTP API:
8989
docker compose logs -f -n 100 http-api-cpp
9090
```
9191

92+
### Configure via environment variables
93+
Instead of using `config_vars.yaml` file, you can also configure service via environment variables.
94+
To do this you need to set environment variables before starting the service and use the following command:
95+
```bash
96+
docker compose -f docker-compose.env-vars.yaml up -d
97+
```
98+
99+
List of available environment variables:
100+
- `THACPP_TONLIB_CONFIG_PATH` - TON global config path (default: `/run/secrets/ton-global-config`)
101+
- `THACPP_TONLIB_KEYSTORE_PATH` - TONlib keystore path (default: `/tmp/keystore/`)
102+
- `THACPP_TONLIB_BOC_ENDPOINTS` - Endpoints to duplicate incoming BOCs; should be a list of comma-separated quoted strings like `["http://127.0.0.1:8080", "https://endpoint.com/postBoc"]` (default: `[]`)
103+
- `THACPP_TONLIB_THREADS` - number of threads for TONlib multiclient (default: `4`)
104+
- `THACPP_PORT` - API port in container (default: `8081`)
105+
- `THACPP_MONITOR_PORT` - Monitoring port in container (default: `8082`)
106+
- `THACPP_MAIN_WORKER_THREADS` - number of threads to serve HTTP requests (default: `4`)
107+
- `THACPP_FS_WORKER_THREADS` - number of threads for I/O operations (default: `1`)
108+
- `THACPP_HTTP_WORKER_THREADS` - number of threads for HTTP client (used to duplicate BOCs on external service) (default: `2`)
109+
- `THACPP_LOG_LEVEL` - api v2 log level (default: `warning`)
110+
- `THACPP_LOG_PATH` - log output destination (`@stdout`, `@stderr`, `@null`, or `/path/to/file`) (default: `"@stdout"`)
111+
- `THACPP_SYSTEM_LOG_LEVEL` - userver system logs level (default: `warning`)
112+
- `THACPP_SYSTEM_LOG_PATH` - userver system log path (default: `"@stdout"`)
113+
- `THACPP_JSONRPC_LOG_LEVEL` - jsonrpc endpoint log level (default: `warning`)
114+
- `THACPP_JSONRPC_LOG_PATH` - jsonrpc endpoint log path (default: `"@stdout"`)
115+
- `THACPP_LOG_FORMAT` - logs format (`tskv`, `ltsv`, `json`) (default: `json`)
116+
- `THACPP_HTTP_WORKER_USER_AGENT` - HTTP user agent sent to BOC endpoint (default: `empty`)
117+
- `THACPP_STATIC_CONTENT_DIR` - directory for static content served by API (default: `"/static/"`)
118+
- `THACPP_MAX_STACK_ENTRY_DEPTH` - max stack entry depth for runGetMethod (higher values increase memory usage) (default: `256`)
119+
92120
### Local run
93121

94122
- Install dependencies:

config/config_vars.yaml

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,83 @@
1-
# TONlib parameters
2-
tonlib_config_path: /run/secrets/ton-global-config # TON global config path
3-
tonlib_keystore_path: /tmp/keystore/ # TONlib keystore path
4-
tonlib_boc_endpoints: [] # Endpoints to duplicate incoming BOCs
5-
tonlib_threads: 4 # number of threads for TONlib multiclient
6-
7-
server_port: 8081 # API port in container,
8-
# to change exposed port set THACPP_PORT env variable
9-
monitor_port: 8082 # Monitoring port in container,
10-
# to change exposed port set THACPP_MONITOR_PORT env variable
11-
12-
main_worker_threads: 4 # number of threads to serve HTTP requests
13-
fs_worker_threads: 1 # number of threads for I/O operations, f.e. logging
14-
http_worker_threads: 2 # number of threads for http client
15-
# to duplicate BOCs on external service
16-
17-
log_level: warning # api v2 log level
18-
log_path: "@stdout" # log destination, available options:
19-
# @stdout
20-
# @stderr
21-
# @null - don't log
22-
# /path/to/file - log to file
23-
# Note: if you log into file,
24-
# logs will be deleted after deploying new container
25-
system_log_level: warning # userver system logs
1+
##### TONlib parameters #####
2+
# TON global config path
3+
# THACPP_TONLIB_CONFIG_PATH
4+
tonlib_config_path: /run/secrets/ton-global-config
5+
# TONlib keystore path
6+
# THACPP_TONLIB_KEYSTORE_PATH
7+
tonlib_keystore_path: /tmp/keystore/
8+
# Endpoints to duplicate incoming BOCs
9+
# should be a list of comma-separated, quoted strings in format `http://host:port`
10+
# example: ["http://127.0.0.1:8080", "https://endpoint.com/postBoc"]
11+
# service is expected to accept POST requests with BOC in body and return 200 in case of success
12+
# request body will have the following format: `{"boc":"base64 encoded boc"}`
13+
# THACPP_TONLIB_BOC_ENDPOINTS
14+
tonlib_boc_endpoints: []
15+
# number of threads for TONlib multiclient
16+
# THACPP_TONLIB_THREADS
17+
tonlib_threads: 4
18+
19+
##### Userver parameters #####
20+
# API port in container
21+
# THACPP_PORT
22+
server_port: 8081
23+
# Monitoring port in container
24+
# THACPP_MONITOR_PORT
25+
monitor_port: 8082
26+
27+
# number of threads to serve HTTP requests
28+
# THACPP_MAIN_WORKER_THREADS
29+
main_worker_threads: 4
30+
# number of threads for I/O operations, f.e. logging
31+
# THACPP_FS_WORKER_THREADS
32+
fs_worker_threads: 1
33+
# number of threads for http client
34+
# to duplicate BOCs on external service
35+
# THACPP_HTTP_WORKER_THREADS
36+
http_worker_threads: 2
37+
38+
##### Logging parameters #####
39+
# for log_path destination use one available options:
40+
# @stdout
41+
# @stderr
42+
# @null - don't log
43+
# /path/to/file - log to file
44+
# Note: if you log into file,
45+
# logs will be deleted after deploying new container
46+
47+
# api v2 log level
48+
# THACPP_LOG_LEVEL
49+
log_level: warning
50+
# THACPP_LOG_PATH
51+
log_path: "@stdout"
52+
53+
# userver system logs
54+
# THACPP_SYSTEM_LOG_LEVEL
55+
system_log_level: warning
56+
# THACPP_SYSTEM_LOG_PATH
2657
system_log_path: "@stdout"
2758

28-
jsonrpc_log_level: warning # jsonrpc endpoint logs
29-
# note that requests to jsonRPC endpoint will appear in both of api-v2 and jsonrpc loggers
59+
# jsonrpc endpoint logs
60+
# note that requests to jsonRPC endpoint will appear in both of api-v2 and jsonrpc loggers
61+
# THACPP_JSONRPC_LOG_LEVEL
62+
jsonrpc_log_level: warning
63+
# THACPP_JSONRPC_LOG_PATH
3064
jsonrpc_log_path: "@stdout"
3165

32-
log_format: json # logs format, one of `tskv`, `ltsv`, `json`
66+
# logs format, one of `tskv`, `ltsv`, `json`
67+
# THACPP_LOG_FORMAT
68+
log_format: json
69+
70+
# http user agent to set in request to boc endpoint
71+
# this may be required for external services with anti-ddos and anti-bot protection
72+
# THACPP_HTTP_WORKER_USER_AGENT
73+
http_worker_user_agent: empty
3374

34-
http_worker_user_agent: empty # http user agent to set in request to boc endpoint
75+
# directory for static content
76+
# static content will be served from this directory
77+
# THACPP_STATIC_CONTENT_DIR
78+
static_content_dir: /static/
3579

36-
static_content_dir: "/static/" # directory for static content
37-
max_stack_entry_depth: 100 # max stack entry depth for runGetMethod
80+
# max stack entry depth for runGetMethod
81+
# increasing this value may increase memory consumption in runGetMethods
82+
# THACPP_MAX_STACK_ENTRY_DEPTH
83+
max_stack_entry_depth: 256

config/static_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ components_manager:
33
initial_size: 1000
44
max_size: 10000
55
stack_size: 4194304
6-
stack_usage_monitor_enabled: true
6+
stack_usage_monitor_enabled: false
77
event_thread_pool:
88
threads: 2
99
task_processors:

docker-compose.env-vars.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
services:
2+
http-api-cpp:
3+
image: toncenter/ton-http-api-cpp:latest
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
target: http-api-cpp
8+
args:
9+
- BUILD_WITH_TON_REPO
10+
- BUILD_WITH_TON_BRANCH
11+
ports:
12+
- ${THACPP_PORT:-8081}:8081
13+
- ${THACPP_MONITOR_PORT:-8082}:8082
14+
environment:
15+
- THACPP_TONLIB_CONFIG_PATH
16+
- THACPP_TONLIB_KEYSTORE_PATH
17+
- THACPP_TONLIB_BOC_ENDPOINTS
18+
- THACPP_TONLIB_THREADS
19+
- THACPP_MAIN_WORKER_THREADS
20+
- THACPP_FS_WORKER_THREADS
21+
- THACPP_HTTP_WORKER_THREADS
22+
- THACPP_LOG_LEVEL
23+
- THACPP_LOG_PATH
24+
- THACPP_SYSTEM_LOG_LEVEL
25+
- THACPP_SYSTEM_LOG_PATH
26+
- THACPP_JSONRPC_LOG_LEVEL
27+
- THACPP_JSONRPC_LOG_PATH
28+
- THACPP_LOG_FORMAT
29+
- THACPP_HTTP_WORKER_USER_AGENT
30+
- THACPP_STATIC_CONTENT_DIR
31+
- THACPP_MAX_STACK_ENTRY_DEPTH
32+
restart: unless-stopped
33+
# secrets:
34+
# - ton-global-config
35+
ulimits:
36+
memlock: -1
37+
stack: -1
38+
core: -1
39+
#secrets:
40+
# ton-global-config:
41+
# file: ${THACPP_GLOBAL_CONFIG_PATH:-private/mainnet.json}

docker-compose.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ services:
66
dockerfile: Dockerfile
77
target: http-api-cpp
88
args:
9-
- TON_REPO
10-
- TON_BRANCH
11-
command: --config /app/static_config.yaml --config_vars /run/secrets/config-vars
9+
- BUILD_WITH_TON_REPO
10+
- BUILD_WITH_TON_BRANCH
1211
ports:
1312
- ${THACPP_PORT:-8081}:8081
1413
- ${THACPP_MONITOR_PORT:-8082}:8082
14+
environment:
15+
- THACPP_CONFIG_VARS=/run/secrets/config-vars
16+
- THACPP_TONLIB_CONFIG_PATH=/run/secrets/ton-global-config
1517
restart: unless-stopped
1618
secrets:
1719
- ton-global-config
@@ -22,6 +24,6 @@ services:
2224
core: -1
2325
secrets:
2426
ton-global-config:
25-
file: ${THACPP_GLOBAL_CONFIG_PATH:-private/mainnet.json}
27+
file: ${THACPP_TONLIB_CONFIG_PATH:-private/mainnet.json}
2628
config-vars:
2729
file: ${THACPP_CONFIG_VARS:-private/config_vars.yaml}

scripts/entrypoint.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# config path
5+
THACPP_STATIC_CONFIG_PATH=${THACPP_STATIC_CONFIG_PATH:-/app/static_config.yaml}
6+
if [[ ! -f "$THACPP_STATIC_CONFIG_PATH" ]]; then
7+
echo "Static config file '$THACPP_STATIC_CONFIG_PATH' not found}"
8+
exit 1
9+
fi
10+
PARAMS="--config $THACPP_STATIC_CONFIG_PATH"
11+
12+
# config vars
13+
USE_ENV_VARS=${USE_ENV_VARS:-0}
14+
THACPP_CONFIG_VARS=${THACPP_CONFIG_VARS:-/app/config_vars.yaml}
15+
if [[ ! -f "$THACPP_CONFIG_VARS" ]]; then
16+
echo "Config vars file path not found. Config will be rendered from env vars"
17+
USE_ENV_VARS=1
18+
fi
19+
PARAMS="$PARAMS --config_vars $THACPP_CONFIG_VARS"
20+
21+
if [[ "$USE_ENV_VARS" -eq 1 ]]; then
22+
echo "Rendering config from env vars"
23+
if [[ "$THACPP_TONLIB_CONFIG_PATH" == "http://"* || "$THACPP_TONLIB_CONFIG_PATH" == "https://"* ]]; then
24+
echo "Downloading TON network config from $THACPP_TONLIB_CONFIG_PATH"
25+
curl -L --fail -o "/app/ton-config" "$THACPP_TONLIB_CONFIG_PATH"
26+
THACPP_TONLIB_CONFIG_PATH=/app/ton-config
27+
fi
28+
cat <<EOF > /app/config_vars.yaml
29+
tonlib_config_path: ${THACPP_TONLIB_CONFIG_PATH:-/run/secrets/ton-global-config}
30+
tonlib_keystore_path: ${THACPP_TONLIB_KEYSTORE_PATH:-/tmp/keystore/}
31+
tonlib_boc_endpoints: ${THACPP_TONLIB_BOC_ENDPOINTS:-[]}
32+
tonlib_threads: ${THACPP_TONLIB_THREADS:-4}
33+
34+
server_port: 8081
35+
monitor_port: 8082
36+
37+
main_worker_threads: ${THACPP_MAIN_WORKER_THREADS:-4}
38+
fs_worker_threads: ${THACPP_FS_WORKER_THREADS:-1}
39+
http_worker_threads: ${THACPP_HTTP_WORKER_THREADS:-2}
40+
41+
log_level: ${THACPP_LOG_LEVEL:-warning}
42+
log_path: "${THACPP_LOG_PATH:-@stdout}"
43+
44+
system_log_level: ${THACPP_SYSTEM_LOG_LEVEL:-warning}
45+
system_log_path: "${THACPP_SYSTEM_LOG_PATH:-@stdout}"
46+
47+
jsonrpc_log_level: ${THACPP_JSONRPC_LOG_LEVEL:-warning}
48+
jsonrpc_log_path: "${THACPP_JSONRPC_LOG_PATH:-@stdout}"
49+
50+
log_format: ${THACPP_LOG_FORMAT:-json}
51+
52+
http_worker_user_agent: ${THACPP_HTTP_WORKER_USER_AGENT:-empty}
53+
54+
static_content_dir: ${THACPP_STATIC_CONTENT_DIR:-/static/}
55+
56+
max_stack_entry_depth: ${THACPP_MAX_STACK_ENTRY_DEPTH:-256}
57+
EOF
58+
echo "Config:"
59+
cat "$THACPP_CONFIG_VARS"
60+
fi
61+
62+
# params ready
63+
echo "params: $PARAMS"
64+
65+
# check global config
66+
TONLIB_CONFIG_PATH=$(grep '^tonlib_config_path:' "$THACPP_CONFIG_VARS" | awk '{print $2}')
67+
68+
if [[ ! -f "$TONLIB_CONFIG_PATH" ]]; then
69+
echo "TON global config '$TONLIB_CONFIG_PATH' not found"
70+
exit 1
71+
fi
72+
73+
# create keystore path
74+
TONLIB_KEYSTORE_PATH=$(grep '^tonlib_keystore_path:' "$THACPP_CONFIG_VARS" | awk '{print $2}')
75+
mkdir -p "$TONLIB_KEYSTORE_PATH"
76+
77+
# check static file dir exists
78+
STATIC_FILES_PATH=$(grep '^static_content_dir:' "$THACPP_CONFIG_VARS" | awk '{print $2}')
79+
if [[ ! -d "$STATIC_FILES_PATH" ]]; then
80+
echo "Config with WWW content not found: '$STATIC_FILES_PATH'"
81+
exit 1
82+
fi
83+
84+
exec ton-http-api-cpp $PARAMS "$@"

0 commit comments

Comments
 (0)