Skip to content

Commit 8950c80

Browse files
authored
Merge pull request #38 from zonblade/feature/aarm
add arm support
2 parents 5ab927c + f6c9ced commit 8950c80

File tree

13 files changed

+546
-337
lines changed

13 files changed

+546
-337
lines changed

.cargo/config.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[target.aarch64-unknown-linux-gnu]
2+
linker = "aarch64-linux-gnu-gcc"
3+
4+
[env]
5+
CC_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-gcc"
6+
CXX_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-g++"
7+
AR_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-ar"
8+
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = "aarch64-linux-gnu-gcc"
9+
10+
# Use vendored OpenSSL to avoid cross-compilation issues
11+
OPENSSL_STATIC = "true"
12+
OPENSSL_VENDORED = "true"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ panic = "abort" # Remove panic unwinding code
3030
strip = true # Strip symbols from binary
3131
debug = false # No debug symbols
3232

33+
3334
# Use faster memory allocator
3435
[profile.release.package."*"]
3536
codegen-units = 1

build-docker/alpine/Dockerfile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22
FROM rust:alpine AS builder
33
WORKDIR /app
44

5-
# Install build dependencies
5+
# Better build environment for Rust + musl
66
RUN apk add --no-cache \
77
build-base \
88
cmake \
9-
musl-dev\
9+
musl-dev \
10+
pkgconfig \
1011
perl \
11-
perl-utils \
12-
perl-dev \
1312
sqlite-dev \
14-
openssl-dev
13+
openssl-dev \
14+
openssl-libs-static
15+
16+
# Optimize Rust compilation for musl
17+
ENV RUSTFLAGS="-C target-feature=-crt-static -C link-arg=-s"
18+
ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"
19+
RUN rustup target add x86_64-unknown-linux-musl
1520

1621
COPY . .
17-
RUN cargo build -p router-cli --release
18-
RUN cargo build -p router-core --release
19-
RUN cargo build -p router-api --release
22+
RUN cargo build -p router-cli --release --target x86_64-unknown-linux-musl
23+
RUN cargo build -p router-core --release --target x86_64-unknown-linux-musl
24+
RUN cargo build -p router-api --release --target x86_64-unknown-linux-musl
2025

2126
# Runtime image stage
2227
FROM alpine:latest

build-docker/debian/Dockerfile

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
# Build stage
22
FROM rust:bookworm AS builder
33
WORKDIR /app
4+
45
# Install build dependencies
56
RUN apt-get update && apt-get install -y \
7+
build-essential \
68
cmake \
9+
perl \
10+
libperl-dev \
11+
libsqlite3-dev \
12+
libssl-dev \
13+
pkg-config \
714
&& rm -rf /var/lib/apt/lists/*
815

916
COPY . .
17+
RUN cargo build -p router-cli --release
1018
RUN cargo build -p router-core --release
1119
RUN cargo build -p router-api --release
1220

1321
# Runtime image stage
1422
FROM debian:bookworm-slim
1523
WORKDIR /app
1624

17-
# Install systemd and other dependencies
25+
# Install minimal runtime dependencies
1826
RUN apt-get update && apt-get install -y \
19-
systemd \
20-
systemd-sysv \
2127
ca-certificates \
28+
bash \
29+
libsqlite3-0 \
30+
libssl3 \
2231
procps \
32+
util-linux \
33+
coreutils \
2334
&& rm -rf /var/lib/apt/lists/*
2435

2536
# Copy binaries from builder
2637
COPY --from=builder /app/target/release/router-core /usr/local/bin/router-core
2738
COPY --from=builder /app/target/release/router-api /usr/local/bin/router-api
39+
COPY --from=builder /app/target/release/router-cli /usr/local/bin/gwrs
2840

29-
# Create necessary directories
30-
RUN mkdir -p /opt/gwrs/bin && \
31-
mkdir -p /opt/gwrs/conf && \
32-
mkdir -p /tmp/gwrs/log
33-
34-
# Create symlinks to binaries
35-
RUN ln -sf /usr/local/bin/router-core /opt/gwrs/bin/router-core && \
36-
ln -sf /usr/local/bin/router-api /opt/gwrs/bin/router-api
37-
38-
# Create systemd service files
39-
RUN mkdir -p /etc/systemd/system
40-
COPY build-docker/debian/router-core.service /etc/systemd/system/gwrs-core.service
41-
COPY build-docker/debian/router-api.service /etc/systemd/system/gwrs-api.service
42-
43-
# Enable services
44-
RUN systemctl enable gwrs-core.service
45-
RUN systemctl enable gwrs-api.service
46-
47-
# Set up entrypoint script
48-
COPY build-docker/debian/entrypoint.sh /entrypoint.sh
49-
RUN chmod +x /entrypoint.sh
41+
COPY build-docker/debian/entrypoint.sh /usr/local/bin/entrypoint.sh
5042

51-
# Use systemd as command, with a fallback path
52-
ENTRYPOINT ["/entrypoint.sh"]
53-
CMD ["/lib/systemd/systemd"]
43+
# Make everything executable
44+
RUN chmod +x /usr/local/bin/*
5445

55-
# Expose API
56-
EXPOSE 24042
46+
# Set entrypoint
47+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

build-docker/debian/entrypoint.sh

Lines changed: 129 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,133 @@
11
#!/bin/bash
22
set -e
33

4-
# Prepare systemd for container environment
5-
if [ ! -d /run/systemd/system ]; then
6-
mkdir -p /run/systemd/system
7-
fi
8-
9-
# Check for the correct init path
10-
INIT_PATH=""
11-
for path in /sbin/init /lib/systemd/systemd /usr/lib/systemd/systemd /bin/systemd; do
12-
if [ -x "$path" ]; then
13-
INIT_PATH="$path"
14-
break
4+
# Simple configuration
5+
LOG_DIR="/tmp/gwrs/log"
6+
PID_DIR="/tmp/gwrs/pids"
7+
CHECK_INTERVAL=5
8+
9+
# Create directories
10+
mkdir -p "$LOG_DIR" "$PID_DIR"
11+
12+
# Logging
13+
log() {
14+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_DIR/manager.log"
15+
}
16+
17+
# Start core service
18+
start_core() {
19+
log "Starting router-core..."
20+
nohup /usr/local/bin/router-core > "$LOG_DIR/core.log" 2> "$LOG_DIR/core.error" &
21+
echo $! > "$PID_DIR/core.pid"
22+
log "router-core started (PID: $!)"
23+
}
24+
25+
# Start API service
26+
start_api() {
27+
log "Starting router-api..."
28+
nohup /usr/local/bin/router-api > "$LOG_DIR/api.log" 2> "$LOG_DIR/api.error" &
29+
echo $! > "$PID_DIR/api.pid"
30+
log "router-api started (PID: $!)"
31+
}
32+
33+
# Stop a service
34+
stop_service() {
35+
local service=$1
36+
local pid_file="$PID_DIR/${service}.pid"
37+
38+
if [ -f "$pid_file" ]; then
39+
local pid=$(cat "$pid_file")
40+
if kill -0 $pid 2>/dev/null; then
41+
log "Stopping $service (PID: $pid)"
42+
kill $pid
43+
sleep 2
44+
# Force kill if still running
45+
if kill -0 $pid 2>/dev/null; then
46+
log "Force killing $service (PID: $pid)"
47+
kill -9 $pid
48+
fi
49+
fi
50+
rm -f "$pid_file"
51+
fi
52+
}
53+
54+
# Check if service is running
55+
is_running() {
56+
local service=$1
57+
local pid_file="$PID_DIR/${service}.pid"
58+
59+
if [ -f "$pid_file" ]; then
60+
local pid=$(cat "$pid_file")
61+
if kill -0 $pid 2>/dev/null; then
62+
return 0 # Running
63+
else
64+
rm -f "$pid_file" # Clean up stale PID
65+
fi
1566
fi
16-
done
17-
18-
# If we're supposed to run init but couldn't find it
19-
if [ "$1" = "/sbin/init" ] && [ -z "$INIT_PATH" ]; then
20-
echo "Error: Could not find systemd init binary. Please check your installation."
21-
exit 1
22-
elif [ "$1" = "/sbin/init" ] && [ -n "$INIT_PATH" ]; then
23-
# Use the found init path instead of the specified one
24-
shift
25-
exec "$INIT_PATH" "$@"
26-
else
27-
# Run command as specified
28-
exec "$@"
29-
fi
67+
return 1 # Not running
68+
}
69+
70+
# Restart core (and then API)
71+
restart_core() {
72+
log "Core is down! Restarting core and API..."
73+
74+
# Stop both services
75+
stop_service "api"
76+
stop_service "core"
77+
78+
sleep 2
79+
80+
# Start core first
81+
start_core
82+
sleep 3
83+
84+
# Then start API
85+
start_api
86+
}
87+
88+
# Restart API only
89+
restart_api() {
90+
log "API is down! Restarting API..."
91+
stop_service "api"
92+
sleep 2
93+
start_api
94+
}
95+
96+
# Monitor services
97+
monitor() {
98+
log "Starting service monitor..."
99+
100+
while true; do
101+
# Check core
102+
if ! is_running "core"; then
103+
restart_core
104+
# Check API (only if core is running)
105+
elif ! is_running "api"; then
106+
restart_api
107+
fi
108+
109+
sleep $CHECK_INTERVAL
110+
done
111+
}
112+
113+
# Cleanup on exit
114+
cleanup() {
115+
log "Shutting down services..."
116+
stop_service "api"
117+
stop_service "core"
118+
exit 0
119+
}
120+
121+
# Handle signals
122+
trap cleanup SIGTERM SIGINT
123+
124+
# Main execution
125+
log "=== Router Process Manager Starting ==="
126+
127+
# Start both services
128+
start_core
129+
sleep 3 # Give core time to start
130+
start_api
131+
132+
# Monitor forever
133+
monitor

build-docker/debian/router-api.service

Lines changed: 0 additions & 27 deletions
This file was deleted.

build-docker/debian/router-core.service

Lines changed: 0 additions & 27 deletions
This file was deleted.

router-api/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
189189
// Bind server to the specified address and port
190190
.bind(&bind_address)?
191191
// Set number of worker threads to 2 for handling concurrent requests
192-
.workers(2)
192+
.workers(1)
193193
// Start the HTTP server and keep it running until terminated
194194
.run()
195195
.await?;

0 commit comments

Comments
 (0)