change index.php
This commit is contained in:
parent
b60810080d
commit
f8fb7ecb03
|
|
@ -9,37 +9,26 @@ use Slim\Middleware\ContentLengthMiddleware;
|
||||||
|
|
||||||
require __DIR__ . '/../vendor/autoload.php';
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
// Загрузка переменных окружения
|
// 1. Загружаем переменные окружения через библиотеку Dotenv
|
||||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
// Используем createUnsafeImmutable чтобы иметь возможность перезаписать переменные при необходимости,
|
||||||
$dotenv->safeLoad(); // Используем safeLoad вместо load для избежания ошибок
|
// хотя обычно createImmutable достаточно.
|
||||||
|
// ВАЖНО: Используем load() вместо safeLoad(), чтобы увидеть ошибку, если файл .env не найден.
|
||||||
|
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||||
|
$dotenv->load();
|
||||||
|
|
||||||
// Настройка контейнера
|
// 2. Настройка контейнера
|
||||||
$container = new DI\Container();
|
$container = new DI\Container();
|
||||||
|
|
||||||
// Настройка PDO
|
// 3. Настройка PDO (Убрали весь ручной парсинг файла)
|
||||||
$container->set('db', function () {
|
$container->set('db', function () {
|
||||||
// Загружаем переменные окружения внутри функции для уверенности
|
// Проверяем, загрузились ли переменные (для отладки)
|
||||||
$envPath = __DIR__ . '/../.env';
|
// Если соединение не будет работать, раскомментируй строку ниже и посмотри, что выводит error_log
|
||||||
if (file_exists($envPath)) {
|
// error_log('DB Host: ' . ($_ENV['DB_HOST'] ?? 'not set'));
|
||||||
$lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
||||||
foreach ($lines as $line) {
|
|
||||||
if (strpos($line, '=') !== false && strpos($line, '#') !== 0) {
|
|
||||||
list($key, $value) = explode('=', $line, 2);
|
|
||||||
$key = trim($key);
|
|
||||||
$value = trim($value);
|
|
||||||
if (!isset($_ENV[$key]) && !isset($_SERVER[$key])) {
|
|
||||||
putenv("$key=$value");
|
|
||||||
$_ENV[$key] = $value;
|
|
||||||
$_SERVER[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$host = $_ENV['DB_HOST'] ?? $_SERVER['DB_HOST'] ?? getenv('DB_HOST') ?? 'localhost';
|
$host = $_ENV['DB_HOST'] ?? 'localhost';
|
||||||
$dbname = $_ENV['DB_NAME'] ?? $_SERVER['DB_NAME'] ?? getenv('DB_NAME') ?? 'your_database_name';
|
$dbname = $_ENV['DB_NAME'] ?? 'your_database_name';
|
||||||
$username = $_ENV['DB_USER'] ?? $_SERVER['DB_USER'] ?? getenv('DB_USER') ?? 'your_database_user';
|
$username = $_ENV['DB_USER'] ?? 'your_database_user';
|
||||||
$password = $_ENV['DB_PASS'] ?? $_SERVER['DB_PASS'] ?? getenv('DB_PASS') ?? 'your_secure_password';
|
$password = $_ENV['DB_PASS'] ?? 'your_secure_password';
|
||||||
|
|
||||||
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
|
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
|
||||||
|
|
||||||
|
|
@ -54,48 +43,43 @@ $container->set('db', function () {
|
||||||
|
|
||||||
// Установка контейнера для фабрики приложений
|
// Установка контейнера для фабрики приложений
|
||||||
AppFactory::setContainer($container);
|
AppFactory::setContainer($container);
|
||||||
$app = AppFactory::create();
|
$app = AppFactory::create();
|
||||||
|
|
||||||
// Добавление middleware для определения длины содержимого
|
// Добавление middleware
|
||||||
$app->addBodyParsingMiddleware();
|
$app->addBodyParsingMiddleware();
|
||||||
$app->add(new ContentLengthMiddleware());
|
$app->add(new ContentLengthMiddleware());
|
||||||
|
|
||||||
// Основной маршрут API
|
// Маршруты (без изменений)
|
||||||
$app->get('/api/feeds', [ApiController::class, 'getFeeds']);
|
$app->get('/api/feeds', [ApiController::class, 'getFeeds']);
|
||||||
$app->post('/api/feeds', [ApiController::class, 'registerFeed']);
|
$app->post('/api/feeds', [ApiController::class, 'registerFeed']);
|
||||||
$app->delete('/api/feeds/{id}', [ApiController::class, 'deleteFeed']);
|
$app->delete('/api/feeds/{id}', [ApiController::class, 'deleteFeed']);
|
||||||
$app->get('/api/categories', [ApiController::class, 'getCategories']);
|
$app->get('/api/categories', [ApiController::class, 'getCategories']);
|
||||||
$app->get('/api/tags', [ApiController::class, 'getTags']);
|
$app->get('/api/tags', [ApiController::class, 'getTags']);
|
||||||
|
|
||||||
// Маршруты для владельцев (агентов)
|
$app->post('/api/owners/register', [OwnerController::class, 'registerOwner']);
|
||||||
$app->post('/api/owners/register', [OwnerController::class, 'registerOwner']);
|
$app->get('/api/owners/me', [OwnerController::class, 'getOwnerByApiKey']);
|
||||||
$app->get('/api/owners/me', [OwnerController::class, 'getOwnerByApiKey']);
|
|
||||||
|
|
||||||
// Маршрут для главной страницы
|
$app->get('/', function (Request $request, Response $response) {
|
||||||
$app->get('/', function (Request $request, Response $response) {
|
|
||||||
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/index.html'));
|
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/index.html'));
|
||||||
return $response->withHeader('Content-Type', 'text/html');
|
return $response->withHeader('Content-Type', 'text/html');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Маршрут для страницы лент
|
$app->get('/feeds', function (Request $request, Response $response) {
|
||||||
$app->get('/feeds', function (Request $request, Response $response) {
|
|
||||||
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/feeds.html'));
|
$response->getBody()->write(file_get_contents(__DIR__ . '/../templates/feeds.html'));
|
||||||
return $response->withHeader('Content-Type', 'text/html');
|
return $response->withHeader('Content-Type', 'text/html');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Маршрут для документации скилла
|
$app->get('/SKILL.md', function (Request $request, Response $response) {
|
||||||
$app->get('/SKILL.md', function (Request $request, Response $response) {
|
|
||||||
$response->getBody()->write(file_get_contents(__DIR__ . '/../SKILL.md'));
|
$response->getBody()->write(file_get_contents(__DIR__ . '/../SKILL.md'));
|
||||||
return $response->withHeader('Content-Type', 'text/plain');
|
return $response->withHeader('Content-Type', 'text/plain');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Маршрут для README
|
$app->get('/README.md', function (Request $request, Response $response) {
|
||||||
$app->get('/README.md', function (Request $request, Response $response) {
|
|
||||||
$response->getBody()->write(file_get_contents(__DIR__ . '/../README.md'));
|
$response->getBody()->write(file_get_contents(__DIR__ . '/../README.md'));
|
||||||
return $response->withHeader('Content-Type', 'text/plain');
|
return $response->withHeader('Content-Type', 'text/plain');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Обработка ошибок
|
// Обработка ошибок
|
||||||
$errorMiddleware = $app->addErrorMiddleware($_ENV['APP_DEBUG'] ?? false, true, true);
|
$errorMiddleware = $app->addErrorMiddleware($_ENV['APP_DEBUG'] ?? true, true, true);
|
||||||
|
|
||||||
$app->run();
|
$app->run();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue