Add new files and make code improvements

This commit is contained in:
2024-03-06 15:14:39 +00:00
parent 113ac2aedd
commit 0a712e1864
55 changed files with 9390 additions and 973 deletions

63
include/bd_pdo_rest.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
require_once "/usr/share/nginx/html/paad/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
use \SeinopSys\PostgresDb;
# Connect to the database
try {
// Postgres
$pdo = new PDO("pgsql:host=" . $_ENV['DB_HOST'] . ";dbname=" . $_ENV['DB_NAME'], $_ENV['DB_USER'], $_ENV['DB_PASS']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db = new PostgresDb();
$db->setConnection($pdo);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// check recursivelly if the array has only empty strings
function is_response_empty($array)
{
foreach ($array as $value) {
if (is_array($value)) {
if (!is_response_empty($value)) {
return false;
}
} else {
if (!empty($value)) {
return false;
}
}
}
return true;
}
// SQL function
function query(string $sql, array $params = null, bool $single = true)
{
global $pdo;
try {
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$response = $single ? $stmt->fetch(PDO::FETCH_ASSOC) : $stmt->fetchAll(PDO::FETCH_ASSOC);
return $response;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
return false;
} finally {
$stmt->closeCursor();
$stmt = null;
}
}
function queryAll(string $sql, array $params = null)
{
return query($sql, $params, false);
}
function toSQLArray(array $array): string
{
return sprintf("{%s}", implode(", ", $array));
}

BIN
include/db/postgrest Normal file

Binary file not shown.

20
include/db/postgrest.conf Normal file
View File

@@ -0,0 +1,20 @@
# postgrest.conf
# The standard connection URI format, documented at
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
db-uri = "postgres://postgres:4ud1t0rf4lt4$$@localhost:5432/paad_pruebas"
# The database role to use when no client authentication is provided.
# Should differ from authenticator
db-anon-role = "postgres"
# The secret to verify the JWT for authenticated requests with.
# Needs to be 32 characters minimum.
jwt-secret = "reallyreallyreallyreallyverysafe"
jwt-secret-is-base64 = false
# Port the postgrest process is listening on for http requests
server-port = 3000
# the location root is /api
server-host = "*"