From 07bbadbbdcb72a01daf91d69f5ea4493dd921fd2 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 29 May 2023 16:26:16 +0000 Subject: [PATCH] wip --- .../server/api/endpoints/admin/server-info.ts | 94 +------------------ packages/misskey-js/src/endpoints.ts | 13 +++ packages/misskey-js/src/schemas.ts | 6 ++ .../misskey-js/src/schemas/server-info.ts | 86 +++++++++++++++++ packages/misskey-js/test-d/schemas.ts | 9 ++ 5 files changed, 117 insertions(+), 91 deletions(-) create mode 100644 packages/misskey-js/src/schemas/server-info.ts diff --git a/packages/backend/src/server/api/endpoints/admin/server-info.ts b/packages/backend/src/server/api/endpoints/admin/server-info.ts index 4ef4fdc665..b86d70fdd4 100644 --- a/packages/backend/src/server/api/endpoints/admin/server-info.ts +++ b/packages/backend/src/server/api/endpoints/admin/server-info.ts @@ -6,98 +6,10 @@ import * as Redis from 'ioredis'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; -export const meta = { - requireCredential: true, - requireModerator: true, - - tags: ['admin', 'meta'], - - res: { - type: 'object', - optional: false, nullable: false, - properties: { - machine: { - type: 'string', - optional: false, nullable: false, - }, - os: { - type: 'string', - optional: false, nullable: false, - example: 'linux', - }, - node: { - type: 'string', - optional: false, nullable: false, - }, - psql: { - type: 'string', - optional: false, nullable: false, - }, - cpu: { - type: 'object', - optional: false, nullable: false, - properties: { - model: { - type: 'string', - optional: false, nullable: false, - }, - cores: { - type: 'number', - optional: false, nullable: false, - }, - }, - }, - mem: { - type: 'object', - optional: false, nullable: false, - properties: { - total: { - type: 'number', - optional: false, nullable: false, - format: 'bytes', - }, - }, - }, - fs: { - type: 'object', - optional: false, nullable: false, - properties: { - total: { - type: 'number', - optional: false, nullable: false, - format: 'bytes', - }, - used: { - type: 'number', - optional: false, nullable: false, - format: 'bytes', - }, - }, - }, - net: { - type: 'object', - optional: false, nullable: false, - properties: { - interface: { - type: 'string', - optional: false, nullable: false, - example: 'eth0', - }, - }, - }, - }, - }, -} as const; - -export const paramDef = { - type: 'object', - properties: {}, - required: [], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'admin/server-info'> { + name = 'admin/server-info' as const; constructor( @Inject(DI.db) private db: DataSource, @@ -106,7 +18,7 @@ export default class extends Endpoint { private redisClient: Redis.Redis, ) { - super(meta, paramDef, async () => { + super(async () => { const memStats = await si.mem(); const fsStats = await si.fsSize(); const netInterface = await si.networkInterfaceDefault(); diff --git a/packages/misskey-js/src/endpoints.ts b/packages/misskey-js/src/endpoints.ts index def244224a..acfc0f1796 100644 --- a/packages/misskey-js/src/endpoints.ts +++ b/packages/misskey-js/src/endpoints.ts @@ -1613,6 +1613,19 @@ export const endpoints = { res: undefined, }], }, + 'admin/server-info': { + requireCredential: true, + requireModerator: true, + + tags: ['admin', 'meta'], + + defines: [{ + req: undefined, + res: { + $ref: 'https://misskey-hub.net/api/schemas/ServerInfoAdmin', + } + }], + }, } as const satisfies { [x: string]: IEndpointMeta; }; /** diff --git a/packages/misskey-js/src/schemas.ts b/packages/misskey-js/src/schemas.ts index 4efc410fcc..e4df1b74fd 100644 --- a/packages/misskey-js/src/schemas.ts +++ b/packages/misskey-js/src/schemas.ts @@ -47,6 +47,10 @@ import { InstanceMetaSharedSchema, InstanceMetaAdminSchema, } from './schemas/instance-meta.js'; +import { + ServerInfoSchema, + ServerInfoAdminSchema, +} from './schemas/server-info.js'; import { Error, ApiError } from './schemas/error.js'; import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type'; @@ -95,6 +99,8 @@ export const refs = { AbuseUserReport: packedAbuseUserReportSchema, InstanceMetaShared: InstanceMetaSharedSchema, InstanceMetaAdmin: InstanceMetaAdminSchema, + ServerInfo: ServerInfoSchema, + ServerInfoAdmin: ServerInfoAdminSchema, Error: Error, ApiError: ApiError, diff --git a/packages/misskey-js/src/schemas/server-info.ts b/packages/misskey-js/src/schemas/server-info.ts new file mode 100644 index 0000000000..588a530f7d --- /dev/null +++ b/packages/misskey-js/src/schemas/server-info.ts @@ -0,0 +1,86 @@ +import type { JSONSchema7Definition } from 'schema-type'; + +export const ServerInfoSchema = { + $id: 'https://misskey-hub.net/api/schemas/ServerInfo', + + type: 'object', + properties: { + machine: { + type: 'string', + }, + cpu: { + type: 'object', + properties: { + model: { type: 'string' }, + cores: { type: 'number' }, + }, + required: ['model', 'cores'], + }, + mem: { + type: 'object', + properties: { + total: { type: 'number', format: 'bytes' }, + }, + required: ['total'], + }, + fs: { + type: 'object', + properties: { + total: { type: 'number', format: 'bytes' }, + used: { type: 'number', format: 'bytes' }, + }, + }, + }, + required: [ + 'machine', + 'cpu', + 'mem', + 'fs', + ], +} as const satisfies JSONSchema7Definition; + +export const ServerInfoAdminSchema = { + $id: 'https://misskey-hub.net/api/schemas/ServerInfoAdmin', + + allOf: [ + { $ref: 'https://misskey-hub.net/api/schemas/ServerInfo' }, + { + type: 'object', + properties: { + os: { + type: 'string', + enum: [ + // NodeJS.Platform + 'aix', + 'android', + 'darwin', + 'freebsd', + 'haiku', + 'linux', + 'openbsd', + 'sunos', + 'win32', + 'cygwin', + 'netbsd', + ] + }, + node: { type: 'string', examples: ['v14.17.0'] }, + psql: { type: 'string' }, + redis: { type: 'string' }, + net: { + type: 'object', + properties: { + interface: { type: 'string', examples: ['eth0'] }, + }, + required: ['interface'], + }, + }, + required: [ + 'os', + 'node', + 'psql', + 'net', + ], + }, + ], +} as const satisfies JSONSchema7Definition; diff --git a/packages/misskey-js/test-d/schemas.ts b/packages/misskey-js/test-d/schemas.ts index 43f1cda765..aa702d6d61 100644 --- a/packages/misskey-js/test-d/schemas.ts +++ b/packages/misskey-js/test-d/schemas.ts @@ -110,6 +110,15 @@ describe('schemas', () => { test('abuse user report', () => { type AbuseUserReport = Packed<'AbuseUserReport'>; }); + test('meta', () => { + type MetaShared = Packed<'InstanceMetaShared'>; + //type Meta = Packed<'InstanceMeta'>; + type MetaAdmin = Packed<'InstanceMetaAdmin'>; + }); + test('server info', () => { + type ServerInfo = Packed<'ServerInfo'>; + type ServerInfoAdmin = Packed<'ServerInfoAdmin'>; + }); test('error', () => { type Error = Packed<'Error'>; type ApiError = Packed<'ApiError'>;