-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsetup.ps1
More file actions
250 lines (211 loc) · 9.51 KB
/
setup.ps1
File metadata and controls
250 lines (211 loc) · 9.51 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
param(
[Parameter(Position=0)]
[string]$Command = "help"
)
function Show-Help {
Write-Host "================================================================" -ForegroundColor Blue
Write-Host " FastAPI-HTMX - Setup Script " -ForegroundColor Blue
Write-Host "================================================================" -ForegroundColor Blue
Write-Host ""
Write-Host "Available commands:" -ForegroundColor Green
Write-Host " setup - Complete project setup" -ForegroundColor Yellow
Write-Host " install - Install dependencies only" -ForegroundColor Yellow
Write-Host " env - Create .env file" -ForegroundColor Yellow
Write-Host " migrate - Run database migrations" -ForegroundColor Yellow
Write-Host " init-db - Initialize database with migrations" -ForegroundColor Yellow
Write-Host " run - Start the application" -ForegroundColor Yellow
Write-Host " run-dev - Start in development mode" -ForegroundColor Yellow
Write-Host " credentials - Show admin login credentials" -ForegroundColor Yellow
Write-Host " status - Show project status" -ForegroundColor Yellow
Write-Host " clean - Clean up generated files" -ForegroundColor Yellow
Write-Host ""
Write-Host "Usage: .\setup.ps1 [command]" -ForegroundColor Magenta
}
function Install-Dependencies {
Write-Host "Installing dependencies..." -ForegroundColor Blue
if (Get-Command poetry -ErrorAction SilentlyContinue) {
poetry install
} else {
pip install -r requirements.txt
}
Write-Host "Dependencies installed!" -ForegroundColor Green
}
function Create-EnvFile {
Write-Host "Creating .env file..." -ForegroundColor Blue
if (-not (Test-Path ".env")) {
$envContent = @"
# Database Configuration
DATABASE_URL="sqlite+aiosqlite:///./users.db"
SECRET_KEY="super-secret-key-example-123456789"
# MinIO Configuration (Required for file uploads)
MINIO_URL="http://localhost:9000"
MINIO_ACCESS_KEY="minioadmin123456789"
MINIO_SECRET_KEY="miniosecret987654321xyz"
MINIO_BUCKET="my-fastapi-bucket"
MINIO_SECURE=false
# CSRF Protection
CSRF_SECRET_KEY="csrf-secret-key-example-987654321"
COOKIE_SAMESITE="lax"
COOKIE_SECURE=true
"@
$envContent | Out-File -FilePath ".env" -Encoding UTF8
Write-Host ".env file created!" -ForegroundColor Green
} else {
Write-Host ".env file already exists, skipping..." -ForegroundColor Yellow
}
}
function Run-Migrations {
Write-Host "Running database migrations..." -ForegroundColor Blue
# Check if this is the first time (no migration files exist)
$migrationsPath = "app\migrations\versions"
$migrationFiles = Get-ChildItem -Path $migrationsPath -Filter "*.py" -ErrorAction SilentlyContinue | Where-Object { $_.Name -ne "__init__.py" }
if (-not $migrationFiles) {
Write-Host "No migration files found. Creating initial migration..." -ForegroundColor Yellow
# Create initial migration
if (Get-Command poetry -ErrorAction SilentlyContinue) {
poetry run alembic revision --autogenerate -m "Initial migration"
} else {
alembic revision --autogenerate -m "Initial migration"
}
# Find the newly created migration file
$newMigrationFiles = Get-ChildItem -Path $migrationsPath -Filter "*.py" -ErrorAction SilentlyContinue | Where-Object { $_.Name -ne "__init__.py" }
if ($newMigrationFiles) {
$latestMigration = $newMigrationFiles | Sort-Object CreationTime -Descending | Select-Object -First 1
Write-Host "Adding required imports to migration file: $($latestMigration.Name)" -ForegroundColor Yellow
# Read the migration file content
$migrationContent = Get-Content $latestMigration.FullName -Raw
# Check if imports are already present
if ($migrationContent -notmatch "import fastapi_users_db_sqlalchemy.generics" -or $migrationContent -notmatch "import app.models.groups") {
# Find the line with existing imports and add our imports after it
$lines = Get-Content $latestMigration.FullName
$newLines = @()
$importsAdded = $false
foreach ($line in $lines) {
$newLines += $line
# Add imports after the revision identifiers but before the main imports
if ($line -match "^# Revision ID:" -and -not $importsAdded) {
# Find the end of the header comments and add imports
continue
}
if ($line -match "^from alembic import op" -and -not $importsAdded) {
$newLines += "import fastapi_users_db_sqlalchemy.generics"
$newLines += "import app.models.groups"
$newLines += ""
$importsAdded = $true
}
}
# Write the updated content back to the file
$newLines | Set-Content $latestMigration.FullName -Encoding UTF8
Write-Host "Required imports added to migration file!" -ForegroundColor Green
} else {
Write-Host "Required imports already present in migration file." -ForegroundColor Green
}
}
}
# Run the migrations
if (Get-Command poetry -ErrorAction SilentlyContinue) {
poetry run alembic upgrade head
} else {
alembic upgrade head
}
Write-Host "Migrations completed!" -ForegroundColor Green
}
function Initialize-Database {
Write-Host "Initializing database with migrations..." -ForegroundColor Blue
# Check if alembic is initialized
if (-not (Test-Path "alembic.ini")) {
Write-Host "Alembic not initialized. Please run 'alembic init' first." -ForegroundColor Red
return
}
# Create initial migration if none exist
$migrationsPath = "app\migrations\versions"
if (-not (Test-Path $migrationsPath)) {
New-Item -ItemType Directory -Path $migrationsPath -Force | Out-Null
}
Run-Migrations
}
function Start-App {
Write-Host "Starting FastAPI-HTMX..." -ForegroundColor Green
Write-Host "Application will be available at: http://127.0.0.1:8000" -ForegroundColor Blue
Show-Credentials
poetry run python main.py
}
function Start-DevApp {
Write-Host "Starting FastAPI-HTMX in development mode..." -ForegroundColor Green
Write-Host "Application will be available at: http://127.0.0.1:8000" -ForegroundColor Blue
Write-Host "Auto-reload enabled" -ForegroundColor Blue
Show-Credentials
uvicorn app.app:app --reload --host 127.0.0.1 --port 8000
}
function Show-Credentials {
Write-Host "Admin Login Credentials" -ForegroundColor Blue
Write-Host "Email: superuser@admin.com" -ForegroundColor Green
Write-Host "Password: password123" -ForegroundColor Green
Write-Host "URL: http://127.0.0.1:8080" -ForegroundColor Blue
}
function Show-Status {
Write-Host "Project Status" -ForegroundColor Blue
Write-Host "Project: FastAPI-HTMX" -ForegroundColor Blue
try {
$pythonVersion = python --version 2>$null
Write-Host "Python: $pythonVersion" -ForegroundColor Blue
} catch {
Write-Host "Python: Not found" -ForegroundColor Red
}
if (Get-Command poetry -ErrorAction SilentlyContinue) {
try {
$poetryVersion = poetry --version
Write-Host "Poetry: $poetryVersion" -ForegroundColor Blue
} catch {
Write-Host "Poetry: Found but error getting version" -ForegroundColor Yellow
}
} else {
Write-Host "Poetry: Not found" -ForegroundColor Yellow
}
if (Test-Path 'users.db') {
Write-Host "Database: Exists" -ForegroundColor Green
} else {
Write-Host "Database: Not found" -ForegroundColor Yellow
}
if (Test-Path '.env') {
Write-Host "Environment: Configured" -ForegroundColor Green
} else {
Write-Host "Environment: Not configured" -ForegroundColor Yellow
}
}
function Clean-Project {
Write-Host "Cleaning up..." -ForegroundColor Blue
Get-ChildItem -Recurse -Filter "*.pyc" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Recurse -Directory -Name "__pycache__" | ForEach-Object {
Remove-Item $_ -Recurse -Force -ErrorAction SilentlyContinue
}
if (Test-Path ".pytest_cache") {
Remove-Item ".pytest_cache" -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "Cleanup completed!" -ForegroundColor Green
}
function Complete-Setup {
Write-Host "Starting complete project setup..." -ForegroundColor Green
Install-Dependencies
Create-EnvFile
Run-Migrations
Write-Host "Setup complete! Run '.\setup.ps1 run' to start the application." -ForegroundColor Green
Show-Credentials
}
switch ($Command.ToLower()) {
"help" { Show-Help }
"setup" { Complete-Setup }
"install" { Install-Dependencies }
"env" { Create-EnvFile }
"migrate" { Run-Migrations }
"init-db" { Initialize-Database }
"run" { Start-App }
"run-dev" { Start-DevApp }
"credentials" { Show-Credentials }
"status" { Show-Status }
"clean" { Clean-Project }
default {
Write-Host "Unknown command: $Command" -ForegroundColor Red
Show-Help
}
}