first commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// config/db.php — Database connection (PDO)
|
||||
// ============================================================
|
||||
|
||||
define('DB_HOST', '127.0.0.1'); // ← ganti dari localhost ke 127.0.0.1
|
||||
define('DB_NAME', 'webgis_db'); // ← ganti dari webgis_db ke webgis
|
||||
define('DB_USER', 'root');
|
||||
define('DB_PASS', 'root123');
|
||||
define('DB_CHARSET', 'utf8mb4');
|
||||
|
||||
function getDB(): PDO {
|
||||
static $pdo = null;
|
||||
if ($pdo === null) {
|
||||
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=%s', DB_HOST, DB_NAME, DB_CHARSET);
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
try {
|
||||
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
return $pdo;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// config/helpers.php — Shared utility functions
|
||||
// ============================================================
|
||||
|
||||
/**
|
||||
* Set JSON response headers and output data.
|
||||
*/
|
||||
function jsonResponse(mixed $data, int $status = 200): void {
|
||||
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');
|
||||
http_response_code($status);
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return decoded JSON body from the request.
|
||||
*/
|
||||
function getRequestBody(): array {
|
||||
$raw = file_get_contents('php://input');
|
||||
$data = json_decode($raw, true);
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the HTTP request method.
|
||||
*/
|
||||
function getMethod(): string {
|
||||
return strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle CORS preflight.
|
||||
*/
|
||||
function handlePreflight(): void {
|
||||
if (getMethod() === 'OPTIONS') {
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
http_response_code(204);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user