Skip to content
Merged
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
12 changes: 12 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

[env]
CC_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-gcc"
CXX_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-g++"
AR_aarch64_unknown_linux_gnu = "aarch64-linux-gnu-ar"
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = "aarch64-linux-gnu-gcc"

# Use vendored OpenSSL to avoid cross-compilation issues
OPENSSL_STATIC = "true"
OPENSSL_VENDORED = "true"
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ panic = "abort" # Remove panic unwinding code
strip = true # Strip symbols from binary
debug = false # No debug symbols


# Use faster memory allocator
[profile.release.package."*"]
codegen-units = 1
Expand Down
21 changes: 13 additions & 8 deletions build-docker/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
FROM rust:alpine AS builder
WORKDIR /app

# Install build dependencies
# Better build environment for Rust + musl
RUN apk add --no-cache \
build-base \
cmake \
musl-dev\
musl-dev \
pkgconfig \
perl \
perl-utils \
perl-dev \
sqlite-dev \
openssl-dev
openssl-dev \
openssl-libs-static

# Optimize Rust compilation for musl
ENV RUSTFLAGS="-C target-feature=-crt-static -C link-arg=-s"
ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"
RUN rustup target add x86_64-unknown-linux-musl

COPY . .
RUN cargo build -p router-cli --release
RUN cargo build -p router-core --release
RUN cargo build -p router-api --release
RUN cargo build -p router-cli --release --target x86_64-unknown-linux-musl
RUN cargo build -p router-core --release --target x86_64-unknown-linux-musl
RUN cargo build -p router-api --release --target x86_64-unknown-linux-musl

# Runtime image stage
FROM alpine:latest
Expand Down
49 changes: 20 additions & 29 deletions build-docker/debian/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,56 +1,47 @@
# Build stage
FROM rust:bookworm AS builder
WORKDIR /app

# Install build dependencies
RUN apt-get update && apt-get install -y \
build-essential \
cmake \
perl \
libperl-dev \
libsqlite3-dev \
libssl-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*

COPY . .
RUN cargo build -p router-cli --release
RUN cargo build -p router-core --release
RUN cargo build -p router-api --release

# Runtime image stage
FROM debian:bookworm-slim
WORKDIR /app

# Install systemd and other dependencies
# Install minimal runtime dependencies
RUN apt-get update && apt-get install -y \
systemd \
systemd-sysv \
ca-certificates \
bash \
libsqlite3-0 \
libssl3 \
procps \
util-linux \
coreutils \
&& rm -rf /var/lib/apt/lists/*

# Copy binaries from builder
COPY --from=builder /app/target/release/router-core /usr/local/bin/router-core
COPY --from=builder /app/target/release/router-api /usr/local/bin/router-api
COPY --from=builder /app/target/release/router-cli /usr/local/bin/gwrs

# Create necessary directories
RUN mkdir -p /opt/gwrs/bin && \
mkdir -p /opt/gwrs/conf && \
mkdir -p /tmp/gwrs/log

# Create symlinks to binaries
RUN ln -sf /usr/local/bin/router-core /opt/gwrs/bin/router-core && \
ln -sf /usr/local/bin/router-api /opt/gwrs/bin/router-api

# Create systemd service files
RUN mkdir -p /etc/systemd/system
COPY build-docker/debian/router-core.service /etc/systemd/system/gwrs-core.service
COPY build-docker/debian/router-api.service /etc/systemd/system/gwrs-api.service

# Enable services
RUN systemctl enable gwrs-core.service
RUN systemctl enable gwrs-api.service

# Set up entrypoint script
COPY build-docker/debian/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY build-docker/debian/entrypoint.sh /usr/local/bin/entrypoint.sh

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

# Expose API
EXPOSE 24042
# Set entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
154 changes: 129 additions & 25 deletions build-docker/debian/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,133 @@
#!/bin/bash
set -e

# Prepare systemd for container environment
if [ ! -d /run/systemd/system ]; then
mkdir -p /run/systemd/system
fi

# Check for the correct init path
INIT_PATH=""
for path in /sbin/init /lib/systemd/systemd /usr/lib/systemd/systemd /bin/systemd; do
if [ -x "$path" ]; then
INIT_PATH="$path"
break
# Simple configuration
LOG_DIR="/tmp/gwrs/log"
PID_DIR="/tmp/gwrs/pids"
CHECK_INTERVAL=5

# Create directories
mkdir -p "$LOG_DIR" "$PID_DIR"

# Logging
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_DIR/manager.log"
}

# Start core service
start_core() {
log "Starting router-core..."
nohup /usr/local/bin/router-core > "$LOG_DIR/core.log" 2> "$LOG_DIR/core.error" &
echo $! > "$PID_DIR/core.pid"
log "router-core started (PID: $!)"
}

# Start API service
start_api() {
log "Starting router-api..."
nohup /usr/local/bin/router-api > "$LOG_DIR/api.log" 2> "$LOG_DIR/api.error" &
echo $! > "$PID_DIR/api.pid"
log "router-api started (PID: $!)"
}

# Stop a service
stop_service() {
local service=$1
local pid_file="$PID_DIR/${service}.pid"

if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
if kill -0 $pid 2>/dev/null; then
log "Stopping $service (PID: $pid)"
kill $pid
sleep 2
# Force kill if still running
if kill -0 $pid 2>/dev/null; then
log "Force killing $service (PID: $pid)"
kill -9 $pid
fi
fi
rm -f "$pid_file"
fi
}

# Check if service is running
is_running() {
local service=$1
local pid_file="$PID_DIR/${service}.pid"

if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
if kill -0 $pid 2>/dev/null; then
return 0 # Running
else
rm -f "$pid_file" # Clean up stale PID
fi
fi
done

# If we're supposed to run init but couldn't find it
if [ "$1" = "/sbin/init" ] && [ -z "$INIT_PATH" ]; then
echo "Error: Could not find systemd init binary. Please check your installation."
exit 1
elif [ "$1" = "/sbin/init" ] && [ -n "$INIT_PATH" ]; then
# Use the found init path instead of the specified one
shift
exec "$INIT_PATH" "$@"
else
# Run command as specified
exec "$@"
fi
return 1 # Not running
}

# Restart core (and then API)
restart_core() {
log "Core is down! Restarting core and API..."

# Stop both services
stop_service "api"
stop_service "core"

sleep 2

# Start core first
start_core
sleep 3

# Then start API
start_api
}

# Restart API only
restart_api() {
log "API is down! Restarting API..."
stop_service "api"
sleep 2
start_api
}

# Monitor services
monitor() {
log "Starting service monitor..."

while true; do
# Check core
if ! is_running "core"; then
restart_core
# Check API (only if core is running)
elif ! is_running "api"; then
restart_api
fi

sleep $CHECK_INTERVAL
done
}

# Cleanup on exit
cleanup() {
log "Shutting down services..."
stop_service "api"
stop_service "core"
exit 0
}

# Handle signals
trap cleanup SIGTERM SIGINT

# Main execution
log "=== Router Process Manager Starting ==="

# Start both services
start_core
sleep 3 # Give core time to start
start_api

# Monitor forever
monitor
27 changes: 0 additions & 27 deletions build-docker/debian/router-api.service

This file was deleted.

27 changes: 0 additions & 27 deletions build-docker/debian/router-core.service

This file was deleted.

2 changes: 1 addition & 1 deletion router-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Bind server to the specified address and port
.bind(&bind_address)?
// Set number of worker threads to 2 for handling concurrent requests
.workers(2)
.workers(1)
// Start the HTTP server and keep it running until terminated
.run()
.await?;
Expand Down
Loading