Initial commit: Retribusi frontend dengan dashboard, event logs, dan settings
This commit is contained in:
76
api/example-endpoint-with-cors.php
Normal file
76
api/example-endpoint-with-cors.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* Contoh Endpoint dengan CORS Handler
|
||||
*
|
||||
* INI ADALAH CONTOH - jangan gunakan langsung, copy logic CORS ke endpoint yang sebenarnya
|
||||
*/
|
||||
|
||||
// ==================== CORS HANDLER - HARUS DI AWAL ====================
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-API-KEY");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
|
||||
// Handle preflight OPTIONS request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
// ==================== END CORS HANDLER ====================
|
||||
|
||||
// Set content type
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Load environment variables (sesuai dengan struktur project)
|
||||
// require_once __DIR__ . '/../vendor/autoload.php'; // Jika pakai Composer
|
||||
// atau load env manual
|
||||
|
||||
// Contoh endpoint: Health Check
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET' && $_SERVER['REQUEST_URI'] === '/health') {
|
||||
echo json_encode([
|
||||
'status' => 'ok',
|
||||
'time' => time()
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Contoh endpoint: Login
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && strpos($_SERVER['REQUEST_URI'], '/auth/v1/login') !== false) {
|
||||
// Parse request body
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validasi
|
||||
if (!isset($input['username']) || !isset($input['password'])) {
|
||||
http_response_code(422);
|
||||
echo json_encode([
|
||||
'error' => 'validation_error',
|
||||
'message' => 'Username and password are required'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// TODO: Implementasi login logic di sini
|
||||
// Contoh response:
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'token' => 'example_token_here',
|
||||
'expires_in' => 3600,
|
||||
'user' => [
|
||||
'id' => 1,
|
||||
'username' => $input['username'],
|
||||
'role' => 'admin'
|
||||
]
|
||||
],
|
||||
'timestamp' => time()
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 404 jika endpoint tidak ditemukan
|
||||
http_response_code(404);
|
||||
echo json_encode([
|
||||
'error' => 'not_found',
|
||||
'message' => 'Endpoint not found'
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user