Experimental async HTTP server built on top of Fibers.
composer require innmind/async-http-server# server.php
<?php
declare(strict_types=1);
require 'path/to/vendor/autoload.php';
use Innmind\Async\HttpServer\Main;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Http\{
ServerRequest,
Response,
Response\StatusCode,
};
use Innmind\Filesystem\Name;
use Innmind\Url\Path;
use Innmind\Immutable\Map;
new class extends Main {
/**
* @param Map<string, string> $env
*/
protected static function handle(
ServerRequest $request,
OperatingSystem $os,
Map $env,
): Response {
return $os
->filesystem()
->mount(Path::of('somewhere/'))
->unwrap()
->get(Name::of('some-file'))
->match(
static fn($file) => Response::of(
StatusCode::ok,
$request->protocolVersion(),
null,
$file->content(),
),
static fn() => Response::of(
StatusCode::notFound,
$request->protocolVersion(),
),
);
}
};You can run this server via the command php server.php. By default the server is exposed on the port 8080.
This example will return the content of the file somewhere/some-file if it exists on the filesystem otherwise it will respond with a 404 not found.
The asynchronicity of this program is handled by the OperatingSystem abstraction meaning you can write code as if it was synchronous.
Note
you can run php server.php --help to see the options available to configure the server.