Skip to content

Commit a3bd219

Browse files
committed
Better attempt at running after server startup
1 parent 075a8f7 commit a3bd219

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

sphinx_autobuild/__main__.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from __future__ import annotations
44

55
import argparse
6+
import asyncio
67
import shlex
78
import sys
89
import webbrowser
9-
from contextlib import asynccontextmanager
1010
from pathlib import Path
1111

1212
import colorama
@@ -83,40 +83,39 @@ def main(argv=()):
8383
ignore_dirs = list(filter(None, ignore_dirs))
8484
ignore_handler = IgnoreFilter(ignore_dirs, args.re_ignore)
8585

86-
app = _create_app(
87-
watch_dirs, ignore_handler, builder, serve_dir, url_host, args.open_browser
88-
)
86+
app = _create_app(watch_dirs, ignore_handler, builder, serve_dir, url_host)
8987

9088
if not args.no_initial_build:
9189
show_message("Starting initial build")
9290
builder(changed_paths=())
9391

9492
show_message("Waiting to detect changes...")
93+
config = uvicorn.Config(app, host=host_name, port=port_num, log_level="warning")
94+
server = uvicorn.Server(config)
95+
96+
async def serve():
97+
await server.startup()
98+
if args.open_browser:
99+
webbrowser.open(f"http://{url_host}")
100+
await server.main_loop()
101+
await server.shutdown()
102+
95103
try:
96-
uvicorn.run(app, host=host_name, port=port_num, log_level="warning")
104+
asyncio.run(serve())
97105
except KeyboardInterrupt:
98106
show_message("Server ceasing operations. Cheerio!")
99107

100108

101-
def _create_app(
102-
watch_dirs, ignore_handler, builder, out_dir, url_host, open_browser=False
103-
):
109+
def _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host):
104110
watcher = RebuildServer(watch_dirs, ignore_handler, change_callback=builder)
105111

106-
@asynccontextmanager
107-
async def lifespan(app):
108-
async with watcher.lifespan(app):
109-
if open_browser:
110-
webbrowser.open(f"http://{url_host}")
111-
yield
112-
113112
return Starlette(
114113
routes=[
115114
WebSocketRoute("/websocket-reload", watcher, name="reload"),
116115
Mount("/", app=StaticFiles(directory=out_dir, html=True), name="static"),
117116
],
118117
middleware=[Middleware(JavascriptInjectorMiddleware, ws_url=url_host)],
119-
lifespan=lifespan,
118+
lifespan=watcher.lifespan,
120119
)
121120

122121

0 commit comments

Comments
 (0)