york8/poa-static

Poa static file serving middleware.

0.1.0 2018-02-23 03:14 UTC

This package is not auto-updated.

Last update: 2024-11-17 03:31:43 UTC


README

Latest Version on Packagist Software License Total Downloads

poa-static 是POA框架的一个中间件,用来处理静态文件的请求

作者

安装

composer require york8/poa-static

使用

$app = new Application();

$staticMiddleware = new StaticMiddleware('..');
$staticMiddleware->use(new ProfileMiddleware())->use(new SimpleFilenamePlugin('/static'));

// 定义路由器,将路径 /static 前缀开头的请求交给 StaticMiddleware 处理
$router = new RouterMiddleware(function (Context $context) {
    $context->statusCode(404)->send('Not Found');
});
$router->get('/static', $staticMiddleware);

$app->useErrorMiddleware(function (Throwable $throwable) {
    // 全局未捕获 错误/异常 处理
    fwrite(STDERR, $throwable->getMessage() . "\n");
})
    // 简单请求错误异常处理
    ->use(function (Context $context) {
        try {
            yield;
        } catch (Throwable $exception) {
            fwrite(STDERR, $exception->getMessage() . "\n");
            $context->statusCode(500)->send($exception->getMessage());
        }
    })
    // 使用路由器
    ->use($router);

$app->listen('0.0.0.0:8088');

License

The MIT License (MIT). Please see License File for more information.