jalle19/react-http-static

react/http 的静态网页服务器创建助手

0.1.4 2016-10-28 17:58 UTC

This package is auto-updated.

Last update: 2024-09-13 00:22:23 UTC


README

Build Status Scrutinizer Code Quality Coverage Status

这是一个使用 react/http 创建静态网页服务器的非常基础的助手。它从指定的目录提供文件,并且可以选择配置为需要认证。

要求

  • PHP >= 5.6

安装

composer require jalle19/react-http-static

使用

此示例将在端口 8080 上提供 /var/www 目录。此外,它将要求客户端使用用户名 admin 和密码 admin 进行认证。

<?php

require_once('vendor/autoload.php');

use Jalle19\ReactHttpStatic\Authentication\Handler\Basic as BasicAuthenticationHandler;
use React\Http\Server as HttpServer;
use React\Socket\Server as Socket;

// Create an event loop. You'll probably want to use your own application's loop instead of creating a new one.
$eventLoop = \React\EventLoop\Factory::create();

// Create the socket to use
$socket = new Socket($eventLoop);
$socket->listen(8080);

// Create the server itself
$httpServer      = new HttpServer($socket);
$staticWebServer = new Jalle19\ReactHttpStatic\StaticWebServer($httpServer, '/var/www');

// Apply our authentication handler
$handlerCallback = function ($username, $password) {
    return $username === 'admin' && $password === 'admin';
};

$staticWebServer->setAuthenticationHandler(new BasicAuthenticationHandler('our fancy realm', $handlerCallback));

// Start the loop
$eventLoop->run();

索引处理

默认情况下,对根路径(/)的请求被映射到 index.htmindex.html,优先找到哪个就用哪个。您可以像这样覆盖此行为

$staticWebserver->setIndexFiles([
	'foo.html',
	'bar.html',
]);

认证

您可以通过实现 Jalle19\ReactHttpStatic\Authentication\Handler\HandlerInterface 来编写自己的认证处理器。

请求记录

您可以通过将一个 PSR-3 兼容的记录器传递给服务器的构造函数或通过调用 setLogger() 来记录请求。

许可证

MIT