mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-04 22:21:21 +09:00
Add plugin system (backend + frontend)
Introduce a plugin installation and management system. Backend: add InstalledPlugin model, plugin_service with download/install/enable/disable/uninstall, dynamic blueprint registration and frontend manifest generation, and plugins API endpoints (list/get/install/enable/disable/uninstall). Register plugins blueprint and attempt to hot-load installed plugin blueprints at app startup. Frontend: add PluginLoader component (Vite import.meta.glob) to render plugin widgets, extend Marketplace UI to install/manage plugins, add API client methods for plugins, and update styles for the marketplace plugin UI. This enables installing plugins from GitHub/repos/zips and managing them from the app.
This commit is contained in:
1 parent
47fdbdb67d
commit
71b341ef77
11 files changed
+978
-1
No files matched your search
@@ -4,6 +4,8 @@ import Sidebar from '../components/Sidebar';
|
||||
import CommandPalette from '../components/CommandPalette';
|
||||
import LogsDrawer from '../components/LogsDrawer';
|
||||
import { LogsDrawerProvider } from '../contexts/LogsDrawerContext';
|
||||
import PluginLoader from '../plugins/PluginLoader';
|
||||
import api from '../services/api';
|
||||
|
||||
const DashboardLayout = () => {
|
||||
const [paletteOpen, setPaletteOpen] = useState(false);
|
||||
@@ -29,6 +31,7 @@ const DashboardLayout = () => {
|
||||
</main>
|
||||
<CommandPalette open={paletteOpen} onClose={() => setPaletteOpen(false)} />
|
||||
<LogsDrawer />
|
||||
<PluginLoader api={api} />
|
||||
</div>
|
||||
</LogsDrawerProvider>
|
||||
);
|
||||
|
||||
@@ -9,23 +9,28 @@ const Marketplace = () => {
|
||||
const { user } = useAuth();
|
||||
const [extensions, setExtensions] = useState([]);
|
||||
const [myExtensions, setMyExtensions] = useState([]);
|
||||
const [plugins, setPlugins] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [search, setSearch] = useState('');
|
||||
const [category, setCategory] = useState('');
|
||||
const [tab, setTab] = useState('browse');
|
||||
const [showSubmit, setShowSubmit] = useState(false);
|
||||
const [pluginUrl, setPluginUrl] = useState('');
|
||||
const [installing, setInstalling] = useState(false);
|
||||
const [form, setForm] = useState({ name: '', display_name: '', description: '', category: 'utility', version: '1.0.0', author: '' });
|
||||
|
||||
const categories = ['monitoring', 'security', 'deployment', 'integration', 'ui', 'utility'];
|
||||
|
||||
const loadExtensions = useCallback(async () => {
|
||||
try {
|
||||
const [eData, mData] = await Promise.all([
|
||||
const [eData, mData, pData] = await Promise.all([
|
||||
api.getMarketplaceExtensions(category, search),
|
||||
api.getMyExtensions(),
|
||||
api.getInstalledPlugins().catch(() => ({ plugins: [] })),
|
||||
]);
|
||||
setExtensions(eData.extensions || []);
|
||||
setMyExtensions(mData.extensions || []);
|
||||
setPlugins(pData.plugins || []);
|
||||
} catch (err) {
|
||||
toast.error('Failed to load extensions');
|
||||
} finally {
|
||||
@@ -51,6 +56,42 @@ const Marketplace = () => {
|
||||
} catch (err) { toast.error(err.message); }
|
||||
};
|
||||
|
||||
const handlePluginInstall = async () => {
|
||||
if (!pluginUrl.trim()) return;
|
||||
setInstalling(true);
|
||||
try {
|
||||
const result = await api.installPlugin(pluginUrl.trim());
|
||||
toast.success(`Plugin "${result.display_name}" installed. Restart backend to activate routes.`);
|
||||
setPluginUrl('');
|
||||
loadExtensions();
|
||||
} catch (err) {
|
||||
toast.error(err.message || 'Plugin installation failed');
|
||||
} finally {
|
||||
setInstalling(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePluginUninstall = async (pluginId) => {
|
||||
try {
|
||||
await api.uninstallPlugin(pluginId);
|
||||
toast.success('Plugin uninstalled');
|
||||
loadExtensions();
|
||||
} catch (err) { toast.error(err.message); }
|
||||
};
|
||||
|
||||
const handlePluginToggle = async (plugin) => {
|
||||
try {
|
||||
if (plugin.status === 'active') {
|
||||
await api.disablePlugin(plugin.id);
|
||||
toast.success('Plugin disabled');
|
||||
} else {
|
||||
await api.enablePlugin(plugin.id);
|
||||
toast.success('Plugin enabled');
|
||||
}
|
||||
loadExtensions();
|
||||
} catch (err) { toast.error(err.message); }
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
await api.createMarketplaceExtension(form);
|
||||
@@ -84,6 +125,7 @@ const Marketplace = () => {
|
||||
<div className="tabs">
|
||||
<button className={`tab ${tab === 'browse' ? 'active' : ''}`} onClick={() => setTab('browse')}>Browse</button>
|
||||
<button className={`tab ${tab === 'installed' ? 'active' : ''}`} onClick={() => setTab('installed')}>Installed ({myExtensions.length})</button>
|
||||
<button className={`tab ${tab === 'plugins' ? 'active' : ''}`} onClick={() => setTab('plugins')}>Plugins ({plugins.length})</button>
|
||||
</div>
|
||||
|
||||
{tab === 'browse' && (
|
||||
@@ -142,6 +184,64 @@ const Marketplace = () => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tab === 'plugins' && (
|
||||
<div className="plugins-section">
|
||||
<div className="plugin-install-form card">
|
||||
<h3>Install Plugin from URL</h3>
|
||||
<p className="text-muted">Paste a GitHub repo URL, release URL, or direct zip link.</p>
|
||||
<div className="plugin-install-row">
|
||||
<input
|
||||
className="form-input"
|
||||
placeholder="https://github.com/user/serverkit-plugin"
|
||||
value={pluginUrl}
|
||||
onChange={e => setPluginUrl(e.target.value)}
|
||||
onKeyDown={e => e.key === 'Enter' && handlePluginInstall()}
|
||||
disabled={installing}
|
||||
/>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handlePluginInstall}
|
||||
disabled={installing || !pluginUrl.trim()}
|
||||
>
|
||||
{installing ? 'Installing...' : 'Install'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="installed-list">
|
||||
{plugins.map(plugin => (
|
||||
<div key={plugin.id} className={`installed-item card ${plugin.status === 'error' ? 'installed-item--error' : ''}`}>
|
||||
<div className="installed-item__info">
|
||||
<strong>{plugin.display_name}</strong>
|
||||
<span className="text-muted">v{plugin.version}</span>
|
||||
<span className={`badge badge--${plugin.status === 'active' ? 'success' : plugin.status === 'error' ? 'danger' : 'outline'}`}>
|
||||
{plugin.status}
|
||||
</span>
|
||||
{plugin.has_backend && <span className="badge badge--subtle">Backend</span>}
|
||||
{plugin.has_frontend && <span className="badge badge--subtle">Frontend</span>}
|
||||
</div>
|
||||
{plugin.description && <p className="text-muted" style={{ margin: '4px 0 8px', fontSize: '13px' }}>{plugin.description}</p>}
|
||||
{plugin.error_message && <p className="text-danger" style={{ margin: '4px 0 8px', fontSize: '12px' }}>{plugin.error_message}</p>}
|
||||
<div className="installed-item__actions">
|
||||
<button
|
||||
className={`btn btn-sm ${plugin.status === 'active' ? 'btn-outline' : 'btn-primary'}`}
|
||||
onClick={() => handlePluginToggle(plugin)}
|
||||
>
|
||||
{plugin.status === 'active' ? 'Disable' : 'Enable'}
|
||||
</button>
|
||||
<button className="btn btn-sm btn-danger" onClick={() => handlePluginUninstall(plugin.id)}>Uninstall</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{plugins.length === 0 && (
|
||||
<div className="empty-state">
|
||||
<p>No plugins installed. Use the form above to install one from a URL.</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showSubmit && (
|
||||
<div className="modal-overlay" onClick={() => setShowSubmit(false)}>
|
||||
<div className="modal" onClick={e => e.stopPropagation()}>
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* PluginLoader - Dynamically loads installed plugin components.
|
||||
*
|
||||
* Uses Vite's import.meta.glob to discover plugin entry points at build time.
|
||||
* Plugins installed via the plugin system are placed in src/plugins/<slug>/
|
||||
* and their components are auto-discovered here.
|
||||
*
|
||||
* Each plugin's index.js should export:
|
||||
* - A default component (the widget/UI to render)
|
||||
* - Optionally a Provider component for context wrapping
|
||||
*/
|
||||
import React, { Suspense, useMemo } from 'react';
|
||||
|
||||
// Vite glob import: discovers all plugin index.js files at build time
|
||||
// Each returns { default: Component, Provider?: Component }
|
||||
const pluginModules = import.meta.glob('./**/index.js', { eager: true });
|
||||
|
||||
/**
|
||||
* Get all discovered plugins with their components.
|
||||
*/
|
||||
export function getInstalledPlugins() {
|
||||
const plugins = [];
|
||||
|
||||
for (const [path, mod] of Object.entries(pluginModules)) {
|
||||
// path looks like "./serverkit-ai/index.js"
|
||||
const match = path.match(/^\.\/([^/]+)\/index\.js$/);
|
||||
if (!match) continue;
|
||||
|
||||
const slug = match[1];
|
||||
// Skip internal files
|
||||
if (slug === 'PluginLoader') continue;
|
||||
|
||||
plugins.push({
|
||||
slug,
|
||||
Component: mod.default || mod.AiAssistant || null,
|
||||
Provider: mod.AiAssistantProvider || mod.Provider || null,
|
||||
module: mod,
|
||||
});
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders all installed plugin widgets.
|
||||
* Wraps each in its Provider if one is exported.
|
||||
*
|
||||
* @param {object} props
|
||||
* @param {object} props.api - The ApiService instance to pass to plugins
|
||||
*/
|
||||
const PluginLoader = ({ api }) => {
|
||||
const plugins = useMemo(() => getInstalledPlugins(), []);
|
||||
|
||||
if (plugins.length === 0) return null;
|
||||
|
||||
return (
|
||||
<>
|
||||
{plugins.map(({ slug, Component, Provider }) => {
|
||||
if (!Component) return null;
|
||||
|
||||
const widget = <Component key={slug} api={api} />;
|
||||
|
||||
if (Provider) {
|
||||
return (
|
||||
<Provider key={`provider-${slug}`}>
|
||||
{widget}
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
|
||||
return widget;
|
||||
})}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default PluginLoader;
|
||||
@@ -9,6 +9,7 @@ import * as systemMethods from './system.js';
|
||||
import * as securityMethods from './security.js';
|
||||
import * as fileMethods from './files.js';
|
||||
import * as dnsMethods from './dns.js';
|
||||
import * as pluginMethods from './plugins.js';
|
||||
|
||||
class ApiService extends ApiClient {
|
||||
constructor() {
|
||||
@@ -25,6 +26,7 @@ class ApiService extends ApiClient {
|
||||
securityMethods,
|
||||
fileMethods,
|
||||
dnsMethods,
|
||||
pluginMethods,
|
||||
];
|
||||
for (const mod of modules) {
|
||||
for (const [key, fn] of Object.entries(mod)) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Plugin management API methods
|
||||
|
||||
export async function getInstalledPlugins(status) {
|
||||
const params = status ? `?status=${status}` : '';
|
||||
return this.request(`/plugins${params}`);
|
||||
}
|
||||
|
||||
export async function getPlugin(pluginId) {
|
||||
return this.request(`/plugins/${pluginId}`);
|
||||
}
|
||||
|
||||
export async function installPlugin(url) {
|
||||
return this.request('/plugins/install', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ url }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function uninstallPlugin(pluginId) {
|
||||
return this.request(`/plugins/${pluginId}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
export async function enablePlugin(pluginId) {
|
||||
return this.request(`/plugins/${pluginId}/enable`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
export async function disablePlugin(pluginId) {
|
||||
return this.request(`/plugins/${pluginId}/disable`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
@@ -74,11 +74,50 @@
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: $spacing-md;
|
||||
flex-wrap: wrap;
|
||||
|
||||
&__info {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: $spacing-sm;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
&__actions {
|
||||
display: flex;
|
||||
gap: $spacing-sm;
|
||||
}
|
||||
|
||||
&--error {
|
||||
border-color: rgba(239, 68, 68, 0.3);
|
||||
}
|
||||
}
|
||||
|
||||
// Plugin install section
|
||||
.plugins-section {
|
||||
margin-top: $spacing-lg;
|
||||
}
|
||||
|
||||
.plugin-install-form {
|
||||
padding: $spacing-lg;
|
||||
margin-bottom: $spacing-lg;
|
||||
|
||||
h3 {
|
||||
margin: 0 0 $space-1;
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 0 0 $spacing-md;
|
||||
font-size: $font-size-sm;
|
||||
}
|
||||
}
|
||||
|
||||
.plugin-install-row {
|
||||
display: flex;
|
||||
gap: $spacing-sm;
|
||||
|
||||
.form-input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user