added logger & /name path

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-12 19:27:38 +01:00
parent 480033d0d5
commit 9271752843
1 changed files with 143 additions and 113 deletions

View File

@ -9,6 +9,9 @@ use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository;
use DI\Container;
use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OpenApi\Generator;
use UnhandledMatchError;
use function DI\autowire;
@ -45,6 +48,8 @@ use OpenApi\Attributes as OAT;
)]
class RequestController
{
private Logger $log;
//private DatabaseConnection $databaseConnection;
private DomainRepository $domainRepository;
private ApikeyRepository $apikeyRepository;
@ -65,9 +70,25 @@ class RequestController
{
$this->requestMethod = strtoupper(string: $requestMethod);
$dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log');
$stream->setFormatter(formatter: $formatter);
$this->log = new Logger(name: 'bindAPI');
$this->log->pushHandler(handler: $stream);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log),
]);
$this->container = $containerBuilder->build();
@ -114,9 +135,7 @@ class RequestController
$domain = [
'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(),
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
'content' => json_decode(json: $singleDomain->getContent())
];
$resultDomain[] = $domain;
}
@ -292,6 +311,20 @@ class RequestController
*/
public function handleDomainGetRequest(): void
{
if ($this->uri[3] == 'name') {
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'content' => json_decode(json: $result->getContent())
];
$this->result = $domain;
} else {
$this->header = "404 Not Found ";
$this->status = "404 Not Found ";
$this->message = "The specified domain was not found.";
}
} else {
if (empty($this->uri[3])) {
$this->handleAllDomainsGetRequest();
} else {
@ -299,9 +332,7 @@ class RequestController
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'panel_id' => $result->getPanelId(),
'a' => $result->getA(),
'aaaa' => $result->getAaaa()
'content' => json_decode(json: $result->getContent())
];
$this->result = $domain;
} else {
@ -311,17 +342,18 @@ class RequestController
}
}
}
}
/**
* @return void
*/
public function handleDomainPostRequest(): void
public
function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
$a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
$content = $_POST['content'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
@ -337,7 +369,7 @@ class RequestController
$this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else {
$domain = new Domain(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
$domain = new Domain(name: $name, content: $content);
$result = $this->domainRepository->insert(domain: $domain);
$this->status = "201 Created";
$this->message = $result;
@ -363,9 +395,7 @@ class RequestController
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$panelID = $put['panel_id'] ?? "";
$a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? "";
$content = $put['content'] ?? "";
if ($id == 0) {
$this->status = "400 Bad Request";
@ -384,7 +414,7 @@ class RequestController
$this->status = "400 Bad Request";
$this->message = "At least one IP address is required.";
} else {
$domain = new Domain(name: $panelID, id: $id, panelID: $name, a: $a, aaaa: $aaaa);
$domain = new Domain(name: $name, id: $id, content: $content);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated";
$this->status = "201 Updated";
@ -432,4 +462,4 @@ class RequestController
}
}
}
}