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 App\Repository\DomainRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OpenApi\Generator; use OpenApi\Generator;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire; use function DI\autowire;
@ -45,6 +48,8 @@ use OpenApi\Attributes as OAT;
)] )]
class RequestController class RequestController
{ {
private Logger $log;
//private DatabaseConnection $databaseConnection; //private DatabaseConnection $databaseConnection;
private DomainRepository $domainRepository; private DomainRepository $domainRepository;
private ApikeyRepository $apikeyRepository; private ApikeyRepository $apikeyRepository;
@ -65,9 +70,25 @@ class RequestController
{ {
$this->requestMethod = strtoupper(string: $requestMethod); $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 = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config), 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(); $this->container = $containerBuilder->build();
@ -114,9 +135,7 @@ class RequestController
$domain = [ $domain = [
'id' => $singleDomain->getId(), 'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(), 'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(), 'content' => json_decode(json: $singleDomain->getContent())
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
]; ];
$resultDomain[] = $domain; $resultDomain[] = $domain;
} }
@ -292,6 +311,20 @@ class RequestController
*/ */
public function handleDomainGetRequest(): void 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])) { if (empty($this->uri[3])) {
$this->handleAllDomainsGetRequest(); $this->handleAllDomainsGetRequest();
} else { } else {
@ -299,9 +332,7 @@ class RequestController
$domain = [ $domain = [
'id' => $result->getId(), 'id' => $result->getId(),
'name' => $result->getName(), 'name' => $result->getName(),
'panel_id' => $result->getPanelId(), 'content' => json_decode(json: $result->getContent())
'a' => $result->getA(),
'aaaa' => $result->getAaaa()
]; ];
$this->result = $domain; $this->result = $domain;
} else { } else {
@ -311,17 +342,18 @@ class RequestController
} }
} }
} }
}
/** /**
* @return void * @return void
*/ */
public function handleDomainPostRequest(): void public
function handleDomainPostRequest(): void
{ {
$name = $_POST['name'] ?? ''; $name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0); $panelID = intval(value: $_POST['panel_id'] ?? 0);
$a = $_POST['a'] ?? ''; $content = $_POST['content'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
if (empty($name)) { if (empty($name)) {
$this->header = "400 Bad Request"; $this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
@ -337,7 +369,7 @@ class RequestController
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } 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); $result = $this->domainRepository->insert(domain: $domain);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
@ -363,9 +395,7 @@ class RequestController
} }
$id = $put['id'] ?? 0; $id = $put['id'] ?? 0;
$name = $put['name'] ?? ''; $name = $put['name'] ?? '';
$panelID = $put['panel_id'] ?? ""; $content = $put['content'] ?? "";
$a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? "";
if ($id == 0) { if ($id == 0) {
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
@ -384,7 +414,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } 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->domainRepository->update(domain: $domain);
$this->header = "201 Updated"; $this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
@ -432,4 +462,4 @@ class RequestController
} }
} }
} }