Skip to content

Commit

Permalink
Remove server negotiation (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Jun 21, 2024
1 parent 000ca94 commit 9664d5f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 67 deletions.
25 changes: 1 addition & 24 deletions betty/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from multiprocessing import get_context
from os import environ
from pathlib import Path
from typing import TYPE_CHECKING, Mapping, Self, Any, final
from typing import TYPE_CHECKING, Self, Any, final

import aiohttp
from aiofiles.tempfile import TemporaryDirectory
Expand Down Expand Up @@ -76,7 +76,6 @@
from types import TracebackType
from collections.abc import AsyncIterator
from betty.jinja2 import Environment
from betty.serve import Server
from betty.url import StaticUrlGenerator, LocalizedUrlGenerator

CONFIGURATION_DIRECTORY_PATH = fs.HOME_DIRECTORY_PATH / "configuration"
Expand Down Expand Up @@ -660,28 +659,6 @@ def event_types(self) -> set[type[EventType]]:
def event_types(self) -> None:
self._event_types = None

@property
def servers(self) -> Mapping[str, Server]:
"""
The available web servers.
"""
from betty import serve
from betty.extension.demo import DemoServer

return {
server.name(): server
for server in [
*(
server
for extension in self.extensions.flatten()
if isinstance(extension, serve.ServerProvider)
for server in extension.servers
),
serve.BuiltinAppServer(self),
DemoServer(app=self),
]
}

@property
def cache(self) -> Cache[Any] & FileCache:
"""
Expand Down
4 changes: 2 additions & 2 deletions betty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from betty.locale import Str, DEFAULT_LOCALIZER
from betty.logging import CliHandler
from betty.serde.load import AssertionFailed
from betty.serve import AppServer
from betty.serve import BuiltinAppServer

if TYPE_CHECKING:
from PyQt6.QtWidgets import QMainWindow
Expand Down Expand Up @@ -330,7 +330,7 @@ async def _generate(app: App) -> None:
@click.command(help="Serve a generated site.")
@app_command
async def _serve(app: App) -> None:
async with AppServer.get(app) as server:
async with BuiltinAppServer(app) as server:
await server.show()
while True:
await asyncio.sleep(999)
Expand Down
12 changes: 0 additions & 12 deletions betty/extension/nginx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Integrate Betty with `nginx <https://nginx.org/>`_."""

from collections.abc import Sequence
from pathlib import Path

from click import Command
Expand All @@ -18,14 +17,12 @@
from betty.generate import Generator, GenerationContext
from betty.gui import GuiBuilder
from betty.locale import Str, Localizable
from betty.serve import ServerProvider, Server


class Nginx(
ConfigurableExtension[NginxConfiguration],
UserFacingExtension,
Generator,
ServerProvider,
GuiBuilder,
CommandProvider,
):
Expand All @@ -50,15 +47,6 @@ def description(cls) -> Localizable:
def default_configuration(cls) -> NginxConfiguration:
return NginxConfiguration()

@override
@property
def servers(self) -> Sequence[Server]:
from betty.extension.nginx.serve import DockerizedNginxServer

if DockerizedNginxServer.is_available():
return [DockerizedNginxServer(self._app)]
return []

@override
async def generate(self, job_context: GenerationContext) -> None:
await generate_configuration_file(self._app)
Expand Down
6 changes: 3 additions & 3 deletions betty/gui/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
from PyQt6.QtWidgets import QVBoxLayout, QWidget, QPushButton
from typing_extensions import override

from betty import documentation
from betty import documentation, serve
from betty.asyncio import wait_to_thread
from betty.extension import demo
from betty.gui.error import ExceptionCatcher
from betty.gui.text import Text
from betty.gui.window import BettyMainWindow
from betty.locale import Str, Localizable
from betty.serve import Server, AppServer

if TYPE_CHECKING:
from betty.serve import Server
from betty.app import App


Expand Down Expand Up @@ -128,7 +128,7 @@ class ServeProjectWindow(_ServeWindow):

@override
def _new_server(self) -> Server:
return AppServer.get(self._app)
return serve.BuiltinAppServer(self._app)

@override
@property
Expand Down
27 changes: 1 addition & 26 deletions betty/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from http.server import SimpleHTTPRequestHandler, HTTPServer
from io import StringIO
from pathlib import Path
from typing import Sequence, Any, TYPE_CHECKING
from typing import Any, TYPE_CHECKING

from aiofiles.os import makedirs, symlink
from aiofiles.tempfile import TemporaryDirectory, AiofilesContextManagerTempDir
Expand Down Expand Up @@ -153,18 +153,6 @@ def __init__(self, app: App) -> None:
super().__init__(localizer=app.localizer)
self._app = app

@staticmethod
def get(app: App) -> AppServer:
"""
Get a web server for the given Betty application.
"""
for server in app.servers.values():
if isinstance(server, AppServer):
return server
raise RuntimeError(
f"Cannot find a project server. This must never happen, because {BuiltinAppServer} should be the fallback."
)

@override
async def start(self) -> None:
await makedirs(
Expand All @@ -173,19 +161,6 @@ async def start(self) -> None:
await super().start()


class ServerProvider:
"""
Provide one or more web servers.
"""

@property
def servers(self) -> Sequence[Server]:
"""
Provide one or more web servers.
"""
raise NotImplementedError(repr(self))


class _BuiltinServerRequestHandler(SimpleHTTPRequestHandler):
@override
def end_headers(self) -> None:
Expand Down

0 comments on commit 9664d5f

Please sign in to comment.