Add server control script and improve logging

\n- Add scripts/start-server.sh for easy server management\n- Add DLC_NO_COLOR environment variable support for clean logs\n- Update server.go to handle color/no-color logging\n- Add server.log and server.pid to .gitignore\n- Update README.md and AGENTS.md with server control documentation\n- Test all server control functions (start, stop, restart, status, logs, test)
This commit is contained in:
Gabriel Radureau
2026-04-03 16:06:00 +02:00
parent eaaa63d74e
commit 736ec9c996
5 changed files with 215 additions and 2 deletions

154
scripts/start-server.sh Executable file
View File

@@ -0,0 +1,154 @@
#!/bin/bash
# DanceLessonsCoach Server Start Script
# This script starts the server in the background and provides control functions
# Configuration
PROJECT_DIR="/Users/gabrielradureau/Work/Vibe/DanceLessonsCoach"
SERVER_CMD="go run ./cmd/server"
LOG_FILE="server.log"
PID_FILE="server.pid"
# Change to project directory
cd "$PROJECT_DIR" || { echo "Failed to change to project directory"; exit 1; }
# Function to start the server
start_server() {
echo "Starting DanceLessonsCoach server..."
# Kill any existing server
if [ -f "$PID_FILE" ]; then
echo "Found existing PID file, stopping previous server..."
stop_server
fi
# Start server in background with colors disabled for log file
export DLC_NO_COLOR=1
nohup $SERVER_CMD > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
unset DLC_NO_COLOR
echo "Server started with PID: $(cat $PID_FILE)"
echo "Log file: $LOG_FILE"
# Wait a moment for server to start
sleep 2
# Check if server is running
if ps -p $(cat $PID_FILE) > /dev/null; then
echo "Server is running successfully"
echo "Server address: http://localhost:8080"
else
echo "Failed to start server"
echo "Check log file for errors: $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
}
# Function to stop the server
stop_server() {
if [ -f "$PID_FILE" ]; then
echo "Stopping server..."
kill -TERM $(cat "$PID_FILE") 2>/dev/null
# Wait for graceful shutdown
for i in {1..10}; do
if ! ps -p $(cat "$PID_FILE") > /dev/null; then
echo "Server stopped successfully"
rm -f "$PID_FILE"
return 0
fi
sleep 1
done
# Force kill if not stopped
echo "Server did not stop gracefully, forcing kill..."
kill -9 $(cat "$PID_FILE") 2>/dev/null
rm -f "$PID_FILE"
return 1
else
echo "No server is running (PID file not found)"
return 1
fi
}
# Function to check server status
status_server() {
if [ -f "$PID_FILE" ]; then
if ps -p $(cat "$PID_FILE") > /dev/null; then
echo "Server is running (PID: $(cat $PID_FILE))"
echo "Started: $(ps -p $(cat $PID_FILE) -o lstart=)"
return 0
else
echo "Server PID file exists but process is not running"
return 1
fi
else
echo "Server is not running"
return 1
fi
}
# Function to show server logs
tail_logs() {
if [ -f "$LOG_FILE" ]; then
echo "=== Server Logs ==="
tail -n 50 "$LOG_FILE"
else
echo "No log file found: $LOG_FILE"
fi
}
# Function to test API endpoints
test_api() {
echo "Testing API endpoints..."
# Test health endpoint
echo "Testing /api/health:"
curl -s http://localhost:8080/api/health
echo ""
# Test greet endpoint
echo "Testing /api/v1/greet/:"
curl -s http://localhost:8080/api/v1/greet/
echo ""
# Test greet with name
echo "Testing /api/v1/greet/John:"
curl -s http://localhost:8080/api/v1/greet/John
echo ""
}
# Main script logic
case "$1" in
start)
start_server
;;
stop)
stop_server
;;
restart)
stop_server
start_server
;;
status)
status_server
;;
logs)
tail_logs
;;
test)
test_api
;;
*)
echo "Usage: $0 {start|stop|restart|status|logs|test}"
echo " start - Start the server"
echo " stop - Stop the server"
echo " restart - Restart the server"
echo " status - Check server status"
echo " logs - Show server logs"
echo " test - Test API endpoints"
exit 1
;;
esac