feat: api/daemon/{start,stop,restart,status}

This commit is contained in:
2024-03-09 16:16:12 +00:00
parent ddfff44c12
commit c42a97bb5f
7 changed files with 136 additions and 5 deletions
+10 -4
View File
@@ -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<ValidResponse & { isRunning: boolean, lastExecuted?: string }>(),
start: POST<ValidResponse>(),
stop: POST<ValidResponse>(),
restart: POST<ValidResponse>(),
}
} satisfies DefAPINamespace;
+13
View File
@@ -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<typeof structure['daemon']>;
+24
View File
@@ -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<BaseAPI>;
export const restart = POST<BaseAPI>()(declProcDecorators(
StartSession,
ReqLogin,
ReqACL('StopAndResumeServer'),
))
(async (query, ctx): RType => {
const controller = getGameEngineController();
await controller.restart();
return {
result: true,
}
})
+31
View File
@@ -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<BaseAPI>;
export const start = POST<BaseAPI>()(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,
}
})
+25
View File
@@ -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<BaseAPI>;
export const status = GET<BaseAPI>()(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,
}
})
+31
View File
@@ -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<BaseAPI>;
export const stop = POST<BaseAPI>()(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,
}
})
+2 -1
View File
@@ -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<typeof structure>;