Skip to content

Commit edb93c7

Browse files
committed
fix: do not store handler in function
1 parent 9403646 commit edb93c7

File tree

6 files changed

+58
-29
lines changed

6 files changed

+58
-29
lines changed

scw_serverless/cli.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import click
66
from scaleway import ScalewayException
7-
from scaleway_functions_python import local
87

9-
from scw_serverless import deployment, loader, logger
8+
import scw_serverless
9+
from scw_serverless import app, deployment, loader, local_app, logger
1010
from scw_serverless.dependencies_manager import DependenciesManager
1111
from scw_serverless.gateway.gateway_manager import GatewayManager
1212

@@ -167,23 +167,10 @@ def deploy(
167167
help="Run Flask in debug mode.",
168168
)
169169
def dev(file: Path, port: int, debug: bool) -> None:
170-
"""Run functions locally with Serverless Offline."""
171-
172-
app_instance = loader.load_app_instance(file.resolve())
173-
174-
server = local.LocalFunctionServer()
175-
176-
for function in app_instance.functions:
177-
relative_url, http_methods = None, None
178-
if function.gateway_route:
179-
relative_url = function.gateway_route.relative_url
180-
http_methods = [
181-
method.value for method in function.gateway_route.http_methods or []
182-
]
183-
path = relative_url or ("/" + function.name)
184-
logging.info("Serving function %s on %s", function.name, path)
185-
server.add_handler(
186-
function.handler, relative_url=relative_url, http_methods=http_methods
187-
)
188-
189-
server.serve(port=port, debug=debug)
170+
"""Run functions locally with Serverless Local Testing."""
171+
app.Serverless = local_app.ServerlessLocal
172+
scw_serverless.Serverless = local_app.ServerlessLocal
173+
app_instance = cast(
174+
local_app.ServerlessLocal, loader.load_app_instance(file.resolve())
175+
)
176+
app_instance.local_server.serve(port=port, debug=debug)

scw_serverless/config/function.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Function:
5757
"""Representation of a Scaleway function."""
5858

5959
name: str
60-
handler: Callable
6160
handler_path: str
6261
environment_variables: Optional[dict[str, str]] = None
6362
min_scale: Optional[int] = None
@@ -86,7 +85,6 @@ def from_handler(
8685
gateway_route = GatewayRoute(url, http_methods=args.get("http_methods"))
8786
return Function(
8887
name=to_valid_function_name(handler.__name__),
89-
handler=handler,
9088
handler_path=module_to_path(handler.__module__) + "." + handler.__name__,
9189
environment_variables=args.get("env"),
9290
min_scale=args.get("min_scale"),

scw_serverless/deployment/api_wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def create_function(
8989
max_scale=function.max_scale,
9090
memory_limit=function.memory_limit,
9191
timeout=function.timeout,
92-
handler=function.handler,
92+
handler=function.handler_path,
9393
description=function.description,
9494
secret_environment_variables=self._get_secrets_from_dict(
9595
function.secret_environment_variables
@@ -110,7 +110,7 @@ def update_function(
110110
max_scale=function.max_scale,
111111
memory_limit=function.memory_limit,
112112
timeout=function.timeout,
113-
handler=function.handler,
113+
handler=function.handler_path,
114114
description=function.description,
115115
secret_environment_variables=self._get_secrets_from_dict(
116116
function.secret_environment_variables

scw_serverless/local_app.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Any, Callable
2+
3+
from scaleway_functions_python import local
4+
5+
from scw_serverless.app import Serverless
6+
from scw_serverless.config.function import FunctionKwargs
7+
8+
try:
9+
from typing import Unpack
10+
except ImportError:
11+
from typing_extensions import Unpack
12+
# pylint: disable=wrong-import-position # Conditional import considered a statement
13+
14+
15+
class ServerlessLocal(Serverless):
16+
"""Serverless class that is used when testing locally.
17+
18+
Crate a local testing framework server and inject the handlers to it.
19+
"""
20+
21+
def __init__(
22+
self,
23+
service_name: str,
24+
env: dict[str, Any] | None = None,
25+
secret: dict[str, Any] | None = None,
26+
):
27+
super().__init__(service_name, env, secret)
28+
self.local_server = local.LocalFunctionServer()
29+
30+
def func(
31+
self,
32+
**kwargs: "Unpack[FunctionKwargs]",
33+
) -> Callable:
34+
decorator = super().func(**kwargs)
35+
36+
def _decorator(handler: Callable):
37+
decorator(handler)
38+
http_methods = None
39+
if methods := kwargs.get("http_methods"):
40+
http_methods = [method.value for method in methods]
41+
self.local_server.add_handler(
42+
handler=handler,
43+
relative_url=kwargs.get("relative_url"),
44+
http_methods=http_methods,
45+
)
46+
47+
return _decorator

tests/test_deployment/test_deployment_manager.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_scaleway_api_backend_deploy_function(mocked_responses: responses.Reques
5454
function = Function(
5555
name="test-function",
5656
handler_path="handler",
57-
handler=lambda event, context: "test",
5857
)
5958
backend = get_test_backend()
6059
backend.app_instance.functions = [function]
@@ -139,7 +138,6 @@ def test_scaleway_api_backend_deploy_function_with_trigger(
139138
function = Function(
140139
name="test-function-with-trigger",
141140
handler_path="handler",
142-
handler=lambda event, context: "test",
143141
triggers=[trigger],
144142
)
145143

tests/test_gateway/test_gateway_manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def test_gateway_manager_update_routes(
4242
):
4343
function = Function(
4444
name="test-function",
45-
handler=lambda event, context: "test",
4645
handler_path="handler",
4746
gateway_route=GatewayRoute(
4847
relative_url="/hello", http_methods=[HTTPMethod.GET]

0 commit comments

Comments
 (0)