Skip to content

Commit

Permalink
remove external CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 21, 2024
1 parent 1cba2aa commit 65bcadd
Show file tree
Hide file tree
Showing 66 changed files with 19,561 additions and 100 deletions.
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ regex = "^2024.7.24"
uvicorn = { version = "^0.30.6", extras = ['standard'] }
uuid6 = "^2024.7.10"
pg8000 = "^1.31.2"
pydash = "^8.0.3"

[tool.poetry.group.dev.dependencies]
mypy = "^1.11.1"
Expand All @@ -33,6 +34,7 @@ types-python-dateutil = "^2.9.0.20240316"
types-regex = "^2024.7.24.20240726"
asyncpg-stubs = "^0.29.1"
types-pyyaml = "^6.0.12.20240808"
semver = "^3.0.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
27 changes: 0 additions & 27 deletions scripts/fetch-bootstrap.py

This file was deleted.

75 changes: 75 additions & 0 deletions scripts/fetch-static-files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import contextlib
import gzip
import io
import json
import shutil
from pathlib import Path
from tarfile import TarFile

import httpx
import semver
from loguru import logger


project_root = Path(__file__, "../..").resolve()
static_path = project_root.joinpath("server/static")
client = httpx.Client(proxies="http://192.168.1.3:7890")


def download_npm_package(name: str, path_filter: tuple[str, ...], version_filter=None):

Check failure on line 19 in scripts/fetch-static-files.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a return type annotation

Check failure on line 19 in scripts/fetch-static-files.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a type annotation for one or more arguments
target = static_path.joinpath(name)
package_json = target.joinpath("package.json")

data = client.get(f"https://registry.npmjs.org/{name}/").raise_for_status().json()

if not version_filter:
latest_version = data["dist-tags"]["latest"]
else:
latest_version = sorted(
[v for v in data["versions"] if version_filter(v)],
key=semver.VersionInfo.parse,
reverse=True,
)[0]

logger.info("[{}]: latest version {}", name, latest_version)

if package_json.exists():
if json.loads(package_json.read_bytes())["version"] == latest_version:
return

logger.info("[{}]: download new version {}", name, latest_version)

with contextlib.suppress(FileNotFoundError):
shutil.rmtree(target)

target.mkdir(exist_ok=True)
package_json.write_bytes(json.dumps({"version": latest_version}).encode())

version = data["versions"][latest_version]
tarball = client.get(version["dist"]["tarball"]).raise_for_status()

with TarFile(fileobj=io.BytesIO(gzip.decompress(tarball.content))) as tar:
for file in tar:
if not file.isfile():
continue
fn = file.path.removeprefix("package/")
if not (fn.startswith(path_filter)):
continue
target_file = target.joinpath(latest_version, fn)
target_file.parent.mkdir(parents=True, exist_ok=True)
target_file.write_bytes(tar.extractfile(file).read())

Check failure on line 60 in scripts/fetch-static-files.py

View workflow job for this annotation

GitHub Actions / mypy

Item "None" of "IO[bytes] | None" has no attribute "read"


def build_version_filter(major: int, stable=True):

Check failure on line 63 in scripts/fetch-static-files.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a return type annotation

Check failure on line 63 in scripts/fetch-static-files.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a type annotation for one or more arguments
def f(s: str) -> bool:
v = semver.VersionInfo.parse(s)
if v.prerelease:
return False
return v.major <= major

return f


download_npm_package("diff2html", ("bundles",), version_filter=build_version_filter(3))
download_npm_package("bootstrap", ("dist",), version_filter=build_version_filter(5))
download_npm_package("jquery", ("dist",), version_filter=build_version_filter(3))
45 changes: 29 additions & 16 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,39 @@ class File(NamedTuple):


static_path = PROJECT_PATH.joinpath("server/static/")
static_files: dict[str, File] = {}

for top, _, files in os.walk(static_path):
for file in files:
file_path = Path(top, file)
rel_path = file_path.relative_to(static_path).as_posix()
static_files["/" + rel_path] = File(
content=file_path.read_bytes(), content_type=mimetypes.guess_type(file)[0]
)
if not DEV:
static_files: dict[str, File] = {}

for top, _, files in os.walk(static_path):
for file in files:
file_path = Path(top, file)
rel_path = file_path.relative_to(static_path).as_posix()
static_files["/" + rel_path] = File(
content=file_path.read_bytes(), content_type=mimetypes.guess_type(file)[0]
)

@litestar.get("/static/{fp:path}", sync_to_thread=False)
def static_file_handler(fp: str) -> Response[bytes]:
try:
f = static_files[fp]
return Response(
content=f.content,
media_type=f.content_type,
headers={"cache-control": "max-age=1200"},
)
except KeyError:
raise NotFoundException() # noqa: B904

else:

@litestar.get("/static/{fp:path}", sync_to_thread=False)
def static_file_handler(fp: str) -> Response[bytes]:
try:
f = static_files[fp]
@litestar.get("/static/{fp:path}")
def static_file_handler(fp: str) -> Response[bytes]:
print(fp)

# fp is '/...', so we need to remove prefix make it relative
return Response(
content=f.content, media_type=f.content_type, headers={"cache-control": "max-age=1200"}
static_path.joinpath(fp[1:]).read_bytes(), media_type=mimetypes.guess_type(fp)[0]
)
except KeyError:
raise NotFoundException() # noqa: B904


async def __fetch_users(rows: list[asyncpg.Record]) -> dict[int, asyncpg.Record]:
Expand Down
12 changes: 6 additions & 6 deletions server/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Annotated, Any

import litestar
import pydash
from asyncpg import Record
from asyncpg.pool import PoolConnectionProxy
from litestar import Response
Expand Down Expand Up @@ -103,15 +104,14 @@ async def __accept_patch(patch: Patch, conn: PoolConnectionProxy[Record], auth:
headers={"Authorization": f"Bearer {auth.access_token}"},
json={
"commitMessage": f"{patch.description} [patch https://patch.bgm38.com/patch/{patch.id}]",
"expectedRevision": {
key: value
for key, value in {
"expectedRevision": pydash.pick(
{
"infobox": patch.original_infobox,
"name": patch.original_name,
"summary": patch.original_summary,
}.items()
if key in subject
},
},
subject.keys(),
),
"subject": subject,
},
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading

0 comments on commit 65bcadd

Please sign in to comment.