askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
urls.py · 5.8 KB · 144 lines
| 1 | """site_urls -- every URL this server answers, compiled: /urls. |
| 2 | |
| 3 | The sibling of /flow. Routing here has three sources and all of them are |
| 4 | inspectable -- convention (a site_* object's file IS its URL), the |
| 5 | site_routes collection (patterned routes as data), and the views |
| 6 | collection (generated pages declare their route) -- plus the fixed core |
| 7 | API surface, whose paths are constants in http_api_contract by design. |
| 8 | Because none of it hides in code, the site map is compiled, not |
| 9 | maintained: install a package and its URLs appear here; delete a page |
| 10 | object and its convention URL vanishes. A hand-written sitemap is wrong |
| 11 | within a week; this one cannot be. |
| 12 | |
| 13 | Each row says WHERE the URL comes from (convention | route | view | core), |
| 14 | what serves it, and -- for routes that shadow or are shadowed -- notes |
| 15 | that convention wins over data (docs/site-routing.md's precedence, |
| 16 | verified the hard way). |
| 17 | |
| 18 | Signed-in users only, same courtesy as /flow: the complete map of a |
| 19 | server's surface is reconnaissance data. |
| 20 | """ |
| 21 | |
| 22 | import html |
| 23 | import os |
| 24 | from pathlib import Path |
| 25 | |
| 26 | import http_api_contract |
| 27 | import object_namespace |
| 28 | import object_records |
| 29 | |
| 30 | |
| 31 | def _base_dir(): |
| 32 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 33 | |
| 34 | |
| 35 | def _esc(value): |
| 36 | return html.escape(str(value or "")) |
| 37 | |
| 38 | |
| 39 | def _safe_records(collection): |
| 40 | try: |
| 41 | return object_records.read_collection_records(collection, base_dir=_base_dir()) |
| 42 | except Exception: |
| 43 | return [] |
| 44 | |
| 45 | |
| 46 | def _convention_pages(): |
| 47 | """site_* objects whose file IS their URL (/notes -> site_notes).""" |
| 48 | out = [] |
| 49 | for root in object_namespace.get_object_roots(): |
| 50 | site_dir = Path(root) / "site" |
| 51 | if not site_dir.exists(): |
| 52 | continue |
| 53 | for path in sorted(site_dir.glob("*.py")): |
| 54 | name = path.stem |
| 55 | if name.startswith("_"): |
| 56 | continue |
| 57 | url = "/" if name == "home" else "/" + name.replace("_", "-") |
| 58 | out.append({"url": url, "serves": f"site_{name}", "source": "convention"}) |
| 59 | return out |
| 60 | |
| 61 | |
| 62 | def _core_api_rows(): |
| 63 | """The fixed platform surface: contract constants, not discovery.""" |
| 64 | rows = [ |
| 65 | (http_api_contract.SEARCH_PATH, "global search"), |
| 66 | (http_api_contract.MCP_PATH, "MCP: agents operate the server as tools"), |
| 67 | (http_api_contract.WEBHOOKS_PATH + "/{name}", "raw-body webhooks (webhook_{name} objects)"), |
| 68 | ("/api/schema/{collection}", "schema as contract: clients build their own UI"), |
| 69 | ("/collections/{collection}/records", "the JSON records API"), |
| 70 | ("/objects/{object_id}", "direct object execution"), |
| 71 | ("/ws", "realtime push (permission-filtered signals)"), |
| 72 | ("/login", "session login"), |
| 73 | (http_api_contract.DAEMON_STATUS_PATH, "daemon posture (admin token)"), |
| 74 | (http_api_contract.DAEMON_SCHEDULER_TASKS_PATH, "scheduler task CRUD (admin token)"), |
| 75 | ] |
| 76 | return [{"url": u, "serves": what, "source": "core"} for u, what in rows] |
| 77 | |
| 78 | |
| 79 | def GET(request): |
| 80 | identity = request.get("_identity") or {} |
| 81 | if not identity.get("user_id"): |
| 82 | body = ('<p class="hint"><a href="/login?next=/urls">Sign in</a> to see the map of ' |
| 83 | "every URL this server answers.</p>") |
| 84 | else: |
| 85 | rows = [] |
| 86 | rows += _convention_pages() |
| 87 | for r in _safe_records("site_routes"): |
| 88 | rows.append({"url": r.get("pattern", ""), "serves": r.get("object_id", ""), |
| 89 | "source": "route"}) |
| 90 | for v in _safe_records("views"): |
| 91 | if v.get("route"): |
| 92 | rows.append({"url": v["route"], "serves": f"view: {v.get('id')}", |
| 93 | "source": "view"}) |
| 94 | rows += _core_api_rows() |
| 95 | |
| 96 | # Convention beats data: where both claim a URL, say so instead of |
| 97 | # listing two rows that look equally live. |
| 98 | convention_urls = {r["url"] for r in rows if r["source"] == "convention"} |
| 99 | seen = set() |
| 100 | table = [] |
| 101 | for r in sorted(rows, key=lambda x: (x["url"], x["source"])): |
| 102 | key = (r["url"], r["serves"]) |
| 103 | if key in seen: |
| 104 | continue |
| 105 | seen.add(key) |
| 106 | shadowed = (r["source"] in ("route", "view") |
| 107 | and r["url"] in convention_urls) |
| 108 | note = ('<span class="warn">shadowed: a convention page object wins for this ' |
| 109 | 'URL</span>' if shadowed else "") |
| 110 | table.append( |
| 111 | f"<tr><td><code><a href='{_esc(r['url'])}'>{_esc(r['url'])}</a></code></td>" |
| 112 | f"<td>{_esc(r['serves'])}</td><td>{_esc(r['source'])}</td><td>{note}</td></tr>") |
| 113 | body = f""" |
| 114 | <div class="breadcrumb"><a href="/">Home</a> / URLs</div> |
| 115 | <h1>Every URL this server answers</h1> |
| 116 | <p class="muted">Compiled from convention (a page object's file is its URL), the |
| 117 | site_routes and views collections, and the fixed core API. Nothing here is |
| 118 | hand-maintained, so nothing here can be stale. {len(table)} entries.</p> |
| 119 | <table><thead><tr><th>URL</th><th>serves</th><th>source</th><th></th></tr></thead> |
| 120 | <tbody>{''.join(table)}</tbody></table> |
| 121 | <p class="muted">Precedence when sources overlap: convention page objects win, then |
| 122 | site_routes patterns match as fallback (docs/site-routing.md).</p> |
| 123 | """ |
| 124 | |
| 125 | return {"content_type": "text/html; charset=utf-8", "body": f"""<!doctype html> |
| 126 | <html lang="en"> |
| 127 | <head> |
| 128 | <meta charset="utf-8"> |
| 129 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 130 | <title>URLs</title> |
| 131 | <link rel="stylesheet" href="/style"> |
| 132 | <style> |
| 133 | .wrap {{ max-width: 960px; margin: 0 auto; padding: 1.25rem; }} |
| 134 | table {{ width: 100%; border-collapse: collapse; }} |
| 135 | td, th {{ text-align: left; padding: 0.35rem 0.6rem; border-bottom: 1px solid var(--line, #333); }} |
| 136 | .muted {{ color: var(--muted, #999); }} |
| 137 | .warn {{ color: var(--warning, #f1b747); font-size: 0.8rem; }} |
| 138 | </style> |
| 139 | </head> |
| 140 | <body> |
| 141 | <script src="/nav"></script> |
| 142 | <div class="wrap">{body}</div> |
| 143 | </body> |
| 144 | </html>"""} |