diff --git a/cli/src/commands/server.ts b/cli/src/commands/server.ts index 30196b2..5e16d49 100644 --- a/cli/src/commands/server.ts +++ b/cli/src/commands/server.ts @@ -15,6 +15,7 @@ import {buildContext} from '../services/context.services'; import {collectIdentities} from '../services/identity.services'; import {setController} from '../services/server/controller.services'; import {transfer} from '../services/server/ledger.services'; +import {toggleOpenIdMonitoring} from '../services/server/observatory.services'; import {touchWatchedFile} from '../services/server/touch.services'; import type {CliContext} from '../types/context'; import {prettifyError} from '../utils/error.utils'; @@ -47,6 +48,8 @@ const buildServer = ({context}: {context: CliContext}): Server => const command = pathname.split('/')[1]; // eslint-disable-next-line @typescript-eslint/prefer-destructuring const subCommand = pathname.split('/')[2]; + // eslint-disable-next-line @typescript-eslint/prefer-destructuring + const subSubCommand = pathname.split('/')[3]; const done = () => { res.writeHead(200, headers); @@ -79,17 +82,26 @@ const buildServer = ({context}: {context: CliContext}): Server => // If the CLI was build for the satellite but the /console/ is queried, then the feature is not supported. const satelliteBuild = process.env.CLI_BUILD === 'satellite'; + const skylabBuild = process.env.CLI_BUILD === 'skylab'; if (!satelliteBuild && ['console', 'observatory'].includes(command)) { - switch (subCommand) { - case 'controller': - await setController({ - context, - searchParams, - key: command === 'observatory' ? observatory.key : consoleModule.key - }); - done(); - return; + if (subCommand === 'controller') { + await setController({ + context, + searchParams, + key: command === 'observatory' ? observatory.key : consoleModule.key + }); + done(); + return; + } + + if (skylabBuild && subCommand === 'monitoring' && subSubCommand === 'openid') { + await toggleOpenIdMonitoring({ + context, + searchParams + }); + done(); + return; } error404(); diff --git a/cli/src/services/server/observatory.services.ts b/cli/src/services/server/observatory.services.ts new file mode 100644 index 0000000..4b492b4 --- /dev/null +++ b/cli/src/services/server/observatory.services.ts @@ -0,0 +1,50 @@ +import {OBSERVATORY_CANISTER_ID} from '../../modules/observatory'; +import type {CliContext} from '../../types/context'; +import {getObservatoryActor} from '../actor.services'; + +export const toggleOpenIdMonitoring = async ({ + context, + searchParams +}: { + context: CliContext; + searchParams: URLSearchParams; +}) => { + const action = searchParams.get('action') ?? ''; + + switch (action) { + case 'start': { + await startOpenIdMonitoring({context}); + return; + } + case 'stop': { + await stopOpenIdMonitoring({context}); + return; + } + default: + throw new Error('Unknown action provided for toggling OpenId monitoring'); + } +}; + +const startOpenIdMonitoring = async ({context}: {context: CliContext}) => { + const {agent} = context; + + const {start_openid_monitoring} = await getObservatoryActor({ + agent, + canisterId: OBSERVATORY_CANISTER_ID + }); + + await start_openid_monitoring(); + console.log('🟢 Observatory OpenId monitoring started.'); +}; + +const stopOpenIdMonitoring = async ({context}: {context: CliContext}) => { + const {agent} = context; + + const {stop_openid_monitoring} = await getObservatoryActor({ + agent, + canisterId: OBSERVATORY_CANISTER_ID + }); + + await stop_openid_monitoring(); + console.log('🔴 Observatory OpenId monitoring stopped.'); +};