79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=UTF-8");
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
|
|
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
// Auto-loader for classes (simple version)
|
|
spl_autoload_register(function ($class) {
|
|
$prefix = 'Saqel\\';
|
|
$base_dir = __DIR__ . '/../src/';
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) !== 0) {
|
|
return;
|
|
}
|
|
$relative_class = substr($class, $len);
|
|
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
|
|
if (file_exists($file)) {
|
|
require $file;
|
|
}
|
|
});
|
|
|
|
use Saqel\Router;
|
|
use Saqel\Database;
|
|
|
|
$router = new Router();
|
|
|
|
$router->add('GET', '/', function() {
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => 'Saqel API is running in Pure PHP mode!'
|
|
]);
|
|
});
|
|
|
|
$router->add('GET', '/api/ping', function() {
|
|
$time_start = microtime(true);
|
|
|
|
// Test DB connection
|
|
$db = Database::getInstance()->getConnection();
|
|
$stmt = $db->query("SELECT 1");
|
|
$dbStatus = $stmt ? 'connected' : 'failed';
|
|
|
|
$time_end = microtime(true);
|
|
$execution_time = ($time_end - $time_start) * 1000;
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => 'Saqel Pure PHP Backend is running incredibly fast!',
|
|
'database' => $dbStatus,
|
|
'execution_time_ms' => round($execution_time, 2)
|
|
]);
|
|
});
|
|
|
|
// Example route for user login (Mock)
|
|
$router->add('POST', '/api/auth/login', function() {
|
|
$data = json_decode(file_get_contents("php://input"), true);
|
|
|
|
// In a real app, query Database here and verify password
|
|
if (isset($data['email']) && isset($data['password'])) {
|
|
echo json_encode([
|
|
'access_token' => bin2hex(random_bytes(16)),
|
|
'token_type' => 'Bearer',
|
|
'user' => [
|
|
'email' => $data['email'],
|
|
'role' => 'student'
|
|
]
|
|
]);
|
|
} else {
|
|
header("HTTP/1.0 400 Bad Request");
|
|
echo json_encode(['error' => 'Missing email or password']);
|
|
}
|
|
});
|
|
|
|
$router->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
|