-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart
More file actions
executable file
·236 lines (211 loc) · 6.9 KB
/
start
File metadata and controls
executable file
·236 lines (211 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
# Media Deduplication Server - Start Script
# This script starts the media deduplication server with the web API
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -b, --build Build the project before starting"
echo " -d, --debug Start in debug mode"
echo " -c, --config Use specific config file"
echo " -p, --port Use specific port (default: 8080)"
echo " --host Use specific host (default: 0.0.0.0)"
echo ""
echo "Examples:"
echo " $0 # Start server with defaults"
echo " $0 --build # Build and start server"
echo " $0 --debug # Start in debug mode"
echo " $0 --port 9090 # Start on port 9090"
echo " $0 --host localhost # Start on localhost only"
}
# Default values
BUILD_FIRST=false
DEBUG_MODE=false
CONFIG_FILE=""
PORT=""
HOST=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-b|--build)
BUILD_FIRST=true
shift
;;
-d|--debug)
DEBUG_MODE=true
shift
;;
-c|--config)
CONFIG_FILE="$2"
shift 2
;;
-p|--port)
PORT="$2"
shift 2
;;
--host)
HOST="$2"
shift 2
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Main execution
main() {
echo "=========================================="
echo " Media Deduplication Server"
echo "=========================================="
echo ""
# Check if we're in the right directory
if [ ! -f "CMakeLists.txt" ]; then
print_error "CMakeLists.txt not found. Please run this script from the project root directory."
exit 1
fi
# Build if requested
if [ "$BUILD_FIRST" = true ]; then
print_status "Building project..."
if [ -f "build.sh" ]; then
./build.sh
else
print_error "build.sh not found. Please build the project manually."
exit 1
fi
print_success "Build completed"
echo ""
fi
# Check if server executable exists
if [ ! -f "build/bin/media_dedup_server" ]; then
print_error "Server executable not found: build/bin/media_dedup_server"
print_status "Building project first..."
if [ -f "build.sh" ]; then
./build.sh
else
print_error "build.sh not found. Please build the project manually."
exit 1
fi
print_success "Build completed"
echo ""
fi
# Prepare server arguments
SERVER_ARGS=""
if [ ! -z "$CONFIG_FILE" ]; then
SERVER_ARGS="$SERVER_ARGS --config $CONFIG_FILE"
print_status "Using config file: $CONFIG_FILE"
fi
if [ "$DEBUG_MODE" = true ]; then
SERVER_ARGS="$SERVER_ARGS --debug"
print_status "Starting in debug mode"
fi
if [ ! -z "$PORT" ]; then
print_status "Port will be configured via config file or server defaults"
fi
if [ ! -z "$HOST" ]; then
print_status "Host will be configured via config file or server defaults"
fi
# Create necessary directories
print_status "Creating necessary directories..."
mkdir -p logs
mkdir -p data
mkdir -p config
# Show server information
echo ""
print_status "Starting Media Deduplication Server..."
# Try to read host/port from config if present for accurate links (robust: Python if available, else grep)
CONF_HOST=$(
python3 - <<'PY' 2>/dev/null || true
try:
import os
import yaml # type: ignore
f='config/config.yaml'
cfg=yaml.safe_load(open(f)) if os.path.exists(f) else {}
print(cfg.get('server.host', cfg.get('server',{}).get('host','')))
except Exception:
print('')
PY
)
if [ -z "$CONF_HOST" ] && [ -f config/config.yaml ]; then
CONF_HOST=$(grep -E '^[[:space:]]*server\.host[[:space:]]*:' config/config.yaml | head -n1 | awk -F: '{print $2}' | tr -d ' "')
fi
if [ -z "$CONF_HOST" ] && [ -f config/config.yaml ]; then
CONF_HOST=$(grep -E '^[[:space:]]*host[[:space:]]*:' config/config.yaml | head -n1 | awk -F: '{print $2}' | tr -d ' "')
fi
[ -z "$CONF_HOST" ] && CONF_HOST="localhost"
CONF_PORT=$(
python3 - <<'PY' 2>/dev/null || true
try:
import os
import yaml # type: ignore
f='config/config.yaml'
cfg=yaml.safe_load(open(f)) if os.path.exists(f) else {}
print(cfg.get('server.port', cfg.get('server',{}).get('port','')))
except Exception:
print('')
PY
)
if [ -z "$CONF_PORT" ] && [ -f config/config.yaml ]; then
CONF_PORT=$(grep -E '^[[:space:]]*server\.port[[:space:]]*:' config/config.yaml | head -n1 | awk -F: '{print $2}' | tr -d ' "')
fi
if [ -z "$CONF_PORT" ] && [ -f config/config.yaml ]; then
CONF_PORT=$(grep -E '^[[:space:]]*port[[:space:]]*:' config/config.yaml | head -n1 | awk -F: '{print $2}' | tr -d ' "')
fi
# Fallback numeric default
[ -z "$CONF_PORT" ] && CONF_PORT=8080
print_status "Web API (OpenAPI JSON) at: http://${CONF_HOST}:${CONF_PORT}/api/openapi.json"
echo ""
print_status "Console commands available:"
print_status " help - Show available commands"
print_status " status - Show server status"
print_status " restart - Restart web server"
print_status " exit - Stop the server"
echo ""
print_status "To see all available API endpoints, run: ./scripts/show_endpoints"
echo ""
print_status "Press Ctrl+C or type 'exit' to stop the server"
echo ""
# CRITICAL: Set ImageMagick environment variables to prevent assertion crashes
# This must be set BEFORE the process starts (setenv() in code is too late)
export MAGICK_DEBUG="None"
export MAGICK_THREAD_LIMIT="1" # Also helps with threading assertions
print_status "ImageMagick debug mode disabled (MAGICK_DEBUG=None) to prevent crashes on corrupted files"
echo ""
# Start the server
if [ "$DEBUG_MODE" = true ]; then
print_status "Starting server in debug mode..."
./build/bin/media_dedup_server $SERVER_ARGS
else
print_status "Starting server..."
./build/bin/media_dedup_server $SERVER_ARGS
fi
}
# Run main function
main "$@"