-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·304 lines (262 loc) · 8.06 KB
/
run.sh
File metadata and controls
executable file
·304 lines (262 loc) · 8.06 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
# Flask RESTful API Server Startup Script
set -e
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Default values
PORT=5000
HOST="0.0.0.0"
DEBUG="true"
USE_SQLITE=false
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV_DIR="$SCRIPT_DIR/venv"
# Check for local venv and activate if needed
if [ -d "$VENV_DIR" ]; then
if [ -z "$VIRTUAL_ENV" ]; then
echo -e "${BLUE}[INFO]${NC} Activating local virtual environment: $VENV_DIR"
source "$VENV_DIR/bin/activate"
elif [ "$VIRTUAL_ENV" != "$VENV_DIR" ]; then
echo -e "${YELLOW}[WARNING]${NC} Different venv is active: $VIRTUAL_ENV"
echo -e "${BLUE}[INFO]${NC} Switching to local venv: $VENV_DIR"
deactivate 2>/dev/null || true
source "$VENV_DIR/bin/activate"
fi
fi
# Detect Python command (works with venv)
if command -v python &> /dev/null; then
PYTHON_CMD="python"
elif command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
else
echo -e "${RED}[ERROR]${NC} Python not found. Please install Python 3."
exit 1
fi
# Detect pip command (works with venv)
if command -v pip &> /dev/null; then
PIP_CMD="pip"
elif command -v pip3 &> /dev/null; then
PIP_CMD="pip3"
else
PIP_CMD="$PYTHON_CMD -m pip"
fi
# Function to print colored output
print_info() {
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() {
cat << EOF
${GREEN}Flask RESTful API Server${NC}
Usage: $0 [OPTIONS]
Options:
-p, --port PORT Port to run on (default: 5000)
-h, --host HOST Host to bind to (default: 0.0.0.0)
-d, --debug Enable debug mode (default: enabled)
-s, --sqlite Use SQLite instead of PostgreSQL
--install-deps Install/update dependencies
--run-tests Run tests before starting
--help Show this help message
Examples:
# Start server with defaults (PostgreSQL, port 5000)
$0
# Start server with SQLite database
$0 --sqlite
# Start on different port
$0 --port 8080
# Install dependencies and start
$0 --install-deps
# Run tests then start server
$0 --run-tests
# Start with SQLite on port 8080
$0 --sqlite --port 8080
Environment Variables:
DATABASE_URL Override database URL
FLASK_ENV Flask environment (development/production)
LOGLEVEL Logging level (DEBUG/INFO/WARNING/ERROR)
EOF
exit 0
}
# Function to check if we're in a virtual environment
check_venv() {
if [ -n "$VIRTUAL_ENV" ]; then
if [ "$VIRTUAL_ENV" = "$VENV_DIR" ]; then
print_success "Using local virtual environment: venv/"
else
print_success "Using virtual environment: $VIRTUAL_ENV"
fi
return 0
else
print_warning "No virtual environment found in project root"
print_info "Create one with: ./setup-venv.sh"
print_info "Or manually: python3 -m venv venv && source venv/bin/activate"
echo ""
return 1
fi
}
# Function to check if dependencies are installed
check_dependencies() {
print_info "Checking dependencies..."
if ! $PYTHON_CMD -c "import flask" 2>/dev/null; then
print_warning "Flask not found. Installing dependencies..."
install_dependencies
else
print_success "Dependencies are installed"
fi
}
# Function to install dependencies
install_dependencies() {
print_info "Installing dependencies from app/requirements.txt..."
$PIP_CMD install -r app/requirements.txt --upgrade --quiet
print_success "Dependencies installed successfully"
}
# Function to run tests
run_tests() {
print_info "Running test suite..."
if DATABASE_URL="sqlite:///:memory:" $PYTHON_CMD -m pytest tests/ -v --tb=short; then
print_success "All tests passed!"
else
print_error "Tests failed. Aborting server start."
exit 1
fi
}
# Parse command line arguments
INSTALL_DEPS=false
RUN_TESTS=false
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
PORT="$2"
shift 2
;;
-h|--host)
HOST="$2"
shift 2
;;
-d|--debug)
DEBUG="true"
shift
;;
-s|--sqlite)
USE_SQLITE=true
shift
;;
--install-deps)
INSTALL_DEPS=true
shift
;;
--run-tests)
RUN_TESTS=true
shift
;;
--help)
show_usage
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Print banner
echo ""
echo -e "${GREEN}╔════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Flask RESTful API Server Starter ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════╝${NC}"
echo ""
# Check virtual environment
check_venv
# Show Python info
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1)
print_info "Using: $PYTHON_VERSION"
echo ""
# Install dependencies if requested
if [ "$INSTALL_DEPS" = true ]; then
install_dependencies
else
check_dependencies
fi
# Run tests if requested
if [ "$RUN_TESTS" = true ]; then
run_tests
fi
# Configure database
if [ "$USE_SQLITE" = true ]; then
export DATABASE_URL="sqlite:///data.db"
print_info "Using SQLite database: data.db"
else
if [ -z "$DATABASE_URL" ]; then
print_warning "Using PostgreSQL (make sure it's running)"
print_info "PostgreSQL config: postgres:magical_password@0.0.0.0/db"
print_info "To use SQLite instead, run with --sqlite flag"
else
print_info "Using custom DATABASE_URL: $DATABASE_URL"
fi
fi
# Initialize database tables
print_info "Initializing database tables..."
$PYTHON_CMD -c "
import sys
sys.path.insert(0, '.')
from app.app import app
from app.db import db
with app.app_context():
db.create_all()
print('✓ Database tables ready')
" 2>&1 | grep -E '✓|Error|Exception' || true
# Print server configuration
echo ""
print_info "Server Configuration:"
echo " Host: $HOST"
echo " Port: $PORT"
echo " Debug: $DEBUG"
echo " Database: ${DATABASE_URL:-PostgreSQL (from config.py)}"
echo ""
print_info "Starting Flask server..."
echo ""
# Export Flask environment variables
export FLASK_APP=app/app.py
if [ "$DEBUG" = "true" ]; then
export FLASK_ENV=development
export FLASK_DEBUG=1
else
export FLASK_ENV=production
export FLASK_DEBUG=0
fi
# Start the server
print_success "Server is running on http://$HOST:$PORT"
print_info "Press Ctrl+C to stop the server"
echo ""
print_info "Available endpoints:"
echo " POST /register - Register new user"
echo " POST /user - Login (get JWT token)"
echo " GET /user - Get current user info (requires auth)"
echo " POST /store/<name> - Create store (requires auth)"
echo " GET /store/<name> - Get store details"
echo " DELETE /store/<name> - Delete store (requires auth)"
echo " GET /stores - List all stores"
echo " POST /item/<name> - Create item (requires auth)"
echo " GET /item/<name> - Get item details (requires auth)"
echo " PUT /item/<name> - Update/create item (requires auth)"
echo " DELETE /item/<name> - Delete item (requires auth)"
echo " GET /items - List all items (requires auth)"
echo ""
echo -e "${YELLOW}────────────────────────────────────────${NC}"
echo ""
# Run the Flask application
cd "$SCRIPT_DIR"
$PYTHON_CMD -m flask run --host="$HOST" --port="$PORT"