commit 066b714718f9b06148f47aec1d621013533ec8bd parent fecf3a40e23dccedfc188e3323773aec4015261e Author: Silas Brack <silasbrack@gmail.com> Date: Sat, 27 Sep 2025 18:18:48 +0200 refactor: move static files into package Diffstat:
9 files changed, 35 insertions(+), 16 deletions(-)
diff --git a/flake.nix b/flake.nix @@ -78,6 +78,18 @@ pyproject-build-systems.overlays.default overlay pyprojectOverrides + # (final: prev: { + # simple-web-app = prev.simple-web-app.overrideAttrs (old: { + # src = lib.fileset.toSource { + # root = old.src; + # fileset = lib.fileset.unions [ + # (old.src + "/pyproject.toml") + # (old.src + "/src") + # (old.src + "/queries") + # ]; + # }; + # }); + # }) ] ); inherit (pkgs.callPackages pyproject-nix.build.util { }) mkApplication; @@ -88,12 +100,15 @@ # Enable no optional dependencies for production build. packages.x86_64-linux.default = pythonSet.mkVirtualEnv "simple-web-app-env" workspace.deps.default; + # default = mkApplication { + # venv = pythonSet.mkVirtualEnv "simple-web-app-env" workspace.deps.default; + # package = pythonSet.hello-world; + # }; + # Make hello runnable with `nix run` - apps.x86_64-linux = { - default = { - type = "app"; - program = "${self.packages.x86_64-linux.default}/bin/simple-web-app"; - }; + apps.x86_64-linux.default = { + type = "app"; + program = "${self.packages.x86_64-linux.default}/bin/simple-web-app"; }; # This example provides two different modes of development: @@ -215,9 +230,5 @@ ''; }; }; - # default = mkApplication { - # venv = pythonSet.mkVirtualEnv "application-env" workspace.deps.default; - # package = pythonSet.simple-web-app; - # }; }; } diff --git a/pyproject.toml b/pyproject.toml @@ -42,3 +42,7 @@ build-backend = "setuptools.build_meta" [tool.setuptools.packages.find] where = ["src"] + +[tool.setuptools.package-data] +simple_web_app = ["queries/**", "migrations/**", "templates/**", "static/**"] + diff --git a/src/simple_web_app/main.py b/src/simple_web_app/main.py @@ -1,4 +1,5 @@ import contextlib +import importlib.resources import logging import os import sqlite3 @@ -31,13 +32,16 @@ SQLITE_PRAGMAS = [ "PRAGMA foreign_keys = ON;", "PRAGMA synchronous = NORMAL;", ] -MIGRATION_FOLDER = Path("./migrations") -DATABASE_PATH = os.getenv("DATABASE_PATH", default="./db.sqlite3") -templates = Jinja2Templates(directory="./templates") -queries_basic = aiosql.from_path("./queries/basic.sql", "aiosqlite") -# queries_auth = aiosql.from_path("./queries/auth.sql", "aiosqlite") +DATABASE_PATH = Path(os.getenv("DATABASE_PATH", default="./db.sqlite3")) +MIGRATION_DIR = importlib.resources.files("simple_web_app").joinpath("migrations") +TEMPLATE_DIR = importlib.resources.files("simple_web_app").joinpath("templates") +QUERY_DIR = importlib.resources.files("simple_web_app").joinpath("queries") +STATIC_DIR = importlib.resources.files("simple_web_app").joinpath("static") + +templates = Jinja2Templates(directory=TEMPLATE_DIR) +queries_basic = aiosql.from_path(QUERY_DIR / "basic.sql", "aiosqlite") def is_htmx_request(request: Request) -> bool: @@ -80,7 +84,7 @@ async def show_home_page(request: Request): async def lifespan(app: Starlette): conn = sqlite3.connect(DATABASE_PATH) create_migrations_table_if_not_exists(conn) - migration_files = sorted(MIGRATION_FOLDER.glob("*.sql")) + migration_files = sorted(MIGRATION_DIR.glob("*.sql")) migration_queries = [p.read_text() for p in migration_files] apply_migrations(conn, migration_queries) @@ -93,6 +97,6 @@ async def lifespan(app: Starlette): routes = [ Route("/", methods=["GET"], endpoint=show_home_page), - Mount("/static", StaticFiles(directory="static"), name="static"), + Mount("/static", StaticFiles(directory=STATIC_DIR), name="static"), ] app = Starlette(debug=False, routes=routes, lifespan=lifespan) diff --git a/migrations/first.sql b/src/simple_web_app/migrations/first.sql diff --git a/migrations/second.sql b/src/simple_web_app/migrations/second.sql diff --git a/queries/basic.sql b/src/simple_web_app/queries/basic.sql diff --git a/static/style.css b/src/simple_web_app/static/style.css diff --git a/templates/application.html b/src/simple_web_app/templates/application.html diff --git a/templates/index.html b/src/simple_web_app/templates/index.html