76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Auth\JwtService;
|
|
use App\Config;
|
|
use App\Middleware\AuthMiddleware;
|
|
use App\Middleware\CorsMiddleware;
|
|
use App\Repository\ProductRepository;
|
|
use App\Repository\UserRepository;
|
|
use DI\Container;
|
|
use Slim\Factory\AppFactory;
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
|
|
$dotenv->safeLoad();
|
|
|
|
$container = new Container();
|
|
$container->set(PDO::class, static fn () => Config::pdo());
|
|
$container->set(
|
|
ProductRepository::class,
|
|
static fn ($c) => new ProductRepository($c->get(PDO::class))
|
|
);
|
|
$container->set(
|
|
UserRepository::class,
|
|
static fn ($c) => new UserRepository($c->get(PDO::class))
|
|
);
|
|
$container->set(
|
|
JwtService::class,
|
|
static fn () => new JwtService(
|
|
$_ENV['JWT_SECRET'] ?? '',
|
|
(int) ($_ENV['JWT_TTL'] ?? 86400)
|
|
)
|
|
);
|
|
$container->set(
|
|
AuthMiddleware::class,
|
|
static fn ($c) => new AuthMiddleware(
|
|
$c->get(JwtService::class),
|
|
$c->get(UserRepository::class)
|
|
)
|
|
);
|
|
|
|
AppFactory::setContainer($container);
|
|
$app = AppFactory::create();
|
|
|
|
$basePath = Config::slimBasePath();
|
|
if ($basePath !== '') {
|
|
$app->setBasePath($basePath);
|
|
}
|
|
|
|
$app->addBodyParsingMiddleware();
|
|
$app->addRoutingMiddleware();
|
|
$app->addErrorMiddleware(true, true, true);
|
|
$app->add(new CorsMiddleware());
|
|
|
|
(require __DIR__ . '/../src/Routes/productRoutes.php')($app);
|
|
(require __DIR__ . '/../src/Routes/userRoutes.php')($app);
|
|
|
|
if (empty($_SERVER['HTTP_AUTHORIZATION'])) {
|
|
$redirected = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '';
|
|
|
|
if ($redirected !== '') {
|
|
$_SERVER['HTTP_AUTHORIZATION'] = $redirected;
|
|
} elseif (function_exists('getallheaders')) {
|
|
foreach (getallheaders() as $name => $value) {
|
|
if (strcasecmp((string) $name, 'Authorization') === 0 && is_string($value) && $value !== '') {
|
|
$_SERVER['HTTP_AUTHORIZATION'] = $value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$app->run();
|