| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- declare(strict_types=1);
- use App\Db\Connection;
- use App\Db\Migrator;
- use App\Http\Request;
- use App\Http\Response;
- use App\Http\Router;
- use App\Http\View;
- define('APP_ROOT', dirname(__DIR__));
- // ---------------------------------------------------------------------------
- // Autoload
- // ---------------------------------------------------------------------------
- $autoload = APP_ROOT . '/vendor/autoload.php';
- if (!is_file($autoload)) {
- http_response_code(500);
- header('Content-Type: text/plain; charset=utf-8');
- echo "Composer dependencies are not installed.\n";
- echo "Run: composer install (or rebuild the container).\n";
- exit;
- }
- require $autoload;
- // ---------------------------------------------------------------------------
- // Environment
- // ---------------------------------------------------------------------------
- if (is_file(APP_ROOT . '/.env')) {
- $dotenv = Dotenv\Dotenv::createImmutable(APP_ROOT);
- $dotenv->safeLoad();
- }
- $appEnv = getenv('APP_ENV') ?: 'production';
- if ($appEnv !== 'production') {
- ini_set('display_errors', '1');
- error_reporting(E_ALL);
- } else {
- ini_set('display_errors', '0');
- }
- // ---------------------------------------------------------------------------
- // Migrations — cheap no-op when already current
- // ---------------------------------------------------------------------------
- try {
- $pdo = Connection::pdo();
- (new Migrator($pdo))->migrate();
- } catch (\Throwable $e) {
- http_response_code(500);
- header('Content-Type: text/plain; charset=utf-8');
- echo "Database bootstrap failed.\n";
- if ($appEnv !== 'production') {
- echo $e->getMessage() . "\n";
- }
- exit;
- }
- // ---------------------------------------------------------------------------
- // Routing
- // ---------------------------------------------------------------------------
- $view = new View(APP_ROOT . '/views');
- $router = new Router();
- $router->get('/', function (Request $req) use ($view, $pdo): Response {
- $version = (int) $pdo->query('SELECT COALESCE(MAX(version), 0) FROM schema_version')->fetchColumn();
- return Response::html($view->render('home', [
- 'title' => 'Sprint Planner',
- 'schemaVersion' => $version,
- 'dbPath' => Connection::path(),
- 'appEnv' => $appEnv,
- ]));
- });
- $router->get('/healthz', function (): Response {
- return Response::text('ok');
- });
- // ---------------------------------------------------------------------------
- // Dispatch
- // ---------------------------------------------------------------------------
- $request = Request::fromGlobals();
- $response = $router->dispatch($request);
- $response->send();
|