Fix issue 1362 and add a default favicon.ico (#1369)

This commit is contained in:
evandrocoan 2021-11-22 23:18:32 -03:00 committed by GitHub
parent 7a556fb6ea
commit 6d0f7e7f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

View File

@ -1,6 +1,7 @@
filegroup( filegroup(
name = "imgs", name = "imgs",
srcs = glob([ srcs = glob([
"**/*.ico",
"**/*.png", "**/*.png",
"**/*.svg", "**/*.svg",
]), ]),

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -1680,6 +1680,7 @@ title="{}" {}>{}</button>""".format(
def setupMediaServer(self) -> None: def setupMediaServer(self) -> None:
self.mediaServer = aqt.mediasrv.MediaServer(self) self.mediaServer = aqt.mediasrv.MediaServer(self)
self.mediaServer.start() self.mediaServer.start()
self.mediaServer.wait_start_up()
def baseHTML(self) -> str: def baseHTML(self) -> str:
return f'<base href="{self.serverURL()}">' return f'<base href="{self.serverURL()}">'

View File

@ -3,16 +3,20 @@
from __future__ import annotations from __future__ import annotations
import datetime
import logging import logging
import mimetypes import mimetypes
import os import os
import re import re
import socket
import sys import sys
import threading import threading
import time import time
import traceback import traceback
from contextlib import contextmanager
from dataclasses import dataclass from dataclasses import dataclass
from http import HTTPStatus from http import HTTPStatus, client
from typing import Any, Generator
import flask import flask
import flask_cors # type: ignore import flask_cors # type: ignore
@ -32,6 +36,18 @@ from aqt.deckoptions import DeckOptionsDialog
from aqt.operations.deck import update_deck_configs from aqt.operations.deck import update_deck_configs
from aqt.qt import * from aqt.qt import *
@contextmanager
def http_connection(
*args: Any, **kwds: Any
) -> Generator[client.HTTPConnection, None, None]:
resource = client.HTTPConnection(*args, **kwds)
try:
yield resource
finally:
resource.close()
app = flask.Flask(__name__, root_path="/fake") app = flask.Flask(__name__, root_path="/fake")
flask_cors.CORS(app) flask_cors.CORS(app)
@ -121,6 +137,46 @@ class MediaServer(threading.Thread):
except KeyError: except KeyError:
pass pass
def getHost(self) -> str:
self._ready.wait()
return str(self.server.effective_host) # type: ignore
def wait_start_up(self) -> None:
self.check_server(self.getHost(), self.getPort(), "/favicon.ico")
@classmethod
def check_server(
cls,
host: str,
port: int,
path_info: str = "/",
timeout: int = 1,
retries: int = 30,
) -> int:
"""Perform a request until the server reply"""
if retries < 0:
return 0
# https://github.com/Pylons/webtest/blob/4b8a3ebf984185ff4fefb31b4d0cf82682e1fcf7/webtest/http.py#L123-L132
for index in range(retries):
if devMode or index > 0:
print(
f"{datetime.datetime.now()} waiting media server on {host}:{port}..."
)
try:
with http_connection(host, port, timeout=timeout) as conn:
conn.request("GET", path_info)
res = conn.getresponse()
return res.status
except (socket.error, client.HTTPException):
time.sleep(0.3)
return 0
@app.route("/favicon.ico")
def favicon() -> Response:
request = BundledFileRequest(os.path.join("imgs", "favicon.ico"))
return _handle_builtin_file_request(request)
def _mime_for_path(path: str) -> str: def _mime_for_path(path: str) -> str:
"Mime type for provided path/filename." "Mime type for provided path/filename."