From c42a97bb5fe2fc06387bce9df3122f0273817efd Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 9 Mar 2024 16:16:12 +0000 Subject: [PATCH] feat: api/daemon/{start,stop,restart,status} --- @sammo/api_def/src/index.ts | 14 +++++++---- @sammo/server/src/api/daemon/index.ts | 13 +++++++++++ @sammo/server/src/api/daemon/restart.ts | 24 +++++++++++++++++++ @sammo/server/src/api/daemon/start.ts | 31 +++++++++++++++++++++++++ @sammo/server/src/api/daemon/status.ts | 25 ++++++++++++++++++++ @sammo/server/src/api/daemon/stop.ts | 31 +++++++++++++++++++++++++ @sammo/server/src/api/index.ts | 3 ++- 7 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 @sammo/server/src/api/daemon/index.ts create mode 100644 @sammo/server/src/api/daemon/restart.ts create mode 100644 @sammo/server/src/api/daemon/start.ts create mode 100644 @sammo/server/src/api/daemon/status.ts create mode 100644 @sammo/server/src/api/daemon/stop.ts diff --git a/@sammo/api_def/src/index.ts b/@sammo/api_def/src/index.ts index e85e954..6d9436a 100644 --- a/@sammo/api_def/src/index.ts +++ b/@sammo/api_def/src/index.ts @@ -1,5 +1,5 @@ import type { - DefAPINamespace, + DefAPINamespace, } from "@strpc/def"; import { GET, POST } from '@strpc/def'; @@ -17,11 +17,17 @@ import type { export type { - InferResponse, - InferError, - InferQuery, + InferResponse, + InferError, + InferQuery, } from "@strpc/def"; export type * as def from './def/index.js'; export const structure = { + daemon: { + status: GET(), + start: POST(), + stop: POST(), + restart: POST(), + } } satisfies DefAPINamespace; \ No newline at end of file diff --git a/@sammo/server/src/api/daemon/index.ts b/@sammo/server/src/api/daemon/index.ts new file mode 100644 index 0000000..5159ac0 --- /dev/null +++ b/@sammo/server/src/api/daemon/index.ts @@ -0,0 +1,13 @@ +import type { APINamespace, APINamespaceType } from '@strpc/express'; +import { status } from './status.js'; +import { start } from './start.js'; +import { stop } from './stop.js'; +import { restart } from './restart.js'; +import type { structure } from "@sammo/api_def"; + +export const daemon = { + status, + start, + stop, + restart, +} satisfies APINamespaceType; \ No newline at end of file diff --git a/@sammo/server/src/api/daemon/restart.ts b/@sammo/server/src/api/daemon/restart.ts new file mode 100644 index 0000000..eea3a0d --- /dev/null +++ b/@sammo/server/src/api/daemon/restart.ts @@ -0,0 +1,24 @@ +import { getGameEngineController } from "@/GameEngineController.js"; +import { ReqACL } from "@/procDecorator/ReqACL.js"; +import { ReqLogin } from "@/procDecorator/ReqLogin.js"; +import type { structure } from "@sammo/api_def"; +import { StartSession } from "@sammo/server_util"; +import { type APIReturnType, POST } from "@strpc/express"; +import { declProcDecorators } from "@strpc/express/proc_decorator"; + +type BaseAPI = typeof structure.daemon.restart; +type RType = APIReturnType; + +export const restart = POST()(declProcDecorators( + StartSession, + ReqLogin, + ReqACL('StopAndResumeServer'), +)) +(async (query, ctx): RType => { + const controller = getGameEngineController(); + + await controller.restart(); + return { + result: true, + } +}) \ No newline at end of file diff --git a/@sammo/server/src/api/daemon/start.ts b/@sammo/server/src/api/daemon/start.ts new file mode 100644 index 0000000..afcd002 --- /dev/null +++ b/@sammo/server/src/api/daemon/start.ts @@ -0,0 +1,31 @@ +import { getGameEngineController } from "@/GameEngineController.js"; +import { ReqACL } from "@/procDecorator/ReqACL.js"; +import { ReqLogin } from "@/procDecorator/ReqLogin.js"; +import type { structure } from "@sammo/api_def"; +import { StartSession } from "@sammo/server_util"; +import { type APIReturnType, POST } from "@strpc/express"; +import { declProcDecorators } from "@strpc/express/proc_decorator"; + +type BaseAPI = typeof structure.daemon.start; +type RType = APIReturnType; + +export const start = POST()(declProcDecorators( + StartSession, + ReqLogin, + ReqACL('StopAndResumeServer'), +)) +(async (query, ctx): RType => { + const controller = getGameEngineController(); + + if(controller.isRunning) { + return { + result: false, + reason: 'Already running', + } + } + + await controller.start(); + return { + result: true, + } +}) \ No newline at end of file diff --git a/@sammo/server/src/api/daemon/status.ts b/@sammo/server/src/api/daemon/status.ts new file mode 100644 index 0000000..bfc8b84 --- /dev/null +++ b/@sammo/server/src/api/daemon/status.ts @@ -0,0 +1,25 @@ +import { getGameEngineController } from "@/GameEngineController.js"; +import { ReqACL } from "@/procDecorator/ReqACL.js"; +import { ReqLogin } from "@/procDecorator/ReqLogin.js"; +import type { structure } from "@sammo/api_def"; +import { StartSession } from "@sammo/server_util"; +import { GET, type APIReturnType } from "@strpc/express"; +import { declProcDecorators } from "@strpc/express/proc_decorator"; + +type BaseAPI = typeof structure.daemon.status; +type RType = APIReturnType; + +export const status = GET()(declProcDecorators( + StartSession, + ReqLogin, +)) +(async (query, ctx): RType => { + const controller = getGameEngineController(); + const isRunning = controller.isRunning + const lastExecuted = controller.lastExecuted?.toISOString() ?? undefined; + return { + result: true, + isRunning, + lastExecuted, + } +}) \ No newline at end of file diff --git a/@sammo/server/src/api/daemon/stop.ts b/@sammo/server/src/api/daemon/stop.ts new file mode 100644 index 0000000..b173a20 --- /dev/null +++ b/@sammo/server/src/api/daemon/stop.ts @@ -0,0 +1,31 @@ +import { getGameEngineController } from "@/GameEngineController.js"; +import { ReqACL } from "@/procDecorator/ReqACL.js"; +import { ReqLogin } from "@/procDecorator/ReqLogin.js"; +import type { structure } from "@sammo/api_def"; +import { StartSession } from "@sammo/server_util"; +import { type APIReturnType, POST } from "@strpc/express"; +import { declProcDecorators } from "@strpc/express/proc_decorator"; + +type BaseAPI = typeof structure.daemon.stop; +type RType = APIReturnType; + +export const stop = POST()(declProcDecorators( + StartSession, + ReqLogin, + ReqACL('StopAndResumeServer'), +)) +(async (query, ctx): RType => { + const controller = getGameEngineController(); + + if(!controller.isRunning) { + return { + result: false, + reason: 'Already stopped', + } + } + + await controller.stop(); + return { + result: true, + } +}) \ No newline at end of file diff --git a/@sammo/server/src/api/index.ts b/@sammo/server/src/api/index.ts index c0fead8..3406f20 100644 --- a/@sammo/server/src/api/index.ts +++ b/@sammo/server/src/api/index.ts @@ -1,5 +1,6 @@ import type { structure } from "@sammo/api_def"; import type { APINamespaceType } from "@strpc/express"; - +import { daemon } from "./daemon/index.js"; export const sammoAPI = { + daemon, } satisfies APINamespaceType; \ No newline at end of file