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

4
.gitignore vendored
View File

@@ -15,3 +15,7 @@ go.work
# macOS specific files
.DS_Store
# Server runtime files
server.log
server.pid

View File

@@ -85,7 +85,41 @@ DanceLessonsCoach/
## 🚀 Server Management
### Starting the Server
### Using the Server Control Script
A convenient shell script is provided for managing the server lifecycle:
```bash
# Navigate to project directory
cd /Users/gabrielradureau/Work/Vibe/DanceLessonsCoach
# Start the server
./scripts/start-server.sh start
# Check server status
./scripts/start-server.sh status
# Test API endpoints
./scripts/start-server.sh test
# View server logs
./scripts/start-server.sh logs
# Stop the server
./scripts/start-server.sh stop
```
**Server Control Script Commands:**
- `start` - Start the server in background with proper logging
- `stop` - Stop the server gracefully
- `restart` - Restart the server
- `status` - Check if server is running
- `logs` - Show recent server logs
- `test` - Test all API endpoints
### Manual Server Management
If you prefer manual control:
```bash
# Navigate to project directory

View File

@@ -101,6 +101,21 @@ go run ./cmd/greet John
### Web Server
**Using the server control script (recommended):**
```bash
# Start the server
./scripts/start-server.sh start
# Test API endpoints
./scripts/start-server.sh test
# Stop the server
./scripts/start-server.sh stop
```
**Manual server management:**
```bash
# Start the server
go run ./cmd/server

View File

@@ -21,7 +21,13 @@ func NewServer() *Server {
// Initialize Zerolog with Trace level
zerolog.SetGlobalLevel(zerolog.TraceLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
// Configure logging with optional color support
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}
if os.Getenv("DLC_NO_COLOR") == "1" {
consoleWriter.NoColor = true // Disable colors when DLC_NO_COLOR=1
}
log.Logger = log.Output(consoleWriter)
s := &Server{
router: chi.NewRouter(),

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