encore-dev 2.0.0b8__tar.gz → 2.0.0b9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/PKG-INFO +4 -2
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/README.md +3 -1
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/__init__.py +6 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/config/_build.py +6 -11
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/cron/__init__.py +33 -3
- encore_dev-2.0.0b9/encore/django/__init__.py +56 -0
- encore_dev-2.0.0b9/encore/django/base.py +48 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/env/__init__.py +56 -14
- encore_dev-2.0.0b9/encore/frontends/__init__.py +49 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/gateway/__init__.py +3 -2
- encore_dev-2.0.0b9/encore/gateways/__init__.py +57 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/_loader.py +38 -3
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/_native.pyi +64 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/clients.py +11 -1
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/reqfields.py +14 -1
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/testscope.py +115 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/pubsub/__init__.py +20 -7
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/_propagate.py +35 -5
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/httpx.py +51 -11
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/hash.py +68 -27
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/testing/__init__.py +53 -7
- encore_dev-2.0.0b9/encore/testing/_playwright_fixtures.py +147 -0
- encore_dev-2.0.0b9/encore/testing/_steps.py +216 -0
- encore_dev-2.0.0b9/encore/testing/playwright.py +369 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/testing/pytest_plugin.py +56 -5
- encore_dev-2.0.0b9/encore/testing/system.py +881 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/_context.py +28 -3
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/pyproject.toml +3 -1
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/pyproject.toml.orig +4 -1
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/_run.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/config/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/config/_markers.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/errs/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/gateway/_serve.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/gateway/match.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/lazy.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/runtime.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/internal/startup.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/logs/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/logs/_api.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/logs/_console.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/logs/_runtime.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/metrics/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/metrics/_instrument.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/metrics/_metrics.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/metrics/_provider.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/py.typed +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/reqctx/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/reqctx/_json.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/secrets/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/_serve.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/_span.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/_tracing.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/asgi.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/server/wsgi.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/aiohttp.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/niquests.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/services/requests.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/shutdown/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/objects/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/postgres/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_base.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_batch.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_errors.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_responses.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_scan.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/_values.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/bitmap.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/command.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/generic.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/geo.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/hyperloglog.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/json.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/list.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/pubsub.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/script.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/set.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/sorted_set.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/stream.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/storage/redis/string.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/__init__.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/_encode.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/_provider.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/_span.py +0 -0
- {encore_dev-2.0.0b8 → encore_dev-2.0.0b9}/encore/tracing/_tracer.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: encore-dev
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.0b9
|
|
4
4
|
Summary: Encore SDK for Python — infrastructure resources
|
|
5
5
|
Author: Encore
|
|
6
6
|
License: MPL-2.0
|
|
@@ -1078,7 +1078,9 @@ The same conversion applies to list elements, hash values, set/sorted-set/geo
|
|
|
1078
1078
|
members, stream field values, and pubsub payloads. For pubsub, use `subscribe[User](...)`
|
|
1079
1079
|
or `psubscribe[User](...)`; `Message.payload` then has that type.
|
|
1080
1080
|
Database keys, hash/stream field names, stream IDs, channel names, cursors,
|
|
1081
|
-
scores, and other metadata retain their existing types.
|
|
1081
|
+
scores, and other metadata retain their existing types. Where hash field names
|
|
1082
|
+
are what a command returns (`hkeys`, and `hrandfield` without values), they
|
|
1083
|
+
are its values: `hkeys[str]` reads them as `str`. A custom value class
|
|
1082
1084
|
is not recorded in Redis: callers must choose the matching decoder.
|
|
1083
1085
|
|
|
1084
1086
|
Missing values remain `None`, and empty collections remain empty. Malformed
|
|
@@ -1055,7 +1055,9 @@ The same conversion applies to list elements, hash values, set/sorted-set/geo
|
|
|
1055
1055
|
members, stream field values, and pubsub payloads. For pubsub, use `subscribe[User](...)`
|
|
1056
1056
|
or `psubscribe[User](...)`; `Message.payload` then has that type.
|
|
1057
1057
|
Database keys, hash/stream field names, stream IDs, channel names, cursors,
|
|
1058
|
-
scores, and other metadata retain their existing types.
|
|
1058
|
+
scores, and other metadata retain their existing types. Where hash field names
|
|
1059
|
+
are what a command returns (`hkeys`, and `hrandfield` without values), they
|
|
1060
|
+
are its values: `hkeys[str]` reads them as `str`. A custom value class
|
|
1059
1061
|
is not recorded in Redis: callers must choose the matching decoder.
|
|
1060
1062
|
|
|
1061
1063
|
Missing values remain `None`, and empty collections remain empty. Malformed
|
|
@@ -52,7 +52,9 @@ if TYPE_CHECKING or "_pytest" not in sys.modules:
|
|
|
52
52
|
cron,
|
|
53
53
|
env,
|
|
54
54
|
errs,
|
|
55
|
+
frontends,
|
|
55
56
|
gateway,
|
|
57
|
+
gateways,
|
|
56
58
|
logs,
|
|
57
59
|
metrics,
|
|
58
60
|
pubsub,
|
|
@@ -78,7 +80,9 @@ __all__ = [
|
|
|
78
80
|
"cron",
|
|
79
81
|
"env",
|
|
80
82
|
"errs",
|
|
83
|
+
"frontends",
|
|
81
84
|
"gateway",
|
|
85
|
+
"gateways",
|
|
82
86
|
"logs",
|
|
83
87
|
"metrics",
|
|
84
88
|
"reqctx",
|
|
@@ -100,7 +104,9 @@ _SUBMODULES = {
|
|
|
100
104
|
"cron",
|
|
101
105
|
"env",
|
|
102
106
|
"errs",
|
|
107
|
+
"frontends",
|
|
103
108
|
"gateway",
|
|
109
|
+
"gateways",
|
|
104
110
|
"logs",
|
|
105
111
|
"metrics",
|
|
106
112
|
"pubsub",
|
|
@@ -21,7 +21,9 @@ import typing_extensions
|
|
|
21
21
|
|
|
22
22
|
from ..secrets import Secret, _config_field
|
|
23
23
|
|
|
24
|
-
#: The key of the placeholder a secret field's value is, as the runtime puts it
|
|
24
|
+
#: The key of the placeholder a secret field's value is, as the runtime puts it:
|
|
25
|
+
#: ``{"$encore_secret": "<name>"}``. The name alone says which secret it is: a
|
|
26
|
+
#: secret's name has one scope across the app.
|
|
25
27
|
PLACEHOLDER_KEY = "$encore_secret"
|
|
26
28
|
|
|
27
29
|
_UNION_TYPES: Tuple[Any, ...] = (Union,)
|
|
@@ -185,18 +187,11 @@ class _Builder:
|
|
|
185
187
|
raise self.fail(path, "matches none of the union's types")
|
|
186
188
|
|
|
187
189
|
def secret(self, data: Any, path: List[str]) -> Any:
|
|
188
|
-
|
|
189
|
-
if not isinstance(
|
|
190
|
+
name = data.get(PLACEHOLDER_KEY) if isinstance(data, dict) else None
|
|
191
|
+
if not isinstance(name, str) or not name or len(data) != 1:
|
|
190
192
|
# Never say what the value is: it may be a pasted secret.
|
|
191
193
|
raise self.fail(path, "is a secret field without the runtime's placeholder")
|
|
192
|
-
|
|
193
|
-
# alone, but a scope the runtime does not know means a mismatch.
|
|
194
|
-
scope = placeholder.get("scope")
|
|
195
|
-
if scope not in ("service", "shared", "global"):
|
|
196
|
-
raise self.fail(
|
|
197
|
-
path, "is a secret field with a placeholder of no known scope"
|
|
198
|
-
)
|
|
199
|
-
return _config_field(placeholder["name"])
|
|
194
|
+
return _config_field(name)
|
|
200
195
|
|
|
201
196
|
def dataclass(
|
|
202
197
|
self,
|
|
@@ -4,18 +4,20 @@ Typical usage example:
|
|
|
4
4
|
|
|
5
5
|
from encore import cron
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
CLEANUP = cron.Job("cleanup", path="/internal/cleanup", title="Nightly cleanup", every="24h")
|
|
8
8
|
|
|
9
9
|
A cron job is a parse-time declaration: the parser discovers it and, when the
|
|
10
10
|
job is due, the platform sends a ``POST`` request for ``path`` to the service
|
|
11
|
-
that declares the job. There is no runtime client.
|
|
11
|
+
that declares the job. There is no runtime client. A job keeps what it was
|
|
12
|
+
declared with, to read but not change: declare its route from ``path``, so
|
|
13
|
+
the route is always the one Encore requests.
|
|
12
14
|
|
|
13
15
|
The request carries an idempotency key in ``IDEMPOTENCY_KEY_HEADER``: an id
|
|
14
16
|
for the run that is the same each time Encore sends it, as when it sends it
|
|
15
17
|
again after a failure. A job that must not do its work twice can check
|
|
16
18
|
whether it has seen the key before::
|
|
17
19
|
|
|
18
|
-
@app.post(
|
|
20
|
+
@app.post(CLEANUP.path)
|
|
19
21
|
async def cleanup(request: Request) -> None:
|
|
20
22
|
key = request.headers.get(cron.IDEMPOTENCY_KEY_HEADER)
|
|
21
23
|
...
|
|
@@ -81,3 +83,31 @@ class Job:
|
|
|
81
83
|
self._title = title
|
|
82
84
|
self._every = every
|
|
83
85
|
self._schedule = schedule
|
|
86
|
+
|
|
87
|
+
# Read-only: Encore schedules the job from its declaration in the source,
|
|
88
|
+
# so a changed path would not be the one it requests.
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def name(self) -> str:
|
|
92
|
+
"""The cron job's resource name."""
|
|
93
|
+
return self._name
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def path(self) -> str:
|
|
97
|
+
"""The service path Encore calls with POST when the job is due."""
|
|
98
|
+
return self._path
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def title(self) -> str | None:
|
|
102
|
+
"""The human-readable title, if one was declared."""
|
|
103
|
+
return self._title
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def every(self) -> str | None:
|
|
107
|
+
"""The interval between calls, such as "24h", if declared with one."""
|
|
108
|
+
return self._every
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def schedule(self) -> str | None:
|
|
112
|
+
"""The cron expression, if declared with one."""
|
|
113
|
+
return self._schedule
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Django's database backend for an Encore database.
|
|
2
|
+
|
|
3
|
+
Point a Django database at one of the app's databases with :func:`database`::
|
|
4
|
+
|
|
5
|
+
from encore.django import database
|
|
6
|
+
from storage.resources import db
|
|
7
|
+
|
|
8
|
+
DATABASES = {"default": database(db.write())}
|
|
9
|
+
|
|
10
|
+
Django then connects to it as it connects to any PostgreSQL database, with
|
|
11
|
+
psycopg, but finds where it is when it connects rather than when the
|
|
12
|
+
settings are read. Under ``encore test`` each test has a database of its
|
|
13
|
+
own, which only exists once the test has begun: a connection is to the
|
|
14
|
+
database of the test it is opened in, and a connection a test before it
|
|
15
|
+
opened is closed and opened again.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import threading
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from ..storage.postgres import DatabaseRef
|
|
24
|
+
|
|
25
|
+
_lock = threading.Lock()
|
|
26
|
+
# The databases settings name, by the NAME they have there.
|
|
27
|
+
_databases: dict[str, DatabaseRef] = {}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def database(ref: DatabaseRef, **settings: Any) -> dict[str, Any]:
|
|
31
|
+
"""Return the settings of a Django database that is ``ref``'s.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
ref: A handle of the database, from its ``write()`` or ``read()``,
|
|
35
|
+
which grants the service the access it gives.
|
|
36
|
+
**settings: Further Django settings of the database, such as
|
|
37
|
+
``CONN_MAX_AGE`` or ``OPTIONS``.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
An entry of ``DATABASES``.
|
|
41
|
+
"""
|
|
42
|
+
with _lock:
|
|
43
|
+
name = f"encore-{len(_databases)}"
|
|
44
|
+
_databases[name] = ref
|
|
45
|
+
return {**settings, "ENGINE": "encore.django", "NAME": name}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _database(name: str) -> DatabaseRef:
|
|
49
|
+
"""The database :func:`database` gave the settings NAME ``name``."""
|
|
50
|
+
try:
|
|
51
|
+
return _databases[name]
|
|
52
|
+
except KeyError:
|
|
53
|
+
raise LookupError(
|
|
54
|
+
f"no Encore database is named {name!r} in Django's settings: give "
|
|
55
|
+
"it with encore.django.database(db.write()) rather than by hand"
|
|
56
|
+
) from None
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""The backend Django loads for ``"ENGINE": "encore.django"``.
|
|
2
|
+
|
|
3
|
+
Django's PostgreSQL backend, connecting where the Encore database is.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import Any, Optional
|
|
9
|
+
|
|
10
|
+
from django.db.backends.postgresql import base
|
|
11
|
+
from psycopg.conninfo import conninfo_to_dict
|
|
12
|
+
|
|
13
|
+
from ..internal import testscope
|
|
14
|
+
from . import _database
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DatabaseWrapper(base.DatabaseWrapper):
|
|
18
|
+
"""A connection to an Encore database, through the runtime's proxy."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
21
|
+
"""Make the connection, as Django's PostgreSQL backend does."""
|
|
22
|
+
super().__init__(*args, **kwargs)
|
|
23
|
+
# The test the connection was opened in, if any.
|
|
24
|
+
self._encore_test: Optional[testscope.Scope] = None
|
|
25
|
+
|
|
26
|
+
def get_connection_params(self) -> dict[str, Any]:
|
|
27
|
+
"""Django's parameters, connecting where the Encore database is.
|
|
28
|
+
|
|
29
|
+
In place of the NAME the settings give it: under test, the test's own.
|
|
30
|
+
"""
|
|
31
|
+
params = super().get_connection_params()
|
|
32
|
+
ref = _database(self.settings_dict["NAME"])
|
|
33
|
+
params.update(conninfo_to_dict(ref.connection_string))
|
|
34
|
+
return params
|
|
35
|
+
|
|
36
|
+
def connect(self) -> None:
|
|
37
|
+
"""Connect, in the test the call belongs to, if any."""
|
|
38
|
+
# Before connecting, which runs queries of its own.
|
|
39
|
+
self._encore_test = testscope.current()
|
|
40
|
+
super().connect()
|
|
41
|
+
|
|
42
|
+
def ensure_connection(self) -> None:
|
|
43
|
+
"""Connect if not connected, or connected for another test."""
|
|
44
|
+
# Django keeps a thread's connection from one test to the next; this
|
|
45
|
+
# test's database is another.
|
|
46
|
+
if self.connection is not None and self._encore_test is not testscope.current():
|
|
47
|
+
self.close()
|
|
48
|
+
super().ensure_connection()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Metadata for the running application
|
|
1
|
+
"""Metadata for the running application, its API gateways and frontends.
|
|
2
2
|
|
|
3
3
|
Typical usage example:
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ Typical usage example:
|
|
|
11
11
|
# An app can declare several gateways, so look one up by name.
|
|
12
12
|
gw = md.gateways.get("api")
|
|
13
13
|
if gw is not None:
|
|
14
|
-
print("api is at", gw.
|
|
14
|
+
print("api is at", gw.url)
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
from __future__ import annotations
|
|
@@ -28,6 +28,7 @@ __all__ = [
|
|
|
28
28
|
"app_meta",
|
|
29
29
|
"AppMeta",
|
|
30
30
|
"GatewayMeta",
|
|
31
|
+
"FrontendMeta",
|
|
31
32
|
"EnvironmentMeta",
|
|
32
33
|
"BuildMeta",
|
|
33
34
|
"DeployMeta",
|
|
@@ -41,20 +42,41 @@ class GatewayMeta:
|
|
|
41
42
|
|
|
42
43
|
Attributes:
|
|
43
44
|
name: The name of the gateway, as declared by the application.
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
url: The URL the API of this running application is called at through
|
|
46
|
+
this gateway: the first of its origins. Empty when the environment
|
|
47
|
+
gives the gateway no origins.
|
|
48
|
+
origins: Every origin the gateway is reached at in this environment
|
|
49
|
+
(``scheme://host[:port]``), the first its URL. After the first, an
|
|
50
|
+
entry may be a pattern of one, ``https://*.example.com``, or
|
|
51
|
+
``*``, which makes the gateway the one serving requests no other
|
|
52
|
+
origin matches.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
name: str
|
|
56
|
+
|
|
57
|
+
url: str
|
|
58
|
+
|
|
59
|
+
origins: List[str] = field(default_factory=list)
|
|
60
|
+
|
|
46
61
|
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class FrontendMeta:
|
|
64
|
+
"""A frontend of the application, and where it is reached.
|
|
49
65
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
66
|
+
Attributes:
|
|
67
|
+
name: The name of the frontend: for one a service hosts, the
|
|
68
|
+
service's.
|
|
69
|
+
url: The URL the frontend is reached at: the first of its origins.
|
|
70
|
+
origins: Every origin the frontend is reached at in this environment,
|
|
71
|
+
the first its URL; after the first, an entry may be a pattern of
|
|
72
|
+
one, ``https://*.example.com``.
|
|
53
73
|
"""
|
|
54
74
|
|
|
55
75
|
name: str
|
|
56
76
|
|
|
57
|
-
|
|
77
|
+
url: str
|
|
78
|
+
|
|
79
|
+
origins: List[str] = field(default_factory=list)
|
|
58
80
|
|
|
59
81
|
|
|
60
82
|
@dataclass(frozen=True)
|
|
@@ -138,12 +160,20 @@ class AppMeta:
|
|
|
138
160
|
|
|
139
161
|
Distinct from :attr:`app_id`: the id is opaque and stable, the slug
|
|
140
162
|
is readable and may be renamed. Locally the two are the same.
|
|
141
|
-
gateways: The API gateways
|
|
142
|
-
name
|
|
163
|
+
gateways: The API gateways of this application the service is given,
|
|
164
|
+
keyed by gateway name: those its code looks up the URL of, and the
|
|
165
|
+
one it hosts.
|
|
143
166
|
|
|
144
167
|
An application can declare several gateways, so there is no single
|
|
145
168
|
base URL for the API — look up the gateway you mean by name. A
|
|
146
|
-
|
|
169
|
+
service's processes, its Workers' included, list the gateways its
|
|
170
|
+
code names (``gateways.url("api")``) and the one it hosts, and no
|
|
171
|
+
other, the default included; one told of none reports an empty
|
|
172
|
+
mapping.
|
|
173
|
+
frontends: The application's frontends the service is given — those
|
|
174
|
+
its code looks up the URL of (``frontends.url("web")``), and the
|
|
175
|
+
one it hosts — keyed by name, with where each is reached. One the
|
|
176
|
+
environment gives no origins is left out.
|
|
147
177
|
environment: The environment the app is running in.
|
|
148
178
|
build: The build the running binary came from.
|
|
149
179
|
deploy: This deployment of the binary.
|
|
@@ -155,6 +185,8 @@ class AppMeta:
|
|
|
155
185
|
|
|
156
186
|
gateways: Dict[str, GatewayMeta]
|
|
157
187
|
|
|
188
|
+
frontends: Dict[str, FrontendMeta]
|
|
189
|
+
|
|
158
190
|
environment: EnvironmentMeta
|
|
159
191
|
|
|
160
192
|
build: BuildMeta
|
|
@@ -187,10 +219,20 @@ def _decode(raw: dict) -> AppMeta:
|
|
|
187
219
|
app_slug=raw.get("app_slug", ""),
|
|
188
220
|
gateways={
|
|
189
221
|
name: GatewayMeta(
|
|
190
|
-
name=gw.get("name", name),
|
|
222
|
+
name=gw.get("name", name),
|
|
223
|
+
url=gw.get("url", ""),
|
|
224
|
+
origins=list(gw.get("origins") or []),
|
|
191
225
|
)
|
|
192
226
|
for name, gw in (raw.get("gateways") or {}).items()
|
|
193
227
|
},
|
|
228
|
+
frontends={
|
|
229
|
+
name: FrontendMeta(
|
|
230
|
+
name=fe.get("name", name),
|
|
231
|
+
url=fe.get("url", ""),
|
|
232
|
+
origins=list(fe.get("origins") or []),
|
|
233
|
+
)
|
|
234
|
+
for name, fe in (raw.get("frontends") or {}).items()
|
|
235
|
+
},
|
|
194
236
|
environment=EnvironmentMeta(
|
|
195
237
|
name=env.get("name", ""),
|
|
196
238
|
type=env.get("type", "development"),
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Where the application's frontends are reached.
|
|
2
|
+
|
|
3
|
+
:func:`url` is a frontend's URL in the environment the code runs in, as to
|
|
4
|
+
send a user there or to build a link to one of its pages::
|
|
5
|
+
|
|
6
|
+
from encore import frontends
|
|
7
|
+
|
|
8
|
+
reset = f"{frontends.url('web')}/reset?token={token}"
|
|
9
|
+
|
|
10
|
+
Looking a frontend's URL up links the calling service to it. Encore reads the
|
|
11
|
+
link from the code, as it reads which services a service depends on: so the
|
|
12
|
+
name must be a string literal, written as the frontend declares it, the
|
|
13
|
+
function must be called directly rather than passed around, and the call must
|
|
14
|
+
be in a service.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from .. import env
|
|
20
|
+
|
|
21
|
+
__all__ = ["url"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def url(frontend: str) -> str:
|
|
25
|
+
"""Return the URL a frontend is reached at, without a trailing slash.
|
|
26
|
+
|
|
27
|
+
It is the first of the frontend's origins in this environment, as
|
|
28
|
+
:func:`encore.env.app_meta` lists them. The name is the one the frontend is
|
|
29
|
+
declared with, in its ``encore.frontend.json`` or as the service hosting
|
|
30
|
+
it: ``web_app`` does not find ``web-app``.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
frontend: The name of the frontend to look up.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
The frontend's URL, with no trailing ``/``.
|
|
37
|
+
|
|
38
|
+
Raises:
|
|
39
|
+
ValueError: If the app has no frontend of that name, or the
|
|
40
|
+
environment gives it no origins.
|
|
41
|
+
"""
|
|
42
|
+
fe = env.app_meta().frontends.get(frontend)
|
|
43
|
+
found = fe.origins[0].rstrip("/") if fe is not None and fe.origins else ""
|
|
44
|
+
if not found:
|
|
45
|
+
raise ValueError(
|
|
46
|
+
f"encore: no URL is known for frontend {frontend!r}: the app has no "
|
|
47
|
+
"frontend of that name, or the environment gives it no origins"
|
|
48
|
+
)
|
|
49
|
+
return found
|
|
@@ -69,8 +69,9 @@ Hooks:
|
|
|
69
69
|
cookies; each reaches Python only as the hook reads it.
|
|
70
70
|
|
|
71
71
|
Middleware see only public requests the gateway routes: calls between
|
|
72
|
-
services, Pub/Sub pushes
|
|
73
|
-
|
|
72
|
+
services, Pub/Sub pushes and requests for paths the gateway does not
|
|
73
|
+
expose are Encore's alone, and so are CORS preflights, but for a gateway
|
|
74
|
+
whose CORS is off (``"cors": false``), which routes them as any request.
|
|
74
75
|
|
|
75
76
|
Answering a request:
|
|
76
77
|
``on_request`` returns to let the request through, or raises a
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Where the application's gateways are reached.
|
|
2
|
+
|
|
3
|
+
:func:`url` is a gateway's URL in the environment the code runs in, as to
|
|
4
|
+
hand a client outside the app the address of its API::
|
|
5
|
+
|
|
6
|
+
from encore import gateways
|
|
7
|
+
|
|
8
|
+
callback = f"{gateways.url('api')}/webhooks/stripe"
|
|
9
|
+
|
|
10
|
+
A service calls the app's other services with :mod:`encore.services`, not
|
|
11
|
+
through a gateway. A gateway's Middleware is :mod:`encore.gateway`'s.
|
|
12
|
+
|
|
13
|
+
Looking a gateway's URL up links the calling service to it. Encore reads the
|
|
14
|
+
link from the code, as it reads which services a service depends on: so the
|
|
15
|
+
name must be a string literal, written as the gateway declares it, the
|
|
16
|
+
function must be called directly rather than passed around, and the call must
|
|
17
|
+
be in a service.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from .. import env
|
|
23
|
+
|
|
24
|
+
__all__ = ["url"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def url(gateway: str) -> str:
|
|
28
|
+
"""Return the URL a gateway is reached at, without a trailing slash.
|
|
29
|
+
|
|
30
|
+
It is the first of the gateway's origins in this environment, as
|
|
31
|
+
:func:`encore.env.app_meta` lists them. The name is the one the gateway is
|
|
32
|
+
declared with, in its ``encore.gateway.json`` or as the service hosting
|
|
33
|
+
it: ``public_api`` does not find ``public-api``.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
gateway: The name of the gateway to look up.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
The gateway's URL, with no trailing ``/``.
|
|
40
|
+
|
|
41
|
+
Raises:
|
|
42
|
+
ValueError: If the app has no gateway of that name, or the
|
|
43
|
+
environment gives it no origins.
|
|
44
|
+
"""
|
|
45
|
+
gw = env.app_meta().gateways.get(gateway)
|
|
46
|
+
if gw is None:
|
|
47
|
+
raise ValueError(
|
|
48
|
+
f"encore: no URL is known for gateway {gateway!r}: the app has no gateway "
|
|
49
|
+
"of that name"
|
|
50
|
+
)
|
|
51
|
+
found = gw.origins[0].rstrip("/") if gw.origins else ""
|
|
52
|
+
if not found:
|
|
53
|
+
raise ValueError(
|
|
54
|
+
f"encore: no URL is known for gateway {gateway!r}: the environment gives "
|
|
55
|
+
"it no origins"
|
|
56
|
+
)
|
|
57
|
+
return found
|
|
@@ -15,6 +15,7 @@ import importlib.util
|
|
|
15
15
|
import os
|
|
16
16
|
from pathlib import Path
|
|
17
17
|
import shutil
|
|
18
|
+
import subprocess
|
|
18
19
|
import sys
|
|
19
20
|
import sysconfig
|
|
20
21
|
from typing import TYPE_CHECKING
|
|
@@ -39,7 +40,6 @@ def _runtime_relative_path(gateway: bool | None = None) -> Path:
|
|
|
39
40
|
environment says.
|
|
40
41
|
|
|
41
42
|
A free-threaded build uses its own ABI even when its GIL is enabled.
|
|
42
|
-
There is no gateway variant for Windows.
|
|
43
43
|
|
|
44
44
|
"""
|
|
45
45
|
if gateway is None:
|
|
@@ -69,6 +69,32 @@ def _installed_runtimes() -> Path | None:
|
|
|
69
69
|
return runtimes if runtimes.is_dir() else None
|
|
70
70
|
|
|
71
71
|
|
|
72
|
+
def _component_runtimes(gateway: bool) -> Path | None:
|
|
73
|
+
"""The runtime component's directory, as the ``encore`` command says.
|
|
74
|
+
|
|
75
|
+
For an installation whose runtimes are components: ``encore component
|
|
76
|
+
path --install`` installs ``runtime/python`` (or ``runtime/python-gateway``
|
|
77
|
+
for a gateway program) if it isn't, and prints where it is.
|
|
78
|
+
``ENCORE_BIN``, else the ``encore`` on ``PATH``.
|
|
79
|
+
"""
|
|
80
|
+
encore = os.environ.get("ENCORE_BIN") or shutil.which("encore")
|
|
81
|
+
if not encore:
|
|
82
|
+
return None
|
|
83
|
+
component = "runtime/python-gateway" if gateway else "runtime/python"
|
|
84
|
+
try:
|
|
85
|
+
out = subprocess.run(
|
|
86
|
+
[encore, "component", "path", component, "--install"],
|
|
87
|
+
stdin=subprocess.DEVNULL,
|
|
88
|
+
stdout=subprocess.PIPE,
|
|
89
|
+
text=True,
|
|
90
|
+
check=True,
|
|
91
|
+
).stdout
|
|
92
|
+
except (OSError, subprocess.SubprocessError):
|
|
93
|
+
return None
|
|
94
|
+
lines = out.strip().splitlines()
|
|
95
|
+
return Path(lines[-1]) if lines else None
|
|
96
|
+
|
|
97
|
+
|
|
72
98
|
def _load_native():
|
|
73
99
|
explicit = os.environ.get("ENCORE_PYTHON_RUNTIME_LIB")
|
|
74
100
|
root = os.environ.get("ENCORE_PYTHON_RUNTIME_DIR")
|
|
@@ -97,7 +123,15 @@ def _load_native():
|
|
|
97
123
|
# The Encore installation's, for a process Encore did not start: a
|
|
98
124
|
# test runner's, a script's.
|
|
99
125
|
if path is None and (runtimes := _installed_runtimes()) is not None:
|
|
100
|
-
|
|
126
|
+
candidate = runtimes / "python" / relative
|
|
127
|
+
if candidate.is_file():
|
|
128
|
+
path = candidate
|
|
129
|
+
# Else its runtime component.
|
|
130
|
+
if (
|
|
131
|
+
path is None
|
|
132
|
+
and (component := _component_runtimes(_gateway_program())) is not None
|
|
133
|
+
):
|
|
134
|
+
path = component / relative
|
|
101
135
|
if path is None or not path.is_file():
|
|
102
136
|
gateway = (
|
|
103
137
|
f" This process is a gateway program ({GATEWAY_LISTEN_ENV} is set), "
|
|
@@ -111,7 +145,8 @@ def _load_native():
|
|
|
111
145
|
+ "."
|
|
112
146
|
+ gateway
|
|
113
147
|
+ " Run through the Encore CLI, install Encore so `encore` is on PATH "
|
|
114
|
-
"(
|
|
148
|
+
"(it installs the runtime/python component, or set ENCORE_RUNTIMES_PATH "
|
|
149
|
+
"to its runtimes), set "
|
|
115
150
|
"ENCORE_PYTHON_RUNTIME_DIR to a directory containing the matching ABI, "
|
|
116
151
|
"or set ENCORE_PYTHON_RUNTIME_LIB to the native library. For SDK "
|
|
117
152
|
"development, run "
|