Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Changed

- Requires `innmind/foundation:~1.9`
- Requires `innmind/http-parser:~3.0`

### Fixed

- PHP `8.4` deprecations
Expand Down
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
},
"require": {
"php": "~8.2",
"innmind/immutable": "~5.7",
"innmind/mantle": "~2.0",
"innmind/io": "~2.7",
"innmind/http-parser": "~2.1",
"innmind/cli": "^3.3",
"innmind/operating-system": "~5.0"
"innmind/foundation": "~1.9",
"innmind/http-parser": "~3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,8 +33,5 @@
"innmind/static-analysis": "^1.2.1",
"innmind/black-box": "~6.5",
"innmind/coding-standard": "~2.0"
},
"provide": {
"innmind/mantle-sources": "1.0"
}
}
26 changes: 16 additions & 10 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
};
use Innmind\CLI\{
Command,
Command\Usage,
Console,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Mantle\Forerunner;
use Innmind\Async\Scheduler;
use Innmind\Http\{
ServerRequest,
Response,
ServerRequest\Environment as HttpEnv,
};
use Innmind\Url\Authority\Port;
use Innmind\IP\IP;
use Innmind\Immutable\Attempt;

final class Serve implements Command
{
Expand All @@ -41,7 +43,7 @@ private function __construct(
}

#[\Override]
public function __invoke(Console $console): Console
public function __invoke(Console $console): Attempt
{
$port = $console
->options()
Expand Down Expand Up @@ -78,32 +80,36 @@ public static function of(
* @psalm-mutation-free
*/
#[\Override]
public function usage(): string
public function usage(): Usage
{
return <<<USAGE
return Usage::parse(<<<USAGE
serve --port= --no-output --allow-anyone

Start an HTTP server

--port is the port on which to expose the server (default: 8080)
--allow-anyone to accept connections coming from outside the machine (default: only local connections are allowed)
USAGE;
USAGE);
}

private function serve(Console $console, Open $open): Console
/**
* @return Attempt<Console>
*/
private function serve(Console $console, Open $open): Attempt
{
$source = Server::of(
$server = Server::of(
$this->os->clock(),
$open,
InjectEnvironment::of(HttpEnv::of($console->variables())),
$this->handle,
);
$forerunner = Forerunner::of($this->os);

if ($console->options()->contains('no-output')) {
$source = $source->withOutput(Nothing::of());
$server = $server->withOutput(Nothing::of());
}

return $forerunner($console, $source);
return Scheduler::of($this->os)
->sink(Attempt::result($console))
->with($server);
}
}
10 changes: 8 additions & 2 deletions src/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
Everything,
};
use Innmind\CLI\Console;
use Innmind\Immutable\Str;
use Innmind\Immutable\{
Attempt,
Str,
};

/**
* @psalm-immutable
Expand All @@ -22,7 +25,10 @@ private function __construct(Output $output)
$this->output = $output;
}

public function __invoke(Console $console, Str $data): Console
/**
* @return Attempt<Console>
*/
public function __invoke(Console $console, Str $data): Attempt
{
return ($this->output)($console, $data);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Display/Everything.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
namespace Innmind\Async\HttpServer\Display;

use Innmind\CLI\Console;
use Innmind\Immutable\Str;
use Innmind\Immutable\{
Attempt,
Str,
};

/**
* @psalm-immutable
Expand All @@ -16,7 +19,7 @@ private function __construct()
}

#[\Override]
public function __invoke(Console $env, Str $data): Console
public function __invoke(Console $env, Str $data): Attempt
{
return $env->output($data);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Display/Nothing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
namespace Innmind\Async\HttpServer\Display;

use Innmind\CLI\Console;
use Innmind\Immutable\Str;
use Innmind\Immutable\{
Attempt,
Str,
};

/**
* @psalm-immutable
Expand All @@ -16,9 +19,9 @@ private function __construct()
}

#[\Override]
public function __invoke(Console $env, Str $data): Console
public function __invoke(Console $env, Str $data): Attempt
{
return $env;
return Attempt::result($env);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Display/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
namespace Innmind\Async\HttpServer\Display;

use Innmind\CLI\Console;
use Innmind\Immutable\Str;
use Innmind\Immutable\{
Attempt,
Str,
};

/**
* @psalm-immutable
*/
interface Output
{
public function __invoke(Console $env, Str $data): Console;
/**
* @return Attempt<Console>
*/
public function __invoke(Console $env, Str $data): Attempt;
}
3 changes: 2 additions & 1 deletion src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
ServerRequest,
Response,
};
use Innmind\Immutable\Attempt;

abstract class Main extends Cli
{
#[\Override]
protected function main(Environment $env, OperatingSystem $os): Environment
protected function main(Environment $env, OperatingSystem $os): Attempt
{
$run = Commands::of(Serve::of($os, static::handle(...)));

Expand Down
35 changes: 22 additions & 13 deletions src/Open.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
namespace Innmind\Async\HttpServer;

use Innmind\OperatingSystem\OperatingSystem;
use Innmind\IO\Sockets\Server;
use Innmind\Socket\Internet\Transport;
use Innmind\IO\Sockets\{
Servers\Server,
Internet\Transport,
};
use Innmind\IP\{
IP,
IPv4,
Expand All @@ -14,6 +16,7 @@
use Innmind\Immutable\{
Sequence,
Maybe,
Predicate\Instance,
};

final class Open
Expand All @@ -34,25 +37,31 @@ private function __construct(Sequence $addresses)
*/
public function __invoke(OperatingSystem $os): Maybe
{
/**
* @psalm-suppress NamedArgumentNotAllowed
* @var Maybe<Server|Server\Pool>
*/
/** @var Server|Server\Pool|null */
$server = null;

return $this
->addresses
->map(static fn($address) => $os->ports()->open(
$address[2],
$address[1],
$address[0],
))
->match(
static fn($server, $rest) => Maybe::all($server, ...$rest->toList())->map(
static fn(Server $server, Server ...$servers) => Sequence::of(...$servers)->reduce(
$server,
static fn(Server|Server\Pool $pool, $server) => $pool->with($server),
),
->sink($server)
->attempt(
static fn($pool, $server) => $server->map(
static fn($server) => match (true) {
\is_null($pool) => $server,
$pool instanceof Server => $pool->pool($server),
default => $pool->with($server),
},
),
)
->maybe()
->keep(
Instance::of(Server::class)->or(
Instance::of(Server\Pool::class),
),
static fn() => Maybe::nothing(),
);
}

Expand Down
Loading
Loading