Skip to content

Commit

Permalink
Add new endpoint & update api-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tklein1801 committed Aug 1, 2021
1 parent 0f389cf commit ddb2d28
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
13 changes: 13 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## Changelogs

### [BBS-Mitfahrzentrale v0.5.1-pre](https://github.com/tklein1801/BBS-Mitfahrzentrale/releases/tag/v0.5.1-pre)

#### Hinzugefügt

- Endpunkt um zu überprüfen ob die Datenbankverbindung erfolgreich war oder nicht

#### Geändert

- Die Anfragemethode wird nun auch bei den API-Logs gespeichert
- Das Datum bei API-Logs wurd nun angezeigt

---

### [BBS-Mitfahrzentrale v0.5-pre](https://github.com/tklein1801/BBS-Mitfahrzentrale/releases/tag/v0.5-pre)

#### Hinzugefügt
Expand Down
6 changes: 6 additions & 0 deletions assets/doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@

---

## database

> Wird verwendet um zu prüfen ob die Datenbankverbindung erfolgreich war. Sollte die Verbindung scheitern wird hier die Fehlermeldung angezeigt. Sollte keine Fehlermeldung angezeigt werden ist davon auszugehen dass die Verbindung erfolgreich war
---

## user

**Registrieren eines neuen Benutzers**
Expand Down
10 changes: 6 additions & 4 deletions assets/php/ApiLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

class ApiLogger
{
public function create(int $responseCode, string $requestedPath, string $requestedIp, $requestedKey)
public function create(int $responseCode, string $requestedPath, string $requestedIp, string $requestMethod, $requestedKey)
{
require get_defined_constants()['CON_PATH'];

$insert = $con->prepare("INSERT INTO `cshare_apiLogs` (`response_code`, `requestedPath`, `requestedIp`, `requestKey`) VALUES (?, ?, ?, ?)");
$insert->bind_param("isss", $responseCode, $requestedPath, $requestedIp, $requestedKey);
$insert = $con->prepare("INSERT INTO `cshare_apiLogs` (`response_code`, `requestedPath`, `requestedIp`, `requestMethod`, `requestKey`) VALUES (?, ?, ?, ?, ?)");
$insert->bind_param("issss", $responseCode, $requestedPath, $requestedIp, $requestMethod, $requestedKey);
$insert->execute();

return $insert;
Expand Down Expand Up @@ -93,6 +93,8 @@ public function createLog($requestedPath)

$HTTP_STATUS_CODE = http_response_code();

$this->create($HTTP_STATUS_CODE, $requestedPath, $clientIp, $apiKey);
$requestMethod = $_SERVER['REQUEST_METHOD'];

$this->create($HTTP_STATUS_CODE, $requestedPath, $clientIp, $requestMethod, $apiKey);
}
}
2 changes: 1 addition & 1 deletion endpoints/sql.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
$con = new mysqli("HOST", "USERNAME", "PASSWORD", "bbs-mitfahrzentrale");
$con = new mysqli("dulliag.de", "db_thorben", "Pizza123", "bbs-mitfahrzentrale-v2");
$con->set_charset("utf8");
if($con->connect_error) {
echo "Connection to database failed. Reason: <br>";
Expand Down
9 changes: 8 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
date_default_timezone_set("Europe/Berlin");

// Application information
define('CURRENT_APP_VERSION', 'v0.5-pre');
define('CURRENT_APP_VERSION', 'v0.5.1-pre');
define('GITHUB', array('author' => 'Thorben Klein',
'user' => 'tklein1801',
'repo' => 'BBS-Mitfahrzentrale'
Expand Down Expand Up @@ -258,6 +258,13 @@ function getApiKey() {
});

// Avaiable endpoints
/**
* Database connection
*/
Route::add($GLOBALS['apiPath'] . "database", function () {
require_once "endpoints/sql.php";
});

/**
* User
*/
Expand Down
17 changes: 16 additions & 1 deletion routes/admin/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
<thead>
<tr>
<th class="text-center">ID</th>
<th>Datum</th>
<th class="text-center">Statuscode</th>
<th>Pfad</th>
<th>Methode</th>
<th class="text-center">Pfad</th>
<th>IP-Adresse</th>
<th colspan="2">API-Key</th>
</tr>
Expand All @@ -59,6 +61,7 @@
if ($LOG_COUNT > 0) {
foreach ($logs as $log) {
$statusCode = $log['response_code'];
$requestMethod = $log['requestMethod'];
if ($statusCode >= 500 && $statusCode < 600) {
$statusBadge = '<span class="badge bg-danger">'.$statusCode.'</span>';
} else if ($statusCode >= 400) {
Expand All @@ -70,12 +73,24 @@
} else {
$statusBadge = '<span class="badge bg-secondary">'.$statusCode.'</span>';
}

if ($requestMethod !== null) {
$requestMethodBadge = '<span class="badge bg-warning">'.$requestMethod.'</span>';
} else {
$requestMethodBadge = '<span class="badge bg-warning">NULL</span>';
}
echo '<tr>
<td class="text-center">
<p class="text-center">'.$log['logId'].'</p>
</td>
<td class="text-center">
'.date("d.m.Y • H:i", strtotime($log['requestedAt'])).'
</td>
<td class="text-center">
'.$statusBadge.'
</td>
<td class="text-center">
'.$requestMethodBadge.'
</td>
<td>
<p>'.$log['requestedPath'].'</p>
Expand Down

0 comments on commit ddb2d28

Please sign in to comment.