innmind / async-http-server
3.1.0
2024-10-27 13:57 UTC
Requires
- php: ~8.2
- innmind/cli: ^3.3
- innmind/http-parser: ~2.1
- innmind/immutable: ~5.7
- innmind/io: ~2.7
- innmind/mantle: ~2.0
- innmind/operating-system: ~5.0
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- vimeo/psalm: ~5.6
Provides
README
Experimental async HTTP server built on top of Fiber
s.
Installation
composer require innmind/async-http-server
Usage
# 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; new class extends Main { protected static function handle(ServerRequest $request, OperatingSystem $os): Response { return $os ->filesystem() ->mount(Path::of('somewhere/')) ->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.