1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/usr/bin/env php <?php require __DIR__.'/../bootstrap/autoload.php'; $appGlobal = require_once __DIR__.'/../bootstrap/app.php'; $swooleConfig = require __DIR__.'/config.php'; require __DIR__.'/Request.php'; require __DIR__.'/Response.php'; $serv = new swoole_http_server(SWOOLE_HTTP_SERVER_HOST, SWOOLE_HTTP_SERVER_PORT); if (!empty($swooleConfig['setting'])) { $serv->set($swooleConfig['setting']); } $serv->on('start', function($swooleHttpServer){ if (!empty(SWOOLE_HTTP_SERVER_LOG_DIR)) { swooleHttpServerLog('swoole_http_server start'); } }); $serv->on('shutdown', function($swooleHttpServer){ if (!empty(SWOOLE_HTTP_SERVER_LOG_DIR)) { swooleHttpServerLog('swoole_http_server shutdown'); } }); $serv->on('request', function($swooleRequest, $swooleResponse) { global $appGlobal; $app = clone $appGlobal; $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = \SwooleAdapter\Request::createWithSwooleRequest($swooleRequest) ); \SwooleAdapter\Response::end($swooleResponse, $response); $kernel->terminate($request, $response); $app->flush(); unset($app); unset($kernel); unset($request); unset($response); }); $serv->start(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php define('SWOOLE_HTTP_SERVER_HOST', '0.0.0.0'); define('SWOOLE_HTTP_SERVER_PORT', 9501); define('SWOOLE_HTTP_SERVER_LOG_DIR', '/tmp/swoole_http_server.log'); function swooleHttpServerLog($content, $dir='') { if (empty($dir) && empty(SWOOLE_HTTP_SERVER_LOG_DIR)) { return; } $dir = !empty($dir) ? $dir : SWOOLE_HTTP_SERVER_LOG_DIR; if (is_array($content)) { $content = var_export($content, true); } elseif (!is_string($content)) { $content = json_encode($content); } $dateTime = date('Y-m-d H:i:s'); file_put_contents($dir, '[' . $dateTime . '] ' . $content . PHP_EOL, FILE_APPEND); } //@see http://wiki.swoole.com/wiki/page/274.html return array( 'setting' => array( 'worker_num' => 1, 'max_request' => 999, 'reactor_num' => 2, ), ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace SwooleAdapter; class Request extends \Illuminate\Http\Request { public static function createWithSwooleRequest($swooleRequest) { $get = isset($swooleRequest->get) ? $swooleRequest->get : []; $post = isset($swooleRequest->post) ? $swooleRequest->post : []; $cookie = isset($swooleRequest->cookie) ? $swooleRequest->cookie : []; $server = isset($swooleRequest->server) ? $swooleRequest->server : []; $header = isset($swooleRequest->header) ? $swooleRequest->header : []; $files = isset($swooleRequest->files) ? $swooleRequest->files : []; $content = $swooleRequest->rawContent() ?: null; return static::create($server['request_uri'], $server['request_method'], array_merge($get, $post), $cookie, $files, $server, $content); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace SwooleAdapter; class Response { public static function end($swooleResponse, $response) { foreach ($response->headers->allPreserveCase() as $key => $value) { foreach ($value as $v) { $swooleResponse->header($key, $v); } } $swooleResponse->status($response->getStatusCode()); foreach ($response->headers->getCookies() as $cookie) { $swooleResponse->cookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); } $swooleResponse->end($response->getContent()); } } |