Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions cli/src/commands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
50 changes: 50 additions & 0 deletions cli/src/services/server/observatory.services.ts
Original file line number Diff line number Diff line change
@@ -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.');
};