| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { serverLogger as log } from '$lib/server/logger';
- import type { Workspace } from '$lib/types/workspace';
- import { env } from '$env/dynamic/private';
- import { Auth } from './auth';
- import { Catalog } from './catalog';
- import { Resources } from './resources';
- import { WorkspaceManager } from './workspace';
- class Server {
- // Guards against double-init in HMR / test environments.
- #initialized = false;
- #ws!: WorkspaceManager;
- #auth: Auth | undefined;
- #catalog!: Catalog;
- #resources!: Resources;
- async init(): Promise<void> {
- if (this.#initialized) return;
- this.#initialized = true;
- log.info('Server initializing');
- await Promise.all([this.#createWorkspaceManager(), this.#createAuth()]);
- const workspaces = this.#ws.list();
- await Promise.all([this.#createCatalog(workspaces), this.#createResources(workspaces)]);
- log.info('Server ready');
- }
- async shutdown(): Promise<void> {}
- async #createWorkspaceManager(): Promise<void> {
- const ws = new WorkspaceManager();
- await ws.init();
- this.#ws = ws;
- }
- async #createAuth(): Promise<void> {
- if (!env.AUTH_JWT_SECRET || !env.AUTH_USERS_FILE) {
- log.info('Auth is disabled because AUTH_JWT_SECRET or AUTH_USERS_FILE is not set');
- return;
- }
- const auth = new Auth({
- jwtSecret: env.AUTH_JWT_SECRET,
- usersFile: env.AUTH_USERS_FILE,
- });
- log.info(`Auth is enabled, users loaded from ${env.AUTH_USERS_FILE}`);
- await auth.init();
- this.#auth = auth;
- }
- async #createCatalog(workspaces: Workspace[]): Promise<void> {
- const catalog = new Catalog(workspaces);
- await catalog.init();
- this.#catalog = catalog;
- }
- async #createResources(workspaces: Workspace[]): Promise<void> {
- const resources = new Resources(workspaces);
- await resources.init();
- this.#resources = resources;
- }
- get ws(): WorkspaceManager {
- return this.#ws;
- }
- get auth(): Auth | undefined {
- return this.#auth;
- }
- get catalog(): Catalog {
- return this.#catalog;
- }
- get resources(): Resources {
- return this.#resources;
- }
- }
- // Singleton shared across hooks and route handlers.
- const server = new Server();
- export { Server, server };
|