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();
@ -112,11 +133,9 @@ class RequestController
$resultDomain = []; $resultDomain = [];
foreach ($domains as $singleDomain) { foreach ($domains as $singleDomain) {
$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,16 +311,12 @@ class RequestController
*/ */
public function handleDomainGetRequest(): void public function handleDomainGetRequest(): void
{ {
if (empty($this->uri[3])) { if ($this->uri[3] == 'name') {
$this->handleAllDomainsGetRequest(); if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
} else {
if ($result = $this->domainRepository->findById(id: $this->uri[3])) {
$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 {
@ -309,127 +324,142 @@ class RequestController
$this->status = "404 Not Found "; $this->status = "404 Not Found ";
$this->message = "The specified domain was not found."; $this->message = "The specified domain was not found.";
} }
}
}
/**
* @return void
*/
public function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
$a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else { } else {
if (empty($a) && empty($aaaa) && empty($panelID)) { if (empty($this->uri[3])) {
$this->header = "400 Bad Request"; $this->handleAllDomainsGetRequest();
$this->status = "400 Bad Request";
$this->message = "At least one IP address or panel ID is required.";
} else { } else {
if ($this->domainRepository->findByName(name: $name)) { if ($result = $this->domainRepository->findById(id: $this->uri[3])) {
$this->header = "400 Bad request"; $domain = [
$this->status = "400 Bad request"; 'id' => $result->getId(),
$this->message = "Domain: $name already exists."; 'name' => $result->getName(),
'content' => json_decode(json: $result->getContent())
];
$this->result = $domain;
} else { } else {
$domain = new Domain(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $this->header = "404 Not Found ";
$result = $this->domainRepository->insert(domain: $domain); $this->status = "404 Not Found ";
$this->status = "201 Created"; $this->message = "The specified domain was not found.";
$this->message = $result;
} }
} }
} }
} }
/** /**
* @return void * @return void
*/ */
public public
function handleDomainPutRequest(): void function handleDomainPostRequest(): void
{ {
$putData = fopen(filename: 'php://input', mode: 'r'); $name = $_POST['name'] ?? '';
$data = fread(stream: $putData, length: 512); $panelID = intval(value: $_POST['panel_id'] ?? 0);
$params = explode(separator: '&', string: $data); $content = $_POST['content'] ?? '';
if (empty($name)) {
foreach ($params as $param) { $this->header = "400 Bad Request";
[$key, $value] = explode(separator: '=', string: $param); $this->status = "400 Bad Request";
$put[$key] = $value; $this->message = "A name is required";
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$panelID = $put['panel_id'] ?? "";
$a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? "";
if ($id == 0) {
$this->status = "400 Bad Request";
$this->message = "An ID is required";
} else {
if (!$this->domainRepository->findByID(id: $id)) {
$this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist.";
} else { } else {
// TODO not required, as we rely on the ID if (empty($a) && empty($aaaa) && empty($panelID)) {
if (empty($name)) { $this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "A name is required"; $this->message = "At least one IP address or panel ID is required.";
} else { } else {
if (empty($a) && empty($aaaa)) { if ($this->domainRepository->findByName(name: $name)) {
$this->status = "400 Bad Request"; $this->header = "400 Bad request";
$this->message = "At least one IP address is required."; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else { } else {
$domain = new Domain(name: $panelID, id: $id, panelID: $name, a: $a, aaaa: $aaaa); $domain = new Domain(name: $name, content: $content);
$this->domainRepository->update(domain: $domain); $result = $this->domainRepository->insert(domain: $domain);
$this->header = "201 Updated"; $this->status = "201 Created";
$this->status = "201 Updated"; $this->message = $result;
$this->message = "201 Updated";
} }
} }
} }
} }
}
/** /**
* @return void * @return void
*/ */
public public
function handleDomainDeleteRequest(): void function handleDomainPutRequest(): void
{ {
$deleteData = fopen(filename: 'php://input', mode: 'r'); $putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512); $data = fread(stream: $putData, length: 512);
$params = explode(separator: '&', string: $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param); [$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value; $put[$key] = $value;
} }
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$content = $put['content'] ?? "";
$id = $delete['id'] ?? 0; if ($id == 0) {
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "An ID is required";
} else { } else {
$this->domainRepository->delete(domain: $domain); if (!$this->domainRepository->findByID(id: $id)) {
$this->header = "204 No content."; $this->status = "404 Not Found";
$this->status = "204 No content."; $this->message = "Domain with ID : $id doesn't exist.";
$this->message = "The domain $id has been deleted."; } else {
// TODO not required, as we rely on the ID
if (empty($name)) {
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($a) && empty($aaaa)) {
$this->status = "400 Bad Request";
$this->message = "At least one IP address is required.";
} else {
$domain = new Domain(name: $name, id: $id, content: $content);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated";
$this->status = "201 Updated";
$this->message = "201 Updated";
}
}
}
} }
} }
}
}
/**
* @return void
*/
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0;
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id.";
} else {
$this->domainRepository->delete(domain: $domain);
$this->header = "204 No content.";
$this->status = "204 No content.";
$this->message = "The domain $id has been deleted.";
}
}
}
}