phpstreamserver / http-server
Http Server plugin for PHPStreamServer
v0.3.0
2024-12-06 17:26 UTC
Requires
- php: >=8.2
- ext-pcntl: *
- ext-posix: *
- amphp/amp: ^3.0.2
- amphp/http-server: ^3.3.1
- amphp/socket: ^2.3.1
- phpstreamserver/core: ^0.3
- psr/http-message: ^2.0
- psr/log: ^3.0
- revolt/event-loop: ^1.0.6
README
HTTP Server Plugin for PHPStreamServer
The HTTP Server Plugin for PHPStreamServer extends the core functionality by providing a high performance, asynchronous HTTP server. It works in the event loop and always persists in memory, enabling fast request handling and reducing startup overhead.
Features:
- Non-blocking, high concurrency request handling.
- Protocol support: HTTP/1.1, HTTP/2.
- HTTPS encrypted connections.
- GZIP compression.
- Serve static files: Can serve files from a directory, making it easy to host static assets like HTML, CSS, JavaScript, and images.
- Middleware support: Integrates middleware for flexible request/response processing.
Install
$ composer require phpstreamserver/core phpstreamserver/http-server
Configure
Here is an example of a simple HTTP server configuration.
// server.php use Amp\Http\Server\HttpErrorException; use Amp\Http\Server\Request; use Amp\Http\Server\Response; use PHPStreamServer\Core\Server; use PHPStreamServer\Plugin\HttpServer\HttpServerPlugin; use PHPStreamServer\Plugin\HttpServer\HttpServerProcess; $server = new Server(); $server->addPlugin( new HttpServerPlugin(), ); $server->addWorker( new HttpServerProcess( name: 'Web Server', count: 4, listen: '0.0.0.0:8080', onStart: function (HttpServerProcess $worker): void { // initialization }, onRequest: function (Request $request, HttpServerProcess $worker): Response { return match ($request->getUri()->getPath()) { '/' => new Response(body: 'Hello world'), '/ping' => new Response(body: 'pong'), default => throw new HttpErrorException(404), }; } ), ); exit($server->run());
Run
$ php server.php start