refactor session startup, shutdown, and import
This commit is contained in:
parent
64c4304f42
commit
bc4959c0da
57
gadget-code/src/services/index.ts
Normal file
57
gadget-code/src/services/index.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// src/services/index.ts
|
||||||
|
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
|
||||||
|
// All Rights Reserved
|
||||||
|
|
||||||
|
import ApiClientService from "./api-client.js";
|
||||||
|
import ChatSessionService from "./chat-session.js";
|
||||||
|
import ContactService from "./contact.js";
|
||||||
|
import CryptoService from "./crypto.js";
|
||||||
|
import DroneService from "./drone.js";
|
||||||
|
import NotificationService from "./notification.js";
|
||||||
|
import ProjectService from "./project.js";
|
||||||
|
import SessionService from "./session.js";
|
||||||
|
import SocketService from "./socket.js";
|
||||||
|
import StorageService from "./storage.js";
|
||||||
|
import UserService from "./user.js";
|
||||||
|
|
||||||
|
export async function startServices(): Promise<void> {
|
||||||
|
await ApiClientService.start();
|
||||||
|
await ChatSessionService.start();
|
||||||
|
await ContactService.start();
|
||||||
|
await CryptoService.start();
|
||||||
|
await DroneService.start();
|
||||||
|
await NotificationService.start();
|
||||||
|
await ProjectService.start();
|
||||||
|
await SessionService.start();
|
||||||
|
await SocketService.start();
|
||||||
|
await StorageService.start();
|
||||||
|
await UserService.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function stopServices(): Promise<void> {
|
||||||
|
await ApiClientService.stop();
|
||||||
|
await ChatSessionService.stop();
|
||||||
|
await ContactService.stop();
|
||||||
|
await CryptoService.stop();
|
||||||
|
await DroneService.stop();
|
||||||
|
await NotificationService.stop();
|
||||||
|
await ProjectService.stop();
|
||||||
|
await SessionService.stop();
|
||||||
|
await SocketService.stop();
|
||||||
|
await StorageService.stop();
|
||||||
|
await UserService.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
ApiClientService,
|
||||||
|
ChatSessionService,
|
||||||
|
ContactService,
|
||||||
|
CryptoService,
|
||||||
|
DroneService,
|
||||||
|
NotificationService,
|
||||||
|
ProjectService,
|
||||||
|
SessionService,
|
||||||
|
SocketService,
|
||||||
|
StorageService,
|
||||||
|
UserService,
|
||||||
|
};
|
||||||
@ -34,18 +34,20 @@ type SameSiteOption = boolean | "lax" | "strict" | "none" | undefined;
|
|||||||
import { DtpLog } from "./lib/log.js";
|
import { DtpLog } from "./lib/log.js";
|
||||||
import { DtpComponent } from "./lib/component.js";
|
import { DtpComponent } from "./lib/component.js";
|
||||||
|
|
||||||
|
import { User } from "./models/user.js";
|
||||||
|
|
||||||
import { ApiController } from "./controllers/api.js";
|
import { ApiController } from "./controllers/api.js";
|
||||||
import { AuthController } from "./controllers/auth.js";
|
import { AuthController } from "./controllers/auth.js";
|
||||||
import { HomeController } from "./controllers/home.js";
|
import { HomeController } from "./controllers/home.js";
|
||||||
import { UserController } from "./controllers/user.js";
|
import { UserController } from "./controllers/user.js";
|
||||||
|
|
||||||
import ApiClient from "./services/api-client.js";
|
import {
|
||||||
import ContactService from "./services/contact.js";
|
SessionService,
|
||||||
import SocketService from "./services/socket.js";
|
SocketService,
|
||||||
import SessionService, { SessionType } from "./services/session.js";
|
startServices,
|
||||||
import StorageService from "./services/storage.js";
|
stopServices,
|
||||||
|
} from "./services/index.js";
|
||||||
import { User } from "./models/user.js";
|
import { SessionType } from "./services/session.js";
|
||||||
|
|
||||||
class DtpWebAppServer implements DtpComponent {
|
class DtpWebAppServer implements DtpComponent {
|
||||||
private log: DtpLog;
|
private log: DtpLog;
|
||||||
@ -67,7 +69,8 @@ class DtpWebAppServer implements DtpComponent {
|
|||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
this.hookProcessSignals();
|
this.hookProcessSignals();
|
||||||
|
|
||||||
await this.startServices();
|
await startServices();
|
||||||
|
|
||||||
await this.createExpressApp();
|
await this.createExpressApp();
|
||||||
await this.startHttpServer();
|
await this.startHttpServer();
|
||||||
|
|
||||||
@ -78,14 +81,9 @@ class DtpWebAppServer implements DtpComponent {
|
|||||||
|
|
||||||
async stop(): Promise<number> {
|
async stop(): Promise<number> {
|
||||||
await this.stopHttpServer();
|
await this.stopHttpServer();
|
||||||
return 0;
|
await stopServices();
|
||||||
}
|
|
||||||
|
|
||||||
async startServices(): Promise<void> {
|
return 0;
|
||||||
await ApiClient.start();
|
|
||||||
await ContactService.start();
|
|
||||||
await SocketService.start();
|
|
||||||
await StorageService.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createExpressApp(): Promise<void> {
|
async createExpressApp(): Promise<void> {
|
||||||
|
|||||||
@ -6,22 +6,40 @@ import { v4 as uuidv4 } from "uuid";
|
|||||||
|
|
||||||
import "./lib/db.js";
|
import "./lib/db.js";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Models
|
||||||
|
*/
|
||||||
|
|
||||||
import ApiClient, { ApiClientStatus } from "./models/api-client.js";
|
import ApiClient, { ApiClientStatus } from "./models/api-client.js";
|
||||||
import User from "./models/user.js";
|
import User from "./models/user.js";
|
||||||
import AiProvider from "./models/ai-provider.js";
|
import AiProvider from "./models/ai-provider.js";
|
||||||
|
|
||||||
import ApiClientService from "./services/api-client.js";
|
/*
|
||||||
import CryptoService from "./services/crypto.js";
|
* Services
|
||||||
import UserService from "./services/user.js";
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
startServices,
|
||||||
|
stopServices,
|
||||||
|
ApiClientService,
|
||||||
|
UserService,
|
||||||
|
CryptoService,
|
||||||
|
} from "./services/index.js";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* App Logic
|
||||||
|
*/
|
||||||
|
|
||||||
import { DtpProcess } from "./lib/process.js";
|
|
||||||
import { createAiApi, type IAiLogger } from "@gadget/ai";
|
import { createAiApi, type IAiLogger } from "@gadget/ai";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type IAiModel,
|
type IAiModel,
|
||||||
type IAiModelCapabilities,
|
type IAiModelCapabilities,
|
||||||
type IAiModelSettings,
|
type IAiModelSettings,
|
||||||
} from "@gadget/api";
|
} from "@gadget/api";
|
||||||
|
|
||||||
|
import { DtpProcess } from "./lib/process.js";
|
||||||
|
|
||||||
class DtpWebCli extends DtpProcess {
|
class DtpWebCli extends DtpProcess {
|
||||||
get name(): string {
|
get name(): string {
|
||||||
return "DtpWebCli";
|
return "DtpWebCli";
|
||||||
@ -547,22 +565,17 @@ class DtpWebCli extends DtpProcess {
|
|||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
|
||||||
await this.startServices();
|
|
||||||
}
|
|
||||||
|
|
||||||
async startServices(): Promise<void> {
|
|
||||||
await ApiClientService.start();
|
|
||||||
await (await import("./services/chat-session.js")).default.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
|
await startServices();
|
||||||
|
|
||||||
const cli = new DtpWebCli();
|
const cli = new DtpWebCli();
|
||||||
await cli.start();
|
|
||||||
await cli.run(process.argv.slice(2));
|
await cli.run(process.argv.slice(2));
|
||||||
|
|
||||||
|
await stopServices();
|
||||||
|
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error((error as Error).message);
|
console.error((error as Error).message);
|
||||||
|
|||||||
@ -69,6 +69,7 @@ class PlatformService extends GadgetService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const json = (await response.json()) as PlatformRegistrationResponse;
|
const json = (await response.json()) as PlatformRegistrationResponse;
|
||||||
|
this.log.debug("platform registration response", json);
|
||||||
if (!json.success) {
|
if (!json.success) {
|
||||||
const error = new Error("failed to register drone with Platform");
|
const error = new Error("failed to register drone with Platform");
|
||||||
error.name = "PlatformError";
|
error.name = "PlatformError";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user