Contenu connexe Similaire à Speed up web APIs with Expressive and Swoole (PHP Day 2018) (20) Plus de Zend by Rogue Wave Software (20) Speed up web APIs with Expressive and Swoole (PHP Day 2018) 1. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 1/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
SPEEDUPWEBAPISWITHSPEEDUPWEBAPISWITH
EXPRESSIVEEXPRESSIVEANDANDSWOOLESWOOLE
by
Senior Software Engineer
(USA)
, Verona (Italy), 12th May
Enrico Zimuel
Rogue Wave Software
phpDay 2018
2. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 2/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ABOUTMEABOUTME
Developer since 1996
Senior Software Engineer at
Inc.
Core team of ,
and
and international speaker
Research Programmer at
Co-founder of (Italy)
Rogue Wave Software
Apigility
Expressive Zend Framework
TEDx
Amsterdam University
PUG Torino
3. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 3/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXPRESSIVEEXPRESSIVE
4. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 4/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
The PHP framework for middleware applications
PSR-7 support (using )
PSR-15 support
Piping work ow (using )
Features: routing, dependency injection, templating,
error handling
Last release 3.0.6, 16th April 2018
zend-diactoros
zend-stratigility
5. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 5/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ABASICWEBAPIABASICWEBAPI
use ZendDiactorosResponseJsonResponse;
use ZendExpressiveApplication;
$container = require 'config/container.php';
$app = $container->get(Application::class);
$app->pipe('/api/ping', function($request) {
return new JsonResponse(['ack' => time()]);
});
// or $app->pipe('/api/ping', AppHandlerPingHandler::class);
$app->run();
6. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 6/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
REQUESTHANDLERREQUESTHANDLER
use PsrHttpMessageResponseInterface; // PSR-7
use PsrHttpMessageServerRequestInterface; // PSR-7
use PsrHttpServerRequestHandlerInterface; // PSR-15
use ZendDiactorosResponseJsonResponse;
class PingHandler implements RequestHandlerInterface
{
public function handle(
ServerRequestInterface $request
) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}
7. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 7/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MIDDLEWARECLASSMIDDLEWARECLASS
use PsrHttpMessageResponseInterface; // PSR-7
use PsrHttpMessageServerRequestInterface; // PSR-7
use PsrHttpServerMiddlewareInterface; // PSR-15
use PsrHttpServerRequestHandlerInterface; // PSR-15
class AuthMiddleware implements MiddlewareInterface
{
// ...
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
) : ResponseInterface
{
$user = $this->auth->authenticate($request);
if (null !== $user) {
return $handler->handle($request->withAttribute(
UserInterface::class,
$user
8. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 8/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXAMPLE:ROUTINGRESTAPIEXAMPLE:ROUTINGRESTAPI
$app->route('/api/users[/{id}]', [
AuthenticationAuthenticationMiddleware::class,
AuthorizationAuthorizationMiddleware::class,
ApiActionUserAction::class
], ['GET', 'POST', 'PATCH', 'DELETE'], 'api.users');
// or route each HTTP method
$app->get('/api/users[/{id}]', ..., 'api.users.get');
$app->post('/api/users', ..., 'api.users.post');
$app->patch('/api/users/{id}', ..., 'api.users.patch');
$app->delete('/api/users/{id}', ..., 'api.users.delete');
9. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 9/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
QUICKSTARTQUICKSTART
You can start using Expressive with :composer
composer create-project zendframework/zend-expressive-skeleton <dir>
10. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 10/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
LIBRARIESFORAPILIBRARIESFORAPI
HAL-JSON:
Problem details:
Filtering & validation:
Authentication (HTTP Basic, OAuth2):
Authorization (ACL, RBAC):
zend-expressive-hal
zend-problem-details
zend-input lter
zend-expressive-
authentication
zend-expressive-
authorization
11. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 11/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
RESTEXAMPLERESTEXAMPLE
github.com/ezimuel/zend-expressive-api
12. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 12/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
SWOOLESWOOLE
13. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 13/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Swoole is an async programming framework for PHP 7
PHP extension, install:
Released under Apache license 2.0
More info at
pecl install swoole
swoole.co.uk
14. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 14/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
FEATURESFEATURES
Event-driven, asynchronous programming for PHP
Async TCP / UDP / HTTP / Websocket / HTTP2
client/server side API
IPv4 / IPv6 / Unixsocket / TCP/ UDP and SSL / TLS
support
, scalable, support C1000K
Fast serializer / unserializer
Milliseconds task scheduler
High performance
15. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 15/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
HTTPSERVERHTTPSERVER
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on("start", function ($server) {
echo "Started at http://127.0.0.1:9501n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello Worldn");
});
$http->start();
16. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 16/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXPRESSIVEWITHSWOOLEEXPRESSIVEWITHSWOOLE
Use library
Install:
Usage:
wshafer/swoole-expressive
composer require wshafer/swoole-expressive:dev-master
vendor/bin/swoole-expressive --host=0.0.0.0 --port=8080
17. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 17/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
HTTPSERVERFORPSR-7HTTPSERVERFORPSR-7
use SwooleHttpRequest;
use SwooleHttpResponse
$http = new swoole_http_server($host, $port);
$http->on(
'request',
function (Request $request, Response $response) use
($app, $psr7Request, $swooleResponse) {
$psrResponse = $app->handle($psr7Request->from($request));
$swooleResponse->fromPsr7($psrResponse, $response);
}
);
$http->start();
18. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 18/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
SWOOLEVS.NGINXVS.APACHESWOOLEVS.NGINXVS.APACHE
19. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 19/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
BENCHMARKRESULTSBENCHMARKRESULTS
Expressive with Swoole runs 4x faster than nginx and
Apache ( )source
20. 5/14/2018 Speed up web APIs with Expressive and Swoole - phpDay 2018
https://www.zimuel.it/slides/phpday2018/expressive_swoole?print-pdf#/ 20/20
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
THANKS!THANKS!
Rate this talk at
This work is licensed under a
.
I used to make this presentation.
joind.in/talk/bef90
Creative Commons Attribution-ShareAlike 3.0 Unported License
reveal.js