Skip to content

Commit 9a1a79b

Browse files
committed
feat: add support for multiple runtime modes (MSW and Server) in the console application
1 parent 3f38d12 commit 9a1a79b

6 files changed

Lines changed: 185 additions & 7 deletions

File tree

apps/console/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Environment Variables
2+
# Copy this file to .env.local and customize
3+
4+
# Runtime Mode
5+
# Options: 'msw' (default) | 'server'
6+
# - msw: Use Mock Service Worker with in-browser ObjectStack kernel
7+
# - server: Connect to a real ObjectStack server
8+
VITE_RUNTIME_MODE=msw
9+
10+
# ObjectStack Server URL (used when VITE_RUNTIME_MODE=server)
11+
# VITE_SERVER_URL=http://localhost:5000/api/v1

apps/console/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ ObjectStack Console provides a modern, responsive admin interface that:
1414

1515
## 🏗️ Architecture
1616

17-
The console runs the ObjectStack Runtime directly in the browser using MSW (Mock Service Worker), enabling full offline development.
17+
The console supports two runtime modes:
18+
19+
### MSW Mode (Default)
20+
21+
Runs the ObjectStack Runtime directly in the browser using MSW (Mock Service Worker), enabling full offline development.
1822

1923
```mermaid
2024
graph TD
@@ -25,18 +29,41 @@ graph TD
2529
Kernel -.->|Reads| Config["objectstack.config.ts"]
2630
```
2731

32+
### Server Mode
33+
34+
Connects to a real ObjectStack server for production use or integration testing.
35+
36+
```mermaid
37+
graph TD
38+
Console["Console App"] -->|REST API| Server["ObjectStack Server"]
39+
Server -->|Processes| Kernel["ObjectStack Kernel"]
40+
Kernel -->|Uses| Driver["Database Driver"]
41+
```
42+
2843
## 🚀 Quick Start
2944

3045
```bash
3146
# Install dependencies
3247
pnpm install
3348

34-
# Start development server
49+
# Start development server (MSW mode)
3550
pnpm dev
51+
52+
# Start in server mode (connects to real backend)
53+
VITE_RUNTIME_MODE=server VITE_SERVER_URL=http://localhost:5000/api/v1 pnpm dev
3654
```
3755

3856
The console will be available at `http://localhost:3000`.
3957

58+
## ⚙️ Environment Variables
59+
60+
| Variable | Default | Description |
61+
|----------|---------|-------------|
62+
| `VITE_RUNTIME_MODE` | `msw` | Runtime mode: `msw` or `server` |
63+
| `VITE_SERVER_URL` | `http://localhost:5000/api/v1` | Server URL (server mode only) |
64+
65+
Copy `.env.example` to `.env.local` to customize:
66+
4067
## 📁 Project Structure
4168

4269
```

apps/console/src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ObjectDataForm } from './components/ObjectDataForm';
88
import { Toaster } from "@/components/ui/toaster"
99
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
1010
import { Database, Layers, Sparkles, Zap } from 'lucide-react';
11+
import { getApiBaseUrl, config } from './lib/config';
1112

1213
function DashboardWelcome() {
1314
return (
@@ -122,7 +123,10 @@ export default function App() {
122123
const [showForm, setShowForm] = useState(false);
123124

124125
useEffect(() => {
125-
const baseUrl = '/api/v1';
126+
// Use the configured API base URL based on runtime mode (MSW or Server)
127+
const baseUrl = getApiBaseUrl();
128+
console.log(`[App] Connecting to API: ${baseUrl} (mode: ${config.mode})`);
129+
126130
const newClient = new ObjectStackClient({
127131
baseUrl,
128132
});

apps/console/src/lib/config.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Console Application Configuration
3+
*
4+
* Supports two runtime modes:
5+
* - MSW Mode: Uses Mock Service Worker with in-browser ObjectStack kernel
6+
* - Server Mode: Connects to a real ObjectStack server
7+
*/
8+
9+
export type RuntimeMode = 'msw' | 'server';
10+
11+
export interface ConsoleConfig {
12+
/**
13+
* Runtime mode
14+
* - 'msw': Mock Service Worker mode (browser-based kernel)
15+
* - 'server': Connect to real ObjectStack server
16+
*/
17+
mode: RuntimeMode;
18+
19+
/**
20+
* Server base URL (used in 'server' mode)
21+
* @default 'http://localhost:5000/api/v1'
22+
*/
23+
serverUrl: string;
24+
25+
/**
26+
* MSW API base path (used in 'msw' mode)
27+
* @default '/api/v1'
28+
*/
29+
mswBasePath: string;
30+
}
31+
32+
/**
33+
* Get runtime mode from environment
34+
*/
35+
function getRuntimeMode(): RuntimeMode {
36+
const envMode = import.meta.env.VITE_RUNTIME_MODE;
37+
38+
if (envMode === 'server') {
39+
return 'server';
40+
}
41+
42+
// Default to MSW mode for development
43+
return 'msw';
44+
}
45+
46+
/**
47+
* Default configuration values
48+
*/
49+
const defaultConfig: ConsoleConfig = {
50+
mode: getRuntimeMode(),
51+
serverUrl: import.meta.env.VITE_SERVER_URL || 'http://localhost:5000/api/v1',
52+
mswBasePath: '/api/v1',
53+
};
54+
55+
/**
56+
* Current application configuration
57+
*/
58+
export const config: ConsoleConfig = {
59+
...defaultConfig,
60+
};
61+
62+
/**
63+
* Check if running in MSW mode
64+
*/
65+
export function isMswMode(): boolean {
66+
return config.mode === 'msw';
67+
}
68+
69+
/**
70+
* Check if running in Server mode
71+
*/
72+
export function isServerMode(): boolean {
73+
return config.mode === 'server';
74+
}
75+
76+
/**
77+
* Get the API base URL based on current mode
78+
*/
79+
export function getApiBaseUrl(): string {
80+
if (isServerMode()) {
81+
return config.serverUrl;
82+
}
83+
return config.mswBasePath;
84+
}
85+
86+
/**
87+
* Update configuration at runtime
88+
* Useful for switching modes programmatically
89+
*/
90+
export function updateConfig(updates: Partial<ConsoleConfig>): void {
91+
Object.assign(config, updates);
92+
}
93+
94+
/**
95+
* Log current configuration (for debugging)
96+
*/
97+
export function logConfig(): void {
98+
console.log('[Console Config]', {
99+
mode: config.mode,
100+
apiBaseUrl: getApiBaseUrl(),
101+
serverUrl: config.serverUrl,
102+
});
103+
}

apps/console/src/main.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* Main Entry Point
33
*
4-
* Initializes MSW and renders the React application.
4+
* Initializes the console application with support for two runtime modes:
5+
* - MSW Mode: Uses Mock Service Worker with in-browser ObjectStack kernel
6+
* - Server Mode: Connects to a real ObjectStack server
7+
*
8+
* Set VITE_RUNTIME_MODE=server to connect to a real server
9+
* Set VITE_RUNTIME_MODE=msw (or leave empty) for MSW mode
510
*/
611

712
import './mocks/process-polyfill';
@@ -11,11 +16,20 @@ import './index.css';
1116
import App from './App';
1217
import { AppWithHooks } from './AppWithHooks';
1318
import { startMockServer } from './mocks/browser';
19+
import { config, isMswMode, logConfig } from './lib/config';
1420

15-
// Start MSW before rendering the app
21+
// Bootstrap the application
1622
async function bootstrap() {
17-
// Initialize Mock Service Worker
18-
await startMockServer();
23+
// Log current configuration
24+
logConfig();
25+
26+
// Only start MSW in MSW mode
27+
if (isMswMode()) {
28+
console.log('[Console] Starting in MSW mode (in-browser kernel)');
29+
await startMockServer();
30+
} else {
31+
console.log(`[Console] Starting in Server mode (connecting to ${config.serverUrl})`);
32+
}
1933

2034
// Switch between traditional approach and hooks-based approach
2135
// Change this to true to use the hooks version

apps/console/src/vite-env.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
/**
5+
* Runtime mode for the console application
6+
* - 'msw': Mock Service Worker mode (browser-based kernel)
7+
* - 'server': Connect to real ObjectStack server
8+
*/
9+
readonly VITE_RUNTIME_MODE: 'msw' | 'server';
10+
11+
/**
12+
* ObjectStack server URL (used when VITE_RUNTIME_MODE=server)
13+
*/
14+
readonly VITE_SERVER_URL: string;
15+
}
16+
17+
interface ImportMeta {
18+
readonly env: ImportMetaEnv;
19+
}

0 commit comments

Comments
 (0)