index.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use FrameworkX\App;
  4. use Middlewares\JwtAuthMiddleware;
  5. $requestUri = $_SERVER['REQUEST_URI'] ?? null;
  6. $path = $requestUri !== null ? parse_url($requestUri, PHP_URL_PATH) : '/';
  7. $file = __DIR__ . $path;
  8. if (php_sapi_name() === 'cli-server' && is_file($file)) {
  9. return false;
  10. }
  11. if (class_exists(Dotenv\Dotenv::class) && file_exists(__DIR__ . '/../.env')) {
  12. Dotenv\Dotenv::createImmutable(
  13. dirname(__DIR__),
  14. null,
  15. true
  16. )->safeLoad();
  17. }
  18. error_reporting(E_ALL);
  19. $dsn = $_ENV['DB_DSN'] ?? (function () {
  20. $host = $_ENV['DB_HOST'] ?? 'localhost';
  21. $port = $_ENV['DB_PORT'] ?? '5432';
  22. $name = $_ENV['DB_NAME'] ?? 'postgres';
  23. return "pgsql:host={$host};port={$port};dbname={$name}";
  24. })();
  25. $dbUser = $_ENV['DB_USER'] ?? 'postgres';
  26. $dbPass = $_ENV['DB_PASSWORD'] ?? '';
  27. $GLOBALS['pdo'] = new \PDO($dsn, $dbUser, $dbPass);
  28. $GLOBALS['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  29. $app = new App();
  30. $authJwt = new JwtAuthMiddleware();
  31. $app->get('/jwthelloworld', $authJwt,\Controllers\HelloController::class);
  32. $app->post('/login', \Controllers\LoginController::class);
  33. $app->post('/register', $authJwt, \Controllers\RegisterController::class);
  34. $app->post('/user/get', $authJwt, \Controllers\UserGetController::class);
  35. $app->post('/user/delete', $authJwt, \Controllers\UserDeleteController::class);
  36. // Public endpoint to create company, user, and wallet in a single transaction
  37. $app->post('/company/user/create', \Controllers\CompanyWithUserController::class);
  38. // Authenticated user profile updates
  39. $app->post('/user/change-email', $authJwt, \Controllers\UserChangeEmailController::class);
  40. $app->post('/user/change-password', $authJwt, \Controllers\UserChangePasswordController::class);
  41. // Commodities (JWT-protected)
  42. $app->post('/commodity/create', $authJwt, \Controllers\CommodityCreateController::class);
  43. $app->post('/commodity/update', $authJwt, \Controllers\CommodityUpdateController::class);
  44. $app->post('/commodity/delete', $authJwt, \Controllers\CommodityDeleteController::class);
  45. $app->get('/commodities', $authJwt, \Controllers\CommoditiesGetController::class);
  46. // CPR registration
  47. $app->post('/cpr/create', $authJwt, \Controllers\RegisterCprController::class);
  48. $app->run();