Skip to content

Commit df4fee1

Browse files
committed
Request unused port for the webserver instead of hardcoding a default and iterating until an unused port is found
1 parent a7ecf9c commit df4fee1

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

CHANGELOG.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
v0.2.0
1+
v0.2.1
22

3-
- redo offline docs instructions
4-
- Add the ability to have "live" lookups (disable the usage of a cache for a library)
5-
- rewrite the settings code to store your libraries in a `libraries.pickle` file, away from the standard flow plugin setting.
6-
- on startup, check for libraries stored the pre-0.2.0 way, and if found, move them to the pickle file.
7-
- make the file not found error to be more human readable
8-
- Allow local docs without the need to use file protocol urls
9-
- Convert file protocol urls to normal paths for backward compatibility
10-
- If the default port (2907) is taken, increment until an available one is found
3+
- Request unused port for the webserver instead of hardcoding a default and iterating until an unused port is found

SettingsTemplate.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
body:
22
- type: textBlock
33
attributes:
4-
description: Thank you for using rtfm. Due to limits with flow and the way this plugin was written, I am unable to put the plugin settings in this settings menu. By default, your plugin settings can be accessed by going to the following website "http://localhost:2907/". Additionally, the default settings keyword is "rtfm", which will give you quick access to the website and a button to reload the cache.
4+
description: Thank you for using rtfm. Due to limits with flow and the way this plugin was written, I am unable to put the plugin settings in this settings menu. By default, the settings keyword is "rtfm". Using this keyword, you can open up the settings menu.

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "rtfm",
55
"Description": "a rtfm lookup plugin",
66
"Author": "cibere",
7-
"Version": "0.2.0",
7+
"Version": "0.2.1",
88
"Language": "python_v2",
99
"Website": "https://github.com/cibere/Flow.Launcher.Plugin.rtfm",
1010
"IcoPath": "assets/app.png",

plugin/server/core.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from ..plugin import RtfmPlugin
1414

1515
log = logging.getLogger("webserver")
16-
DEFAULT_PORT = 2908
17-
1816

1917
def build_app(
2018
write_settings: Callable[[list[dict[str, str]]], None],
@@ -74,27 +72,15 @@ async def get_local_doc_path(request: web.Request):
7472
return app
7573

7674

77-
async def start_runner(app: web.Application, host: str, port: int) -> int:
75+
async def start_runner(app: web.Application, host: str, port: int):
7876
runner = web.AppRunner(app)
7977
await runner.setup()
8078

8179
site = web.TCPSite(runner, host, port)
82-
try:
83-
await site.start()
84-
except OSError as e:
85-
# port already used
86-
if e.errno == 10048:
87-
new_port = port + 1
88-
log.exception(
89-
f"Could not start on port {port!r}, incremending port and trying again on {new_port!r}",
90-
exc_info=e,
91-
)
92-
return await start_runner(app, host, new_port)
93-
raise
94-
else:
95-
log.info(f"Started on port {port!r}")
96-
return port
80+
await site.start()
9781

82+
socket_info: tuple[str, int] = site._server.sockets[0].getsockname() # type: ignore
83+
return socket_info[1]
9884

9985
async def run_app(
10086
write_settings: Callable[[list[dict[str, str]]], None],
@@ -103,7 +89,7 @@ async def run_app(
10389
run_forever: bool = True,
10490
):
10591
app = build_app(write_settings, plugin)
106-
port = await start_runner(app, "localhost", DEFAULT_PORT)
92+
port = await start_runner(app, "localhost", 0)
10793

10894
plugin.webserver_port = port
10995

0 commit comments

Comments
 (0)