frontend and backend
This commit is contained in:
0
backend/src/Controller/.gitignore
vendored
Normal file
0
backend/src/Controller/.gitignore
vendored
Normal file
19
backend/src/Controller/DencodeController.php
Normal file
19
backend/src/Controller/DencodeController.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class DencodeController extends AbstractController
|
||||
{
|
||||
#[Route('/api/dencode', name: 'api_dencode', methods: "GET")]
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->json([
|
||||
'message' => 'Welcome to your new controller!',
|
||||
'path' => 'src/Controller/DencodeController.php',
|
||||
]);
|
||||
}
|
||||
}
|
20
backend/src/Controller/ErrorController.php
Normal file
20
backend/src/Controller/ErrorController.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Exceptions will end here.
|
||||
*/
|
||||
class ErrorController extends AbstractController
|
||||
{
|
||||
public function show(\Throwable $exception): Response
|
||||
{
|
||||
//return $this->json(['fatal' => sprintf('%s: %s [%s:%d]', $exception::class, $exception->getMessage(), basename($exception->getFile()), $exception->getLine())]);
|
||||
return $this->json(['fatal' => sprintf('%s: %s', $exception::class, $exception->getMessage())]);
|
||||
}
|
||||
}
|
55
backend/src/Controller/PregController.php
Normal file
55
backend/src/Controller/PregController.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\PregDTO;
|
||||
use App\Service\Preg;
|
||||
use Exception;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* PREGs controller.
|
||||
*/
|
||||
class PregController extends AbstractController
|
||||
{
|
||||
private const STORE_KEY = 'xrg.es2022:%s';
|
||||
private const STORE_TTL = 60*60*24*7;
|
||||
|
||||
#[Route('/api/preg', name: 'api_preg_post', methods: ["POST"])]
|
||||
public function index(Request $request, SerializerInterface $serializer, \Redis $redis): Response
|
||||
{
|
||||
$dto = $serializer->deserialize($request->getContent(), PregDTO::class, 'json');
|
||||
|
||||
if ($dto->pattern === '') {
|
||||
throw new Exception('Empty regular expression');
|
||||
}
|
||||
|
||||
$preg = new Preg($dto);
|
||||
$result = $preg->exec();
|
||||
$key = $preg->getHash();
|
||||
|
||||
$redis->setex(sprintf(self::STORE_KEY, $key), self::STORE_TTL, $serializer->serialize($dto, 'json'));
|
||||
|
||||
return $this->json(array_merge((array)$result, ['hash' => $key]));
|
||||
}
|
||||
|
||||
#[Route('/api/preg', name: 'api_preg_hash', methods: ["GET"])]
|
||||
public function hash(Request $request, \Redis $redis): Response
|
||||
{
|
||||
$json = $redis->get(sprintf(self::STORE_KEY, $request->query->get('hash')));
|
||||
|
||||
if ($json === false) {
|
||||
throw new NotFoundHttpException('Key not found.');
|
||||
}
|
||||
|
||||
return JsonResponse::fromJsonString($json);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user