innmind/async-http-server

3.1.0 2024-10-27 13:57 UTC

This package is auto-updated.

Last update: 2024-11-27 14:13:27 UTC


README

Build Status Type Coverage

Experimental async HTTP server built on top of Fibers.

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.