-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·282 lines (242 loc) · 6.92 KB
/
setup.sh
File metadata and controls
executable file
·282 lines (242 loc) · 6.92 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
#!/bin/bash
# Ledn Backend Challenge - Automated Setup Script
# This script sets up the complete development environment
set -e # Exit on any error
echo "🚀 Ledn Backend Challenge - Setup Starting..."
echo "================================================"
# 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"
}
# Check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
# Check Node.js
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed. Please install Node.js 18+ first."
exit 1
fi
NODE_VERSION=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 18 ]; then
print_error "Node.js version must be 18 or higher. Current version: $(node --version)"
exit 1
fi
# Check Docker
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
# Check Docker Compose
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
print_success "All prerequisites are satisfied"
echo " - Node.js: $(node --version)"
echo " - Docker: $(docker --version | cut -d',' -f1)"
}
# Install dependencies
install_dependencies() {
print_status "Installing Node.js dependencies..."
npm ci
print_success "Dependencies installed"
}
# Setup environment
setup_environment() {
print_status "Setting up environment configuration..."
if [ ! -f .env ]; then
if [ -f .env.example ]; then
cp .env.example .env
print_success "Environment file created from template"
else
# Create basic .env if example doesn't exist
cat > .env << EOF
# Database
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/ledn_db?schema=public"
# Server
NODE_ENV=development
PORT=3000
HOST=localhost
# API Configuration
API_VERSION=v1
API_PREFIX=/api
# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
TRANSACTION_RATE_LIMIT_MAX=30
TRANSFER_RATE_LIMIT_MAX=10
# Logging
LOG_LEVEL=info
LOG_FILE_ENABLED=true
# Metrics
METRICS_ENABLED=true
PROMETHEUS_ENABLED=true
EOF
print_success "Basic environment file created"
fi
else
print_success "Environment file already exists"
fi
}
# Start database
start_database() {
print_status "Starting PostgreSQL database..."
# Check if database is already running
if docker ps | grep -q postgres; then
print_warning "PostgreSQL container is already running"
else
npm run docker:up
print_status "Waiting for database to be ready..."
sleep 10
print_success "Database started"
fi
}
# Run migrations
run_migrations() {
print_status "Running database migrations..."
npm run db:migrate
print_success "Migrations completed"
}
# Seed database
seed_database() {
print_status "Seeding database with development data..."
print_warning "This will take approximately 30-60 seconds..."
npm run db:seed:dev
print_success "Database seeded with development data"
echo " - Approximately 5,000 accounts created"
echo " - Approximately 10,000 transactions created"
}
# Build application
build_application() {
print_status "Building TypeScript application..."
npm run build
print_success "Application built successfully"
}
# Final verification
verify_setup() {
print_status "Verifying setup..."
# Check if build directory exists
if [ ! -d "dist" ]; then
print_error "Build directory not found"
return 1
fi
# Check if database is accessible
if ! npm run db:generate &> /dev/null; then
print_error "Database is not accessible"
return 1
fi
print_success "Setup verification completed"
}
# Start development server
start_server() {
print_status "Starting development server..."
echo ""
print_success "🎉 Setup completed successfully!"
echo "================================================"
echo ""
echo "📋 Access Information:"
echo " • API: http://localhost:3000/api/v1/accounts"
echo " • Documentation: http://localhost:3000/api-docs"
echo " • Health Check: http://localhost:3000"
echo ""
echo "🛠️ Available Commands:"
echo " • npm run dev - Start development server with hot reload"
echo " • npm run test - Run tests"
echo " • npm run db:seed - Seed with full dataset (95k transactions)"
echo " • npm run docker:down - Stop database"
echo ""
echo "🚀 Starting development server..."
echo " Press Ctrl+C to stop"
echo ""
npm run dev
}
# Handle script interruption
cleanup() {
print_warning "Setup interrupted by user"
exit 1
}
# Set trap for cleanup
trap cleanup SIGINT SIGTERM
# Main execution
main() {
echo "This script will:"
echo " 1. Check prerequisites (Node.js, Docker)"
echo " 2. Install dependencies"
echo " 3. Setup environment configuration"
echo " 4. Start PostgreSQL database"
echo " 5. Run database migrations"
echo " 6. Seed database with development data"
echo " 7. Build and start the application"
echo ""
echo "⏱️ Estimated time: 2-3 minutes"
echo ""
read -p "Do you want to continue? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_warning "Setup cancelled by user"
exit 0
fi
check_prerequisites
install_dependencies
setup_environment
start_database
run_migrations
seed_database
build_application
verify_setup
start_server
}
# Show help
show_help() {
echo "Ledn Backend Challenge - Setup Script"
echo ""
echo "Usage:"
echo " ./setup.sh - Run full setup and start server"
echo " ./setup.sh --help - Show this help message"
echo " ./setup.sh --db-only - Setup database only (no server start)"
echo ""
echo "Prerequisites:"
echo " - Node.js 18+ (recommended: 22.15.0)"
echo " - Docker and Docker Compose"
echo " - Git"
}
# Parse command line arguments
case "${1:-}" in
--help|-h)
show_help
exit 0
;;
--db-only)
check_prerequisites
install_dependencies
setup_environment
start_database
run_migrations
seed_database
print_success "Database setup completed"
exit 0
;;
"")
main
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac