Skip to content

Commit

Permalink
Merge branch 'jsonrpc'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard B committed Oct 18, 2021
2 parents 85dfe19 + 708e865 commit 6679a14
Show file tree
Hide file tree
Showing 24 changed files with 1,846 additions and 814 deletions.
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ body:
required: true
attributes:
label: In which mode are you using the docker container?
description: If you have `USE_NATIVE=1` set in your `docker-compose.yml` file then you are using the native mode.
description: Please have a look at the `MODE` parameter in you `docker-compose.yml` file. If you do not have the `MODE` parameter explicitly set, then you are running in normal mode.
options:
- Normal Mode
- Native Mode
- JSON-RPC Mode
- type: dropdown
validations:
required: true
Expand Down
19 changes: 13 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ARG SIGNAL_CLI_VERSION=0.8.5
ARG SIGNAL_CLI_VERSION=0.9.0
ARG ZKGROUP_VERSION=0.7.0
ARG LIBSIGNAL_CLIENT_VERSION=0.8.1
ARG LIBSIGNAL_CLIENT_VERSION=0.9.0

ARG SWAG_VERSION=1.6.7
ARG GRAALVM_JAVA_VERSION=11
ARG GRAALVM_VERSION=21.0.0
ARG GRAALVM_VERSION=21.2.0

FROM golang:1.14-buster AS buildcontainer
FROM golang:1.17-bullseye AS buildcontainer

ARG SIGNAL_CLI_VERSION
ARG ZKGROUP_VERSION
Expand Down Expand Up @@ -125,16 +125,22 @@ RUN cd /tmp/signal-cli-${SIGNAL_CLI_VERSION}/build/distributions/ \


COPY src/api /tmp/signal-cli-rest-api-src/api
COPY src/client /tmp/signal-cli-rest-api-src/client
COPY src/utils /tmp/signal-cli-rest-api-src/utils
COPY src/scripts /tmp/signal-cli-rest-api-src/scripts
COPY src/main.go /tmp/signal-cli-rest-api-src/
COPY src/go.mod /tmp/signal-cli-rest-api-src/
COPY src/go.sum /tmp/signal-cli-rest-api-src/

# build signal-cli-rest-api
RUN cd /tmp/signal-cli-rest-api-src && swag init && go build

# build supervisorctl_config_creator
RUN cd /tmp/signal-cli-rest-api-src/scripts && go build -o jsonrpc2-helper


# Start a fresh container for release container
FROM adoptopenjdk:11-jre-hotspot-bionic
FROM eclipse-temurin:11-jre-focal

ENV GIN_MODE=release

Expand All @@ -143,12 +149,13 @@ ENV PORT=8080
ARG SIGNAL_CLI_VERSION

RUN apt-get update \
&& apt-get install -y --no-install-recommends setpriv \
&& apt-get install -y --no-install-recommends util-linux supervisor netcat \
&& rm -rf /var/lib/apt/lists/*

COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api /usr/bin/signal-cli-rest-api
COPY --from=buildcontainer /tmp/signal-cli-${SIGNAL_CLI_VERSION}/build/distributions/signal-cli-${SIGNAL_CLI_VERSION}.tar /tmp/signal-cli-${SIGNAL_CLI_VERSION}.tar
COPY --from=buildcontainer /tmp/signal-cli-${SIGNAL_CLI_VERSION}/build/native-image/signal-cli /tmp/signal-cli-native
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/scripts/jsonrpc2-helper /usr/bin/jsonrpc2-helper
COPY entrypoint.sh /entrypoint.sh

RUN tar xf /tmp/signal-cli-${SIGNAL_CLI_VERSION}.tar -C /opt
Expand Down
53 changes: 36 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,41 @@ At the moment, the following functionality is exposed via REST:

and [many more](https://bbernhard.github.io/signal-cli-rest-api/)

## Examples
## Modes

The `signal-cli-rest-api` supports three different modes:

* Normal Mode: In normal mode, the `signal-cli` executable is invoked for every REST API request. As `signal-cli` is a Java application, a significant amount of time is spent in the JVM (Java Virtual Machine) startup - which makes this mode pretty slow.
* Native Mode: Instead of calling a Java executable for every REST API request, a native image (compiled with GraalVM) is called. This mode therefore usually performs better than the normal mode.
* JSON-RPC Mode: In JSON-RPC mode, a single `signal-cli` instance is spawned in daemon mode. The communication happens via JSON-RPC. This mode is usually the fastest.


| architecture | normal mode | native mode | json-rpc mode |
|--------------|:-----------:|:-----------:|---------------|
| x86-64 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| armv7 | :heavy_check_mark: | ❌ <sup>1</sup> | :heavy_check_mark: |
| arm64 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |


| mode | speed |
|-------------:|:------------|
| json-rpc | :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: |
| native | :heavy_check_mark: :heavy_check_mark: |
| normal | :heavy_check_mark: |


Notes:
1. If the signal-cli-rest-api docker container is started on an armv7 system in native mode, it automatically falls back to the normal mode.

## Auto Receive Schedule

> :warning: This setting is only needed in normal/native mode!
[signal-cli](https://github.com/AsamK/signal-cli), which this REST API wrapper is based on, recommends to call `receive` on a regular basis. So, if you are not already calling the `receive` endpoint regularily, it is recommended to set the `AUTO_RECEIVE_SCHEDULE` parameter in the docker-compose.yml file. The `AUTO_RECEIVE_SCHEDULE` accepts cron schedule expressions and automatically calls the `receive` endpoint at the given time. e.g: `0 22 * * *` calls `receive` daily at 10pm. If you are not familiar with cron schedule expressions, you can use this [website](https://crontab.guru).

**WARNING** Calling `receive` will fetch all the messages for the registered Signal number from the Signal Server! So, if you are using the REST API for receiving messages, it's _not_ a good idea to use the `AUTO_RECEIVE_SCHEDULE` parameter, as you might lose some messages that way.

## Example

Sample `docker-compose.yml`file:

Expand All @@ -25,7 +59,7 @@ services:
signal-cli-rest-api:
image: bbernhard/signal-cli-rest-api:latest
environment:
- USE_NATIVE=0
- MODE=normal #supported modes: json-rpc, native, normal
#- AUTO_RECEIVE_SCHEDULE=0 22 * * * #enable this parameter on demand (see description below)
ports:
- "8080:8080" #map docker port 8080 to host port 8080.
Expand All @@ -34,21 +68,6 @@ services:

```

## Auto Receive Schedule

[signal-cli](https://github.com/AsamK/signal-cli), which this REST API wrapper is based on, recommends to call `receive` on a regular basis. So, if you are not already calling the `receive` endpoint regularily, it is recommended to set the `AUTO_RECEIVE_SCHEDULE` parameter in the docker-compose.yml file. The `AUTO_RECEIVE_SCHEDULE` accepts cron schedule expressions and automatically calls the `receive` endpoint at the given time. e.g: `0 22 * * *` calls `receive` daily at 10pm. If you are not familiar with cron schedule expressions, you can use this [website](https://crontab.guru).

**WARNING** Calling `receive` will fetch all the messages for the registered Signal number from the Signal Server! So, if you are using the REST API for receiving messages, it's _not_ a good idea to use the `AUTO_RECEIVE_SCHEDULE` parameter, as you might lose some messages that way.


## Native Image (EXPERIMENTAL)

On Systems like the Raspberry Pi, some operations like sending messages can take quite a while. That's because signal-cli is a Java application and a significant amount of time is spent in the JVM (Java Virtual Machine) startup. signal-cli recently added the possibility to compile the Java application to a native binary (done via GraalVM).

By adding `USE_NATIVE=1` as environmental variable to the `docker-compose.yml` file the native mode will be enabled.

In case there's no native binary available (e.g on a 32 bit Raspian OS), it will fall back to the signal-cli Java application. The native mode only works on a 64bit system, when the native mode is enabled on a 32bit system, it falls back to the Java application.

## Documentation

### API Reference
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ services:
signal-cli-rest-api:
image: bbernhard/signal-cli-rest-api:latest
environment:
- USE_NATIVE=0
#- AUTO_RECEIVE_SCHEDULE=0 22 * * *
- PORT=8080
- MODE=normal #supported modes: json-rpc, native, normal
#- AUTO_RECEIVE_SCHEDULE=0 22 * * * #enable this parameter on demand (see description below)
ports:
- "8080:8080" #map docker port 8080 to host port 8080.
volumes:
Expand Down
8 changes: 8 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ EOF
cap_prefix="-cap_"
caps="$cap_prefix$(seq -s ",$cap_prefix" 0 $(cat /proc/sys/kernel/cap_last_cap))"

# TODO: check mode
if [ "$MODE" = "json-rpc" ]
then
/usr/bin/jsonrpc2-helper
service supervisor start
supervisorctl start all
fi

# Start API as signal-api user
exec setpriv --reuid=1000 --regid=1000 --init-groups --inh-caps=$caps signal-cli-rest-api -signal-cli-config=${SIGNAL_CLI_CONFIG_DIR}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 6679a14

Please sign in to comment.