Skip to content
Open
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
49 changes: 40 additions & 9 deletions app/components/Launcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,56 @@ import { useInfraStore } from "@ogw_front/stores/infra";

interface Props {
appName: AppName;
// Firebase ID token for the cloud launch; required in cloud mode.
// Firebase ID token for the cloud launch; required in cloud mode. Also sent with the local launches.
authToken?: AuthToken;
isUserAuthenticated?: boolean;
// True while the authentication state is being restored: the launcher waits instead of showing the auth slot.
isAuthPending?: boolean;
// Outside cloud mode, launches the local microservices only once the user is authenticated.
requireAuth?: boolean;
logo?: ImageSource;
}
const {
appName,
authToken = undefined,
isUserAuthenticated = false,
isAuthPending = false,
requireAuth = false,
logo = "",
} = defineProps<Props>();

const infraStore = useInfraStore();
// In cloud mode, wait for the user to authenticate and load the app, unless a `?cloud_url=` link
// Points at an already running service: that one is connected straight away, without authentication.
if (infraStore.app_mode !== appMode.CLOUD || getCloudUrlParam() !== undefined) {
const isCloud = infraStore.app_mode === appMode.CLOUD;
// The local launch is automatic once the user is authenticated: no "Load the app" button.
const waitsForLogin = requireAuth && !isCloud;

if (isCloud) {
// In cloud mode, wait for the user to authenticate and load the app, unless a `?cloud_url=` link
// Points at an already running service: that one is connected straight away, without authentication.
if (getCloudUrlParam() !== undefined) {
void createBackend(authToken);
}
} else if (waitsForLogin) {
let launch_requested = false;
watch(
() => isUserAuthenticated && authToken !== undefined,
(is_ready) => {
if (!is_ready || launch_requested || infraStore.status !== Status.NOT_CREATED) {
return;
}
launch_requested = true;
void createBackend(authToken);
},
{ immediate: true },
);
} else {
void createBackend(authToken);
}

const showLoadButton = computed(
() => !waitsForLogin && isUserAuthenticated && infraStore.status === Status.NOT_CREATED,
);

function cloudCreateBackend(): Promise<void> {
return createBackend(authToken);
}
Expand All @@ -37,11 +68,14 @@ function cloudCreateBackend(): Promise<void> {
<template>
<VContainer class="justify">
<VRow align-content="center" align="center" justify="center">
<VCol cols="12" align-self="center">
<VCol v-if="!isAuthPending" cols="12" align-self="center">
<slot name="auth" />
</VCol>
<VCol v-if="isAuthPending || infraStore.status === Status.CREATING">
<Loading :logo="logo" :app-name="appName" />
</VCol>
<VCol
v-if="isUserAuthenticated && infraStore.status === Status.NOT_CREATED"
v-else-if="showLoadButton"
class="d-flex justify-center align-center"
cols="12"
align-self="center"
Expand All @@ -56,9 +90,6 @@ function cloudCreateBackend(): Promise<void> {
@click="cloudCreateBackend"
/>
</VCol>
<VCol v-else-if="infraStore.status === Status.CREATING">
<Loading :logo="logo" :app-name="appName" />
</VCol>
</VRow>
</VContainer>
</template>
Expand Down
6 changes: 4 additions & 2 deletions app/stores/back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
usePing,
useRequestCounter,
} from "@ogw_internal/stores/microservice";
import type { AuthToken } from "@ogw_front/types/server";
import type { RequestHandlers } from "@ogw_shared/utils/types.js";
import { Status } from "@ogw_front/utils/status";
import type { Url } from "@ogw_front/types/files";
import { authorizationHeaders } from "@ogw_front/utils/auth_headers";
import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_typed_schemas.js";
import opengeodeweb_front_schemas from "@geode/opengeodeweb-front/opengeodeweb_front_typed_schemas.js";
import { useAppStore } from "@ogw_front/stores/app";
Expand Down Expand Up @@ -67,14 +69,14 @@ export const useBackStore = defineStore(

const { start_ping, stop_ping } = usePing(ping);

async function launch(args: LaunchArgs): Promise<unknown> {
async function launch(args: LaunchArgs, authToken?: AuthToken): Promise<unknown> {
const appStore = useAppStore();
const { COMMAND_BACK, NUXT_ROOT_PATH } = useRuntimeConfig().public;
const schema = opengeodeweb_front_schemas.api.local.app.run_back;
const params = { COMMAND_BACK, NUXT_ROOT_PATH, args };

const result = await appStore.request(
{ schema, params },
{ schema, params, headers: authorizationHeaders(authToken) },
{
response_function: (response) => {
default_local_port.value = String(response.port);
Expand Down
4 changes: 3 additions & 1 deletion app/stores/infra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FilePath, Url } from "@ogw_front/types/files";
import { type MicroserviceStatus, Status } from "@ogw_front/utils/status";
import type { AuthToken } from "@ogw_front/types/server";
import type { MicroserviceId } from "@ogw_front/types/identifiers";

const LOCALHOST = "localhost";
Expand All @@ -14,7 +15,8 @@ interface Microservice {
$id: MicroserviceId;
status?: MicroserviceStatus;
is_busy?: boolean;
launch?: (params: LaunchArgs) => Promise<unknown>;
// `authToken` is the user's Firebase ID token, sent to the app server that spawns the process
launch?: (params: LaunchArgs, authToken?: AuthToken) => Promise<unknown>;
connect: () => void | Promise<void>;
}

Expand Down
16 changes: 12 additions & 4 deletions app/stores/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ export const useViewerStore = defineStore(
}
});
}
async function launch(args: Readonly<LaunchArgs>): Promise<unknown> {
const result = await launchViewerMicroservice(args, (new_port) => {
default_local_port.value = new_port;
});
// The token type is taken from the helper: one more import would exceed this file's dependency limit
async function launch(
args: Readonly<LaunchArgs>,
authToken?: Parameters<typeof launchViewerMicroservice>[2],
): Promise<unknown> {
const result = await launchViewerMicroservice(
args,
(new_port) => {
default_local_port.value = new_port;
},
authToken,
);
return result;
}
async function connect(): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions app/stores/viewer_helpers/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { AuthToken } from "@ogw_front/types/server";
import type { Coordinate } from "@ogw_front/types/geometry";
import type { LaunchArgs } from "@ogw_front/stores/infra";
import type { Vector3 } from "@kitware/vtk.js/types";
import type { ViewerRequest } from "./request.js";
import { authorizationHeaders } from "@ogw_front/utils/auth_headers";
import { consola } from "consola";
import opengeodeweb_front_schemas from "@geode/opengeodeweb-front/opengeodeweb_front_typed_schemas.js";
import opengeodeweb_viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_typed_schemas.js";
Expand All @@ -28,6 +30,7 @@ async function pickWorldPosition(
async function launchViewerMicroservice(
args: Readonly<LaunchArgs>,
onPort: (port: string) => void,
authToken?: AuthToken,
): Promise<unknown> {
const appStore = useAppStore();
const { COMMAND_VIEWER, NUXT_ROOT_PATH } = useRuntimeConfig().public;
Expand All @@ -41,6 +44,7 @@ async function launchViewerMicroservice(
{
schema,
params,
headers: authorizationHeaders(authToken),
},
{
response_function: (response) => {
Expand Down
11 changes: 11 additions & 0 deletions app/utils/auth_headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { AuthToken } from "@ogw_front/types/server";

// The `Authorization` header carrying the user's Firebase ID token, or no header without a token.
function authorizationHeaders(authToken?: AuthToken): Record<string, string> {
if (authToken === undefined) {
return {};
}
return { Authorization: `Bearer ${authToken}` };
}

export { authorizationHeaders };
12 changes: 8 additions & 4 deletions app/utils/backend_lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ declare global {
var electronAPI: ElectronApi["electronAPI"];
}

async function launchLocalServices(): Promise<void> {
// `authToken` is the user's Firebase ID token, sent with every request that spawns a process
async function launchLocalServices(authToken?: AuthToken): Promise<void> {
const infraStore = useInfraStore();
const appStore = useAppStore();
await appStore.createProjectFolder();
Expand All @@ -43,10 +44,13 @@ async function launchLocalServices(): Promise<void> {
const launch_promises: Promise<unknown>[] = infraStore.microservices
.filter((store: Microservice) => store.launch)
.map(async (store: Microservice) => {
const result = await store.launch?.({ projectFolderPath: appStore.projectFolderPath });
const result = await store.launch?.(
{ projectFolderPath: appStore.projectFolderPath },
authToken,
);
return result;
});
launch_promises.push(registerRunningExtensions());
launch_promises.push(registerRunningExtensions(authToken));
await Promise.all(launch_promises);
}

Expand Down Expand Up @@ -147,7 +151,7 @@ async function createBackend(authToken?: AuthToken): Promise<void> {
throw error;
}
} else {
await launchLocalServices();
await launchLocalServices(authToken);
}
infraStore.set_status(Status.CREATED);
await infraStore.create_connection();
Expand Down
27 changes: 19 additions & 8 deletions app/utils/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// Third party imports

// Local imports
import type { ExtensionName, PackageVersion, Port } from "@ogw_front/types/server";
import type { AuthToken, ExtensionName, PackageVersion, Port } from "@ogw_front/types/server";
import type { FileName, Url } from "@ogw_front/types/files";
import type { RegisterableStore, useAppStore } from "@ogw_front/stores/app";
import type { ExtensionId } from "@ogw_front/types/identifiers";
import type { Microservice } from "@ogw_front/stores/infra";
import { authorizationHeaders } from "@ogw_front/utils/auth_headers";
import { consola } from "consola";
import { isCloudMode } from "@ogw_front/utils/stores";
import opengeodeweb_front_schemas from "@geode/opengeodeweb-front/opengeodeweb_front_typed_schemas.js";
Expand Down Expand Up @@ -40,7 +41,10 @@ async function uploadExtension(file: File): Promise<void> {
await appStore.upload(file);
}

async function runExtensions(): Promise<{ extensionsArray: ExtensionDescriptor[] }> {
// `authToken` is the user's Firebase ID token: running the extensions spawns their processes
async function runExtensions(
authToken?: AuthToken,
): Promise<{ extensionsArray: ExtensionDescriptor[] }> {
const { useAppStore } = await import("@ogw_front/stores/app");
const appStore = useAppStore();
const { projectFolderPath } = appStore;
Expand All @@ -55,6 +59,7 @@ async function runExtensions(): Promise<{ extensionsArray: ExtensionDescriptor[]
const result = await appStore.request({
schema,
params,
headers: authorizationHeaders(authToken),
});
return result;
}
Expand Down Expand Up @@ -83,12 +88,12 @@ function isMicroservice(store: RegisterableStore): store is RegisterableStore &
return typeof store.connect === "function";
}

async function registerRunningExtensions(): Promise<RegisteredExtension[]> {
async function registerRunningExtensions(authToken?: AuthToken): Promise<RegisteredExtension[]> {
const { useAppStore } = await import("@ogw_front/stores/app");
const { useInfraStore } = await import("@ogw_front/stores/infra");
const appStore = useAppStore();
const infraStore = useInfraStore();
const { extensionsArray } = await runExtensions();
const { extensionsArray } = await runExtensions(authToken);
return Promise.all(
extensionsArray.map(async (extension: ExtensionDescriptor) => {
const { id, name, version, frontendContent, port, serverPort } = extension;
Expand Down Expand Up @@ -123,14 +128,20 @@ async function registerRunningExtensions(): Promise<RegisteredExtension[]> {
);
}

async function importExtensionFile(file: File): Promise<RegisteredExtension[]> {
async function importExtensionFile(
file: File,
authToken?: AuthToken,
): Promise<RegisteredExtension[]> {
await uploadExtension(file);
return registerRunningExtensions();
return registerRunningExtensions(authToken);
}

async function importExtensionURL(url: DownloadExtensionParams): Promise<RegisteredExtension[]> {
async function importExtensionURL(
url: DownloadExtensionParams,
authToken?: AuthToken,
): Promise<RegisteredExtension[]> {
await downloadExtension(url);
return registerRunningExtensions();
return registerRunningExtensions(authToken);
}

async function unloadExtension(extensionId: ExtensionId): Promise<boolean> {
Expand Down
Loading
Loading