added annotations

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-01 20:39:46 +01:00
parent 93142c9744
commit e0e6c27c00
1 changed files with 156 additions and 37 deletions

View File

@ -8,12 +8,17 @@ 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 OpenApi\Annotations\ServerVariable;
use OpenApi\Annotations\Tag;
use OpenApi\Generator;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire; use function DI\autowire;
use OpenApi\Attributes as OAT;
/** /**
* *
*/ */
#[OAT\Info(version: '0.0.1', title: 'bindAPI' )]
class RequestController class RequestController
{ {
//private DatabaseConnection $databaseConnection; //private DatabaseConnection $databaseConnection;
@ -46,54 +51,164 @@ class RequestController
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
} }
/** /**
* @OA\Server(
* url = "https://ns2.24unix.net/api"
* )
* @OA\Tag(name = "Server")
* @OA\Get(
* path = "/ping",
* summary = "Returning pong.",
* description = "Can be used to check API or server availability.",
* tags={"Server"},
* @OA\Response(response = "200", description = "OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* security={
* {"Authorization":{"read"}}
* }
* )
*
* @OA\SecurityScheme (name="bindAPISecurity",
* type="apiKey",
* description="description",
* name="X-API-Key",
* in="header",
* securityScheme="Authorization"
*
* )
* @SwaggerDefinition(
* securityDefinition = @SecurityDefinition(
* apiKeyAuthDefinitions = {
* @ApiKeyAuthDefinition(
* key = "X-API-Key", in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-API-KEY"
* )
* }
* )
* )
* @OA\Tag(name = "Domains")
* @OA\Get(
* path="/domains",
* summary="Listing all domains.",
* description="desc",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Post(
* path="/domains",
* summary="Create a domain.",
* description="Creates a new domain.",
* tags={"Domains"},
* @OA\Response(response="201", description="Created"),
* @OA\Response(response = "400", description = "Invalid request body."),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Get(
* path="/domains/{name}",
* summary="Returns a single domain.",
* description="Returns information of a single domain specified by its domain name.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Put(
* path="/domains/{name}",
* summary="Updates a domain.",
* description="Updates a domain. Only supplied fields will be updated, existing won't be affected.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Delete (
* path="/domains/{name}",
* summary="Deletes a domain.",
* description="Deletes a domain.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
*
* @return void * @return void
*/ */
public function processRequest() public function processRequest()
{ {
if (empty($this->uri[2]) || !(($this->uri[2] == 'domains') || $this->uri[2] == 'ping')) { $command = $this->uri[2];
if (empty($command) || !(($command == 'domains') || ($command == 'ping') || ($command == 'apidoc'))) {
$this->header = '404 Not Found'; $this->header = '404 Not Found';
$this->status = "404 Not Found"; $this->status = "404 Not Found";
$this->message = "Endpoint not found."; $this->message = "Endpoint not found.";
} else { } else {
if ($this->checkPassword()) { if ($command == 'apidoc') {
if ($this->uri[2] == "ping") { $openapi = Generator::scan(sources: [__DIR__ . 'RequestController.php']);
$this->header = '200 OK'; $this->status = 'openapi';
$this->status = 'pong'; $this->result[] = $openapi->toJson();
} else { } else {
try { if ($this->checkPassword()) {
match ($this->requestMethod) {
'GET' => $this->handleDomainGetRequest(), if ($this->uri[2] == "ping") {
'POST' => $this->handleDomainPostRequest(), $this->header = '200 OK';
'PUT' => $this->handleDomainPutRequest(), $this->status = 'pong';
'DELETE' => $this->handleDomainDeleteRequest() } else {
}; try {
} catch (UnhandledMatchError) { match ($this->requestMethod) {
$this->header = '400 Bad Request'; 'GET' => $this->handleDomainGetRequest(),
$this->status = '400 Bad Request'; 'POST' => $this->handleDomainPostRequest(),
$this->message = "unknown request method: $this->requestMethod"; 'PUT' => $this->handleDomainPutRequest(),
'DELETE' => $this->handleDomainDeleteRequest()
};
} catch (UnhandledMatchError) {
$this->header = '400 Bad Request';
$this->status = '400 Bad Request';
$this->message = "unknown request method: $this->requestMethod";
}
} }
} }
} }
}
if (!empty($this->header)) {
if (!empty($this->header)) { header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); }
} if (!empty($this->result)) {
if (!empty($this->result)) { if (!empty($this->status) && $this->status == 'openapi') {
echo json_encode(value: $this->result); header(header: 'Content-Type: application/json');
} else { echo $this->result[0];
if (!empty($this->status) && $this->status == 'pong') { } else {
echo json_encode(value: [ echo json_encode(value: $this->result);
'response' => $this->status }
]);
} else { } else {
echo json_encode(value: [ if (!empty($this->status) && $this->status == 'pong') {
'status' => $this->status ?? "Error: No status", echo json_encode(value: [
'message' => $this->message ?? "Error: No message." 'response' => $this->status
]); ]);
} else {
echo json_encode(value: [
'status' => $this->status ?? "Error: No status",
'message' => $this->message ?? "Error: No message."
]);
}
} }
} }
} }
@ -173,7 +288,8 @@ 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);
@ -206,7 +322,8 @@ class RequestController
/** /**
* @return void * @return void
*/ */
public function handleDomainPutRequest(): void public
function handleDomainPutRequest(): void
{ {
$putData = fopen(filename: 'php://input', mode: 'r'); $putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512); $data = fread(stream: $putData, length: 512);
@ -249,10 +366,12 @@ class RequestController
} }
} }
/** /**
* @return void * @return void
*/ */
public function handleDomainDeleteRequest(): void public
function handleDomainDeleteRequest(): void
{ {
$deleteData = fopen(filename: 'php://input', mode: 'r'); $deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512); $data = fread(stream: $deleteData, length: 512);