Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run lambda HTTP APIs via xp web lambda [class] #11

Merged
merged 4 commits into from
Nov 20, 2023
Merged

Conversation

thekid
Copy link
Member

@thekid thekid commented Nov 18, 2023

This pull request makes it possible to run HTTP APIs locally via xp web, improving the developer experience.

use com\amazon\aws\lambda\HttpApi;

class Greet extends HttpApi {

  /**
   * Returns routes
   *
   * @param  web.Environment $env
   * @return web.Application|web.Routing|[:var]
   */
  public function routes($env) {
    return ['/' => function($req, $res) {
      $greeting= sprintf(
        'Hello %s from PHP %s on stage %s @ %s',
        $req->param('name') ?? $req->header('User-Agent') ?? 'Guest',
        PHP_VERSION,
        $req->value('request')->stage,
        $req->value('context')->region
      );

      $res->answer(200);
      $res->send($greeting, 'text/plain');
    }];
  }
}

Run via:

$ xp web lambda Greet
@xp.web.srv.Standalone(HTTP @ peer.ServerSocket(Resource id #116 -> tcp://127.0.0.1:8080))
Serving prod:xp.lambda.Web<Greet>[] > web.logging.ToConsole
════════════════════════════════════════════════════════════════════════
> Server started: http://localhost:8080 in 0.046 seconds
  Mon, 20 Nov 2023 19:57:35 +0100 - PID 16340; press Ctrl+C to exit

# ...

See also xp-forge/lambda#21

@thekid thekid added the enhancement New feature or request label Nov 18, 2023
@thekid
Copy link
Member Author

thekid commented Nov 18, 2023

xp web com.amazon.aws.lambda.Ws Greet is quite long, how about:

  • Shortening the class name: xp web xp.lambda.Ws Greet using the xp namespace used for commands
  • Introducing @[name]: xp web @lambda Greet as a shorthand for xp.[name].App
  • Extending the lambda subcommand: xp lambda ws Greet by mapping ws to xp.lambda.WsCommand

@thekid
Copy link
Member Author

thekid commented Nov 20, 2023

Introducing @[name]: xp web @lambda Greet as a shorthand for xp.[name].App

We don't even need the @ prefix, we can simply use the convention that PHP types start with an uppercase character:

  • xp web lambda loads the class web xp.lambda.Web
  • xp web Lambda loads the class Lambda

Implementation:

diff --git a/src/main/php/xp/web/Source.class.php b/src/main/php/xp/web/Source.class.php
index ded3208..a66d1a5 100755
--- a/src/main/php/xp/web/Source.class.php
+++ b/src/main/php/xp/web/Source.class.php
@@ -24,7 +24,13 @@ class Source {
 
     $cl= ClassLoader::getDefault();
     try {
-      $class= is_file($application) ? $cl->loadUri($application) : $cl->loadClass($application);
+      if (is_file($application)) {
+        $class= $cl->loadUri($application);
+      } else if ($application[0] < 'a' || false !== strpos($application, '.')) {
+        $class= $cl->loadClass($application);
+      } else {
+        $class= $cl->loadClass("xp.{$application}.Web");
+      }
     } catch (ClassLoadingException $e) {
       throw new IllegalArgumentException('Cannot load class '.$application, $e);
     }

@thekid thekid changed the title Run lambda HTTP APIs via xp web com.amazon.aws.lambda.Ws [class] Run lambda HTTP APIs via xp web lambda [class] Nov 20, 2023
@thekid thekid merged commit 39cbe7b into main Nov 20, 2023
20 checks passed
@thekid thekid deleted the feature/xp-web branch November 20, 2023 19:02
@thekid
Copy link
Member Author

thekid commented Nov 20, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant