Skip to content

Commit

Permalink
Merge pull request #52 from zenitsu0509/main
Browse files Browse the repository at this point in the history
Add PowerShell Script for Windows Backend Execution
  • Loading branch information
Pranav0-0Aggarwal authored Nov 14, 2024
2 parents 9ae449b + cb0f5ee commit 9d784a8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
private = dW50cnVzdGVkIGNvbW1lbnQ6IHJzaWduIGVuY3J5cHRlZCBzZWNyZXQga2V5ClJXUlRZMEl5TjRQaUpFRXRad0dDbkNpTHlVNFBOWmZCaDRRWGNDcDdHQ3c5a0hRSUE3NEFBQkFBQUFBQUFBQUFBQUlBQUFBQVhoT1pUY2xvak01d0ZKeVVYdlVnQnlGQXlkUFhuRkdBMEY0QmdYWCtXUCtvaDMvS2tNYnJHSytXcGluQytFQWxPYm1UbVZNdU85aU53aEdJT1IveE82LzB2K2swS0MyZlFybmt5SG51aEt3YlVmMWdDSHBkN1llelU2L0JKT1puVGZFc3liNXplRlk9Cg==
public = dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDY1MDM5OUIzNDQ5MTNEOTMKUldTVFBaRkVzNWtEWlRxWWJCeXhtMTV6ZXZ3OUs0SXhzQzBreVRoekl5bjdXS3hkZlZzQ3VvTkgK
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,43 @@ Once installation and dependency resolution are complete, you can start the back

**UNIX-based Systems (Linux, macOS):**

Bash
bash

```
./run.sh # To run in production mode
./run.sh --test # To run in testing mode (if a test script exists)
./run.sh --test # To run in testing mode
```
**Windows:**
Using PowerShell (Recommended):
powershell
```
.\run-server.ps1 # To run in production mode
.\run-server.ps1 --test # To run in testing mode
```
Note: If you encounter a PowerShell execution policy error, run this command first:
powershell
**Windows:**
```
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
```
Bash
Alternative using Batch (Legacy):
bash
```
run.bat # To run in production mode
run.bat --test # To run in testing mode (if a test script exists)

run.bat --test # To run in testing mode
```
Us
The backend should now be running on port 8000 by default.
The server will start on `http://localhost:8000` by default. In test mode, the server will automatically restart if any errors are detected or if source files are modified.
You can control the number of workers by setting the `WORKERS` environment variable before running the script. If not set, it defaults to 1 worker.
## Additional Resources
- [Tauri Documentation](https://tauri.app/v1/guides/)
Expand Down
49 changes: 49 additions & 0 deletions backend/run-server.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# PowerShell equivalent of the bash script

# Handle Ctrl+C
$script:running = $true
[Console]::TreatControlCAsInput = $true

function Handle-CtrlC {
Write-Host "Exiting..."
$script:running = $false
exit 0
}

# Start a job to check for Ctrl+C
Start-Job -ScriptBlock {
while ($true) {
if ([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true)
if (($key.Modifiers -band [ConsoleModifiers]::Control) -and ($key.Key -eq 'C')) {
Handle-CtrlC
}
}
Start-Sleep -Milliseconds 100
}
} | Out-Null

if ($args[0] -eq "--test") {
while ($script:running) {
Write-Host "Starting Hypercorn server in test environment..."
$process = Start-Process -FilePath "hypercorn" -ArgumentList "main:app --bind 0.0.0.0:8000 --log-level debug --reload --access-log -" -RedirectStandardOutput "hypercorn.log" -RedirectStandardError "hypercorn.log" -PassThru

# Monitor the log file for errors
Get-Content "hypercorn.log" -Wait | ForEach-Object {
Write-Host $_
if ($_ -match "Syntax error") {
Write-Host "Syntax error detected. Restarting server..."
$process.Kill()
}
}

Write-Host "Hypercorn server crashed in test environment. Restarting in 3 seconds..."
Start-Sleep -Seconds 3
}
}
else {
# Get the WORKERS environment variable or default to 1
$workers = if ($env:WORKERS) { $env:WORKERS } else { "1" }
Write-Host "WORKERS: $workers"
hypercorn main:app --workers $workers --bind 0.0.0.0:8000
}

0 comments on commit 9d784a8

Please sign in to comment.