You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Router\Router;
if (!function_exists('loadRoutes')) {
/**
* 加载路由.
*/
function loadRoutes($path)
{
$files = scandir($path);
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$filePath = $path . '/' . $file;
if (is_dir($filePath)) {
$prefix = '/' . $file;
$options = [];
if (file_exists($filePath . '/_init.php')) {
$config = require($filePath . '/_init.php');
$prefix = $config['prefix'] ?? $prefix;
$options = $config['options'] ?? $options;
}
Router::addGroup($prefix, function () use ($filePath) {
loadRoutes($filePath);
}, $options);
} elseif (is_file($filePath)) {
if ($file == '_init.php') {
continue;
}
require_once $filePath;
}
}
}
}
if (!function_exists('getClientIp')) {
function getClientIp(RequestInterface $request)
{
$headers = $request->getHeaders();
if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
$ips = explode(',', $headers['x-forwarded-for'][0]);
return trim($ips[0]);
} elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
return $headers['x-real-ip'][0];
}
$serverParams = $request->getServerParams();
return $serverParams['remote_addr'] ?? '';
}
}