Browse Source

refactor: simplify project structure and make it "virtual"

Zubarev Grigoriy 7 months ago
parent
commit
2c9111248b

+ 6 - 0
.dockerignore

@@ -0,0 +1,6 @@
+*
+!requirements.lock
+!src/
+!templates/
+!static/
+static/**/*.xcf

+ 5 - 3
Dockerfile

@@ -1,5 +1,7 @@
 FROM python:3.12.4-alpine
-RUN --mount=source=dist,target=/dist PYTHONDONTWRITEBYTECODE=1 \
-    pip install --no-cache-dir --disable-pip-version-check /dist/*.whl
-CMD [ "uvicorn", "app.__main__:app", "--no-access-log", "--proxy-headers", \
+WORKDIR /app
+COPY requirements.lock ./
+RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -r requirements.lock
+COPY . .
+CMD [ "uvicorn", "src.main:app", "--no-access-log", "--proxy-headers", \
     "--forwarded-allow-ips", "*", "--host", "0.0.0.0", "--port", "5758" ]

+ 1 - 2
README.md

@@ -39,8 +39,7 @@ cd rural-dict
 ### 🐳 With Docker
 
 ```sh
-rye build --wheel --clean
-docker buildx build . --tag rural-dict
+docker build . -t rural-dict
 docker compose up -d
 ```
 

+ 0 - 1
app/__init__.py

@@ -1 +0,0 @@
-__version__ = "0.1.0"

+ 2 - 4
docker-compose.yml

@@ -1,9 +1,7 @@
 version: "3"
 services:
   rural-dict:
-    build:
-      context: .
-      dockerfile: Dockerfile
+    build: .
     restart: unless-stopped
     ports:
-      - "5758:5758"
+      - "127.0.0.1:5758:5758"

+ 5 - 24
pyproject.toml

@@ -1,5 +1,6 @@
 [project]
 name = "rural-dict"
+version = "1.0.0"
 description = "Privacy-respecting, NoJS-supporting Urban Dictionary frontend."
 license = "AGPL-3.0-or-later"
 readme = "README.md"
@@ -17,17 +18,9 @@ dependencies = [
     "uvicorn[standard]~=0.30.6",
     "jinja2~=3.1.4",
 ]
-dynamic = ["version"]
-
-[project.urls]
-"Source Code" = "https://git.vern.cc/cobra/rural-dict"
-"Issue Tracker" = "https://git.vern.cc/cobra/rural-dict/issues"
-
-[build-system]
-requires = ["hatchling"]
-build-backend = "hatchling.build"
 
 [tool.rye]
+virtual = true
 managed = true
 universal = true
 dev-dependencies = [
@@ -35,23 +28,11 @@ dev-dependencies = [
 ]
 
 [tool.rye.scripts]
-dev = """uvicorn app.__main__:app --reload
---reload-include '**/*.py' --reload-include '**/*.html' --reload-include '**/*.css' --port 5758"""
-start = """uvicorn app.__main__:app --no-access-log --proxy-headers
+dev = """uvicorn src.main:app --reload --reload-include 'src/**/*.py'
+--reload-include 'templates/**/*.html' --reload-include 'static/**/*.css' --port 5758"""
+start = """uvicorn src.main:app --no-access-log --proxy-headers
 --forwarded-allow-ips '*' --host 0.0.0.0 --port 5758"""
 
-[tool.hatch.version]
-path = "app/__init__.py"
-
-[tool.hatch.metadata]
-allow-direct-references = true
-
-[tool.hatch.build.targets.wheel]
-packages = ["app"]
-
-[tool.hatch.build.targets.sdist]
-exclude = ["*.xcf"]
-
 [tool.ruff]
 target-version = "py312"
 line-length = 99

+ 0 - 6
requirements-dev.lock

@@ -9,11 +9,9 @@
 #   generate-hashes: false
 #   universal: true
 
--e file:.
 aiohappyeyeballs==2.3.6
     # via aiohttp
 aiohttp==3.10.3
-    # via rural-dict
 aiosignal==1.3.1
     # via aiohttp
 annotated-types==0.7.0
@@ -30,7 +28,6 @@ colorama==0.4.6 ; platform_system == 'Windows' or sys_platform == 'win32'
     # via click
     # via uvicorn
 fastapi==0.112.1
-    # via rural-dict
 frozenlist==1.4.1
     # via aiohttp
     # via aiosignal
@@ -42,7 +39,6 @@ idna==3.7
     # via anyio
     # via yarl
 jinja2==3.1.4
-    # via rural-dict
 markupsafe==2.1.5
     # via jinja2
 multidict==6.0.5
@@ -59,7 +55,6 @@ python-dotenv==1.0.1
 pyyaml==6.0.2
     # via uvicorn
 selectolax==0.3.21
-    # via rural-dict
 sniffio==1.3.1
     # via anyio
 starlette==0.38.2
@@ -69,7 +64,6 @@ typing-extensions==4.12.2
     # via pydantic
     # via pydantic-core
 uvicorn==0.30.6
-    # via rural-dict
 uvloop==0.20.0 ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
     # via uvicorn
 watchfiles==0.23.0

+ 0 - 6
requirements.lock

@@ -9,11 +9,9 @@
 #   generate-hashes: false
 #   universal: true
 
--e file:.
 aiohappyeyeballs==2.3.6
     # via aiohttp
 aiohttp==3.10.3
-    # via rural-dict
 aiosignal==1.3.1
     # via aiohttp
 annotated-types==0.7.0
@@ -29,7 +27,6 @@ colorama==0.4.6 ; platform_system == 'Windows' or sys_platform == 'win32'
     # via click
     # via uvicorn
 fastapi==0.112.1
-    # via rural-dict
 frozenlist==1.4.1
     # via aiohttp
     # via aiosignal
@@ -41,7 +38,6 @@ idna==3.7
     # via anyio
     # via yarl
 jinja2==3.1.4
-    # via rural-dict
 markupsafe==2.1.5
     # via jinja2
 multidict==6.0.5
@@ -56,7 +52,6 @@ python-dotenv==1.0.1
 pyyaml==6.0.2
     # via uvicorn
 selectolax==0.3.21
-    # via rural-dict
 sniffio==1.3.1
     # via anyio
 starlette==0.38.2
@@ -66,7 +61,6 @@ typing-extensions==4.12.2
     # via pydantic
     # via pydantic-core
 uvicorn==0.30.6
-    # via rural-dict
 uvloop==0.20.0 ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
     # via uvicorn
 watchfiles==0.23.0

+ 2 - 4
app/__main__.py → src/main.py

@@ -2,7 +2,6 @@ import re
 from contextlib import asynccontextmanager
 from datetime import datetime
 from json import JSONDecodeError
-from pathlib import Path
 
 import aiohttp
 from fastapi import FastAPI, Request
@@ -21,10 +20,9 @@ async def lifespan(app: FastAPI):
     await session.close()
 
 
-ROOT_PATH = Path(__file__).parent
 app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None)
-app.mount("/static", StaticFiles(directory=ROOT_PATH / "static"), name="static")
-templates = Jinja2Templates(directory=ROOT_PATH / "templates")
+app.mount("/static", StaticFiles(directory="static"), name="static")
+templates = Jinja2Templates(directory="templates")
 session: aiohttp.ClientSession = None  # pyright: ignore[reportAssignmentType]
 
 

+ 0 - 0
app/static/css/main.css → static/css/main.css


+ 0 - 0
app/static/img/favicon.png → static/img/favicon.png


+ 0 - 0
app/static/img/favicon.xcf → static/img/favicon.xcf


+ 0 - 0
app/static/img/logo.png → static/img/logo.png


+ 0 - 0
app/static/img/logo.xcf → static/img/logo.xcf


+ 0 - 0
app/templates/404.html → templates/404.html


+ 0 - 0
app/templates/base.html → templates/base.html


+ 0 - 0
app/templates/index.html → templates/index.html