Skip to content

Commit

Permalink
Update example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Apr 19, 2022
1 parent c258a32 commit d10a44f
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
# http-server-static-content

This package provides a static content `RequestHandler` for [Amp's HTTP server](https://github.com/amphp/http-server).
This package provides a static content `RequestHandler` implementations for the [AMPHP HTTP server](https://github.com/amphp/http-server).

## Usage

**`DocumentRoot`** implements `RequestHandler`.
**`DocumentRoot`** and **`StaticResource`** implement `RequestHandler`.

## Example

```php
<?php

use Amp\Http\Server\RequestHandler\CallableRequestHandler;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\Router;
use Amp\Http\Server\Server;
use Amp\Http\Server\SocketHttpServer;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Http\Status;

$documentRoot = new DocumentRoot(__DIR__ . '/public');

$router = new Amp\Http\Server\Router;

$router->addRoute('GET', '/', new CallableRequestHandler(function () {
$router->setFallback(new DocumentRoot(__DIR__ . '/public'));
$router->addRoute('GET', '/', new ClosureRequestHandler(function () {
return new Response(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));

$router->setFallback($documentRoot);

$server = new Server(..., $router, ...);
$server->start($router, new DefaultErrorHandler());
```

0 comments on commit d10a44f

Please sign in to comment.