113 lines
4.4 KiB
PHP
113 lines
4.4 KiB
PHP
<?php
|
||
|
||
use App\Controllers\ApiController;
|
||
use App\Controllers\OwnerController;
|
||
use Psr\Http\Message\ResponseInterface as Response;
|
||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||
use Slim\Factory\AppFactory;
|
||
use Slim\Middleware\ContentLengthMiddleware;
|
||
|
||
require __DIR__ . '/../vendor/autoload.php';
|
||
|
||
// Загружаем переменные окружения
|
||
$envPath = __DIR__ . '/../.env';
|
||
if (file_exists($envPath)) {
|
||
error_log("RSS Hub: .env file found at $envPath");
|
||
} else {
|
||
error_log("RSS Hub: .env file NOT FOUND at $envPath");
|
||
}
|
||
|
||
try {
|
||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||
$dotenv->load();
|
||
error_log("RSS Hub: .env file loaded successfully");
|
||
} catch (Exception $e) {
|
||
error_log("RSS Hub: Error loading .env file: " . $e->getMessage());
|
||
// Продолжаем выполнение, даже если .env не загрузился
|
||
}
|
||
|
||
// Создаем PDO напрямую
|
||
$host = $_ENV['DB_HOST'] ?? $_SERVER['DB_HOST'] ?? getenv('DB_HOST') ?? 'localhost';
|
||
$dbname = $_ENV['DB_NAME'] ?? $_SERVER['DB_NAME'] ?? getenv('DB_NAME') ?? 'your_database_name';
|
||
$username = $_ENV['DB_USER'] ?? $_SERVER['DB_USER'] ?? getenv('DB_USER') ?? 'your_database_user';
|
||
$password = $_ENV['DB_PASS'] ?? $_SERVER['DB_PASS'] ?? getenv('DB_PASS') ?? 'your_secure_password';
|
||
|
||
error_log("RSS Hub DB config - Host: $host, DB: $dbname, User: $username");
|
||
|
||
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
|
||
|
||
$options = [
|
||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||
PDO::ATTR_EMULATE_PREPARES => false,
|
||
];
|
||
|
||
$db = new PDO($dsn, $username, $password, $options);
|
||
|
||
// Настройка контейнера
|
||
$container = new DI\Container();
|
||
|
||
// Регистрируем PDO в контейнере как уже созданный объект
|
||
$container->set('db', $db);
|
||
|
||
// Регистрируем контроллеры с явной зависимостью от PDO
|
||
$container->set(App\Controllers\ApiController::class, function() use ($db) {
|
||
return new App\Controllers\ApiController($db);
|
||
});
|
||
|
||
$container->set(App\Controllers\OwnerController::class, function() use ($db) {
|
||
return new App\Controllers\OwnerController($db);
|
||
});
|
||
|
||
// Регистрируем контроллеры с явным указанием зависимости от PDO
|
||
$container->set(App\Controllers\ApiController::class, function() use ($db) {
|
||
return new App\Controllers\ApiController($db);
|
||
});
|
||
|
||
$container->set(App\Controllers\OwnerController::class, function() use ($db) {
|
||
return new App\Controllers\OwnerController($db);
|
||
});
|
||
|
||
// Установка контейнера для фабрики приложений
|
||
AppFactory::setContainer($container);
|
||
$app = AppFactory::create();
|
||
|
||
// Добавление middleware
|
||
$app->addBodyParsingMiddleware();
|
||
$app->add(new ContentLengthMiddleware());
|
||
|
||
// Маршруты (без изменений)
|
||
$app->get('/api/feeds', [ApiController::class, 'getFeeds']);
|
||
$app->post('/api/feeds', [ApiController::class, 'registerFeed']);
|
||
$app->delete('/api/feeds/{id}', [ApiController::class, 'deleteFeed']);
|
||
$app->get('/api/categories', [ApiController::class, 'getCategories']);
|
||
$app->get('/api/tags', [ApiController::class, 'getTags']);
|
||
|
||
$app->post('/api/owners/register', [OwnerController::class, 'registerOwner']);
|
||
$app->get('/api/owners/me', [OwnerController::class, 'getOwnerByApiKey']);
|
||
|
||
$app->get('/', function (Request $request, Response $response) {
|
||
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/index.html'));
|
||
return $response->withHeader('Content-Type', 'text/html');
|
||
});
|
||
|
||
$app->get('/feeds', function (Request $request, Response $response) {
|
||
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/feeds.html'));
|
||
return $response->withHeader('Content-Type', 'text/html');
|
||
});
|
||
|
||
$app->get('/SKILL.md', function (Request $request, Response $response) {
|
||
$response->getBody()->write(file_get_contents(__DIR__ . '/../SKILL.md'));
|
||
return $response->withHeader('Content-Type', 'text/plain');
|
||
});
|
||
|
||
$app->get('/README.md', function (Request $request, Response $response) {
|
||
$response->getBody()->write(file_get_contents(__DIR__ . '/../README.md'));
|
||
return $response->withHeader('Content-Type', 'text/plain');
|
||
});
|
||
|
||
// Обработка ошибок
|
||
$errorMiddleware = $app->addErrorMiddleware($_ENV['APP_DEBUG'] ?? true, true, true);
|
||
|
||
$app->run();
|