From 6ca42cbf627990c1d448d67143d2992d2575c8df Mon Sep 17 00:00:00 2001 From: hide_d Date: Sun, 12 Sep 2021 15:05:21 +0900 Subject: [PATCH] =?UTF-8?q?api:=20gateway,=20ingame=20=EB=AA=A8=EB=91=90?= =?UTF-8?q?=20=EA=B0=80=EB=8A=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.php | 8 ++++ hwe/api.php | 84 +---------------------------------- hwe/func_converter.php | 4 +- hwe/sammo/BaseAPI.php | 4 +- src/sammo/APIHelper.php | 98 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 86 deletions(-) create mode 100644 api.php create mode 100644 src/sammo/APIHelper.php diff --git a/api.php b/api.php new file mode 100644 index 00000000..a6d67bae --- /dev/null +++ b/api.php @@ -0,0 +1,8 @@ +getMessage()); -} - -if (!key_exists('path', $input)) { - Json::dieWithReason('path가 지정되지 않았습니다.'); -} - -if (key_exists('args', $input) && !is_array($input['args'])) { - Json::dieWithReason('args가 array가 아닙니다.' . gettype($input['args'])); -} - -try { - $obj = buildAPIExecutorClass($input['path'], $input['args'] ?? []); - $api = - $validateResult = $obj->validateArgs(); - if ($validateResult !== null) { - Json::dieWithReason($validateResult); - } - - $sessionMode = $obj->getRequiredSessionMode(); - if ($sessionMode === BaseAPI::NO_SESSION) { - $session = null; - } else { - if ($sessionMode & BaseAPI::REQ_GAME_LOGIN) { - $session = Session::requireGameLogin(); - } else if ($sessionMode & BaseAPI::REQ_LOGIN) { - $session = Session::requireLogin(); - } else { - Json::dieWithReason("올바르지 않은 SessionMode: {$sessionMode}"); - } - - if ($sessionMode & BaseAPI::REQ_READ_ONLY) { - $session->setReadOnly(); - } - } - - $modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) - ? new \DateTimeImmutable($_SERVER['HTTP_IF_MODIFIED_SINCE'], new \DateTimeZone("UTC")) - : null; - $reqEtags = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : null; - - $result = $obj->launch($session, $modifiedSince, $reqEtags); - if (is_string($result)) { - Json::dieWithReason($result); - } - - $cacheResult = $obj->tryCache(); - if ($cacheResult !== null) { - /** @var \DateTimeInterface $lastModified */ - [$lastModified, $etag] = $cacheResult; - - if ($lastModified !== null) { - header("Last-Modified: " . gmdate("D, d M Y H:i:s", TimeUtil::DateTimeToSeconds($lastModified, true)) . " GMT"); - } - if ($etag !== null) { - header("Etag: $etag"); - } - - if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) { - header("HTTP/1.1 304 Not Modified"); - die(); - } - if ($reqEtags !== null && $reqEtags === $etag) { - header("HTTP/1.1 304 Not Modified"); - die(); - } - } - - if ($result === null) { - Json::die([ - 'result' => true, - 'reason' => 'success' - ], $cacheResult === null ? Json::NO_CACHE : 0); - } - Json::die($result, $cacheResult === null ? Json::NO_CACHE : 0); -} catch (\Exception $e) { - Json::dieWithReason($e->getMessage()); -} catch (mixed $e) { - Json::dieWithReason($e); -} +APIHelper::launch(dirname(__FILE__)); \ No newline at end of file diff --git a/hwe/func_converter.php b/hwe/func_converter.php index 9508b13a..f8d80927 100644 --- a/hwe/func_converter.php +++ b/hwe/func_converter.php @@ -377,9 +377,9 @@ function getAPIExecutorClass($path){ throw new \InvalidArgumentException("{$path}는 올바른 API 경로가 아님"); } -function buildAPIExecutorClass(string $type, array $args):\sammo\BaseAPI{ +function buildAPIExecutorClass(string $type, string $rootPath, array $args):\sammo\BaseAPI{ $class = getAPIExecutorClass($type); - return new $class($args); + return new $class($rootPath, $args); } function getWarUnitTriggerClass(string $type){ diff --git a/hwe/sammo/BaseAPI.php b/hwe/sammo/BaseAPI.php index 32f6d512..c730b5f2 100644 --- a/hwe/sammo/BaseAPI.php +++ b/hwe/sammo/BaseAPI.php @@ -10,8 +10,10 @@ abstract class BaseAPI const REQ_READ_ONLY = 4; protected array $args; - public function __construct(array $args) + protected string $rootPath; + public function __construct(string $rootPath, array $args) { + $this->rootPath = $rootPath; $this->args = $args; } abstract public function getRequiredSessionMode(): int; diff --git a/src/sammo/APIHelper.php b/src/sammo/APIHelper.php new file mode 100644 index 00000000..8ad48b23 --- /dev/null +++ b/src/sammo/APIHelper.php @@ -0,0 +1,98 @@ +getMessage()); + } + + if (!key_exists('path', $input)) { + Json::dieWithReason('path가 지정되지 않았습니다.'); + } + + if (key_exists('args', $input) && !is_array($input['args'])) { + Json::dieWithReason('args가 array가 아닙니다.' . gettype($input['args'])); + } + + try { + $obj = buildAPIExecutorClass($input['path'], $rootPath, $input['args'] ?? []); + $api = + $validateResult = $obj->validateArgs(); + if ($validateResult !== null) { + Json::dieWithReason($validateResult); + } + + $sessionMode = $obj->getRequiredSessionMode(); + if ($sessionMode === BaseAPI::NO_SESSION) { + $session = null; + } else { + if ($sessionMode & BaseAPI::REQ_GAME_LOGIN) { + $session = Session::requireGameLogin(); + } else if ($sessionMode & BaseAPI::REQ_LOGIN) { + $session = Session::requireLogin(); + } else { + Json::dieWithReason("올바르지 않은 SessionMode: {$sessionMode}"); + } + + if ($sessionMode & BaseAPI::REQ_READ_ONLY) { + $session->setReadOnly(); + } + } + + $modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) + ? new \DateTimeImmutable($_SERVER['HTTP_IF_MODIFIED_SINCE'], new \DateTimeZone("UTC")) + : null; + $reqEtags = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : null; + + $result = $obj->launch($session, $modifiedSince, $reqEtags); + if (is_string($result)) { + Json::dieWithReason($result); + } + + $cacheResult = $obj->tryCache(); + if ($cacheResult !== null) { + /** @var \DateTimeInterface $lastModified */ + [$lastModified, $etag] = $cacheResult; + + if ($lastModified !== null) { + header("Last-Modified: " . gmdate("D, d M Y H:i:s", TimeUtil::DateTimeToSeconds($lastModified, true)) . " GMT"); + } + if ($etag !== null) { + header("Etag: $etag"); + } + + if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) { + header("HTTP/1.1 304 Not Modified"); + die(); + } + if ($reqEtags !== null && $reqEtags === $etag) { + header("HTTP/1.1 304 Not Modified"); + die(); + } + } + + if ($result === null) { + Json::die([ + 'result' => true, + 'reason' => 'success' + ], $cacheResult === null ? Json::NO_CACHE : 0); + } + Json::die($result, $cacheResult === null ? Json::NO_CACHE : 0); + } catch (\Exception $e) { + Json::dieWithReason($e->getMessage()); + } catch (mixed $e) { + Json::dieWithReason($e); + } + } +}