Skip to content

Conversation

@tabuna
Copy link
Member

@tabuna tabuna commented Nov 6, 2022

When using microservices, you may want to use Sajya as an entry gate and pass the request to other servers. This PR solves the "Method not found" problem. A sign that a procedure must handle all requests that contain its name is the presence of an interface Proxy. When no method is found in such a procedure, it will execute the __invoke method by passing the JSON-RPC request itself. For example:

use Sajya\Server\Http\Request;
use Sajya\Server\Procedure;
use Sajya\Server\Proxy;

class FixtureProxyProcedure extends Procedure implements Proxy
{
    /**
     * The name of the procedure that will be
     * displayed and taken into account in the search.
     *
     * @var string
     */
    public static string $name = 'proxy';

    /**
     * The method that will be called if there is no match.
     * 
     * @param \Sajya\Server\Http\Request $request
     *
     * @return mixed
     */
    public function __invoke(Request $request): mixed
    {
        return 'Hello '.$request->getId();
    }
}

After that, we can call any method of this procedure:

curl 'http://127.0.0.1:8000/api/v1/endpoint' --data-binary '{"jsonrpc":"2.0","method":"proxy@ping","id":2}'

And the result is

{"id": "2", "result": "Hello 2", "jsonrpc": "2.0"}

@codecov
Copy link

codecov bot commented Nov 6, 2022

Codecov Report

Base: 98.44% // Head: 98.46% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (02eddb5) compared to base (41950fe).
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head 02eddb5 differs from pull request most recent head f11aced. Consider uploading reports for the commit f11aced to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #47      +/-   ##
============================================
+ Coverage     98.44%   98.46%   +0.02%     
- Complexity      135      137       +2     
============================================
  Files            22       22              
  Lines           449      456       +7     
============================================
+ Hits            442      449       +7     
  Misses            7        7              
Impacted Files Coverage Δ
src/Guide.php 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tabuna tabuna merged commit 5f0c31b into master Nov 6, 2022
@tabuna tabuna deleted the proxy branch November 6, 2022 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants