diff --git a/read.ps1 b/read.ps1 new file mode 100644 index 00000000..37e5e3e9 --- /dev/null +++ b/read.ps1 @@ -0,0 +1,69 @@ +<# +.SYNOPSIS + Convenience wrapper for reader3: process a book, (re)start the server, and open it. + +.EXAMPLE + .\read.ps1 dracula.epub # process (if new), start server if needed, open the book +.EXAMPLE + .\read.ps1 # just open the library, starting the server if needed +.EXAMPLE + .\read.ps1 -Stop # stop the server +#> +param( + [string]$EpubPath, + [switch]$Stop +) + +$ErrorActionPreference = "Stop" + +function Get-ServerPid { + $line = netstat -ano | Select-String "127.0.0.1:8123\s.*LISTENING" + if ($line) { + return ($line -split "\s+")[-1] + } + return $null +} + +Push-Location $PSScriptRoot +try { + $serverPid = Get-ServerPid + + if ($Stop) { + if ($serverPid) { + Stop-Process -Id $serverPid -Force + Write-Host "Server stopped." + } else { + Write-Host "Server was not running." + } + return + } + + $dataDir = $null + if ($EpubPath) { + if (-not (Test-Path $EpubPath)) { + Write-Error "File not found: $EpubPath" + return + } + $bookName = [System.IO.Path]::GetFileNameWithoutExtension($EpubPath) + $dataDir = "${bookName}_data" + if (-not (Test-Path $dataDir)) { + Write-Host "Processing $EpubPath..." + uv run reader3.py $EpubPath + } + } + + if (-not $serverPid) { + Write-Host "Starting server..." + Start-Process -WindowStyle Hidden -FilePath "uv" -ArgumentList "run","server.py" -WorkingDirectory $PSScriptRoot + Start-Sleep -Seconds 2 + } + + if ($dataDir) { + Start-Process "http://localhost:8123/read/$dataDir" + } else { + Start-Process "http://localhost:8123/" + } +} +finally { + Pop-Location +} diff --git a/server.py b/server.py index 9c870dc6..3a33e0e4 100644 --- a/server.py +++ b/server.py @@ -56,9 +56,9 @@ async def library_view(request: Request): return templates.TemplateResponse("library.html", {"request": request, "books": books}) @app.get("/read/{book_id}", response_class=HTMLResponse) -async def redirect_to_first_chapter(book_id: str): +async def redirect_to_first_chapter(request: Request, book_id: str): """Helper to just go to chapter 0.""" - return await read_chapter(book_id=book_id, chapter_index=0) + return await read_chapter(request=request, book_id=book_id, chapter_index=0) @app.get("/read/{book_id}/{chapter_index}", response_class=HTMLResponse) async def read_chapter(request: Request, book_id: str, chapter_index: int):