Skip to content

Commit cf329e7

Browse files
MrNaif2018gitbook-bot
authored andcommitted
GITBOOK-88: Add plugins development docs
1 parent 0b16abc commit cf329e7

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
## Guides
2626

27+
* [Bitcart Plugins Development](guides/bitcart-plugins-dev.md)
2728
* [Default nodes/your own node](guides/default-nodes-your-own-node.md)
2829
* [One domain mode](guides/one-domain-mode.md)
2930
* [Backups](guides/backups.md)

guides/bitcart-plugins-dev.md

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
# Bitcart Plugins Development
2+
3+
### Creating a New Plugin
4+
5+
Bitcart plugins are modular components that can extend the functionality of your Bitcart instance. A plugin can consist of one or more of the following components:
6+
7+
* **Backend**: Server-side logic, database models, and API endpoints
8+
* **Admin**: Extensions to the admin panel UI
9+
* **Store**: Customizations for the store frontend
10+
* **Docker**: Docker compose deploy customizations
11+
12+
To create a new plugin, use the [Bitcart CLI](https://github.com/bitcart/bitcart-cli):
13+
14+
```bash
15+
bitcart-cli plugin init .
16+
```
17+
18+
This will guide you through creating a new plugin by asking for:
19+
20+
* Plugin name
21+
* Author
22+
* Description
23+
* Component types to include
24+
25+
To install plugin to develop alongside bitcart, you can use `bitcart-cli plugin install .`
26+
27+
It is recommended to pass `--dev` flag to install plugin in development mode. It uses symlinks to link the plugin to the bitcart source code, so when you make changes to the plugin, they are reflected immediately.
28+
29+
It asks for paths where you cloned bitcart, bitcart-admin and other repositories. You can pass `--save` flag to save paths to config, so that it is never asked again.
30+
31+
After you are done developing and want to remove it from codebase:
32+
33+
```bash
34+
bitcart-cli plugin uninstall your_plugin_name
35+
```
36+
37+
### Backend Plugin Development
38+
39+
#### Plugin Structure
40+
41+
A backend plugin must have a `plugin.py` file which as a `Plugin` class that implements the `BasePlugin` class from `api.plugins`. The basic structure is:
42+
43+
```python
44+
from api.plugins import BasePlugin
45+
46+
class Plugin(BasePlugin):
47+
name = "your_plugin_name"
48+
49+
def setup_app(self, app):
50+
# Configure FastAPI app, add routes
51+
pass
52+
53+
async def startup(self):
54+
# Initialize plugin, register hooks
55+
pass
56+
57+
async def shutdown(self):
58+
# Cleanup resources
59+
pass
60+
61+
async def worker_setup(self):
62+
# Setup background tasks
63+
pass
64+
```
65+
66+
#### Database Models
67+
68+
To add custom database tables:
69+
70+
1. Create a `models.py` file in your plugin directory
71+
2. Define SQLAlchemy models
72+
3. Manage migrations using alembic:
73+
74+
```bash
75+
python scripts/pluginmigrate.py your_plugin_name revision --autogenerate -m "Migration message"
76+
```
77+
78+
```bash
79+
python scripts/pluginmigrate.py your_plugin_name upgrade head
80+
```
81+
82+
Basically same commands as alembic, but you provide your plugin name.
83+
84+
This will create a new migration in the `versions/` directory. Migrations are automatically applied when the plugin is loaded.
85+
86+
#### Hooks and Filters
87+
88+
Bitcart provides two extension mechanisms:
89+
90+
1. **Hooks**: Execute actions at specific points
91+
2. **Filters**: Modify data as it flows through the system
92+
93+
```python
94+
from api.plugins import register_hook, register_filter
95+
96+
# Register a hook
97+
register_hook("event_name", async_handler_function)
98+
99+
# Register a filter
100+
register_filter("filter_name", async_filter_function)
101+
```
102+
103+
To find hooks and filters, you can search the codebase for `apply_filters` and `run_hook`.
104+
105+
For example, `db_modify_wallet` would be called right after wallet is saved in db.
106+
107+
So you can use it like this:
108+
109+
```python
110+
from api.plugins import register_hook
111+
112+
class Plugin(BasePlugin):
113+
...
114+
115+
async def startup(self):
116+
register_hook("db_modify_wallet", self.my_hander)
117+
118+
async def my_handler(self, wallet):
119+
print(wallet)
120+
```
121+
122+
And in filters first argument you receive is the value which you can return unmodified or modify. Other arguments are optional data
123+
124+
Example:
125+
126+
```python
127+
from api.plugins import register_filter, SKIP_PAYMENT_METHOD
128+
129+
class Plugin(BasePlugin):
130+
...
131+
132+
async def startup(self):
133+
register_filter("create_payment_method", self.create_payment_method)
134+
135+
async def create_payment_method(self, method, wallet, coin, amount, invoice, product, store, lightning):
136+
# multiple options:
137+
# return SKIP_PAYMENT_METHOD # skip this method, e.g. if error occured
138+
# return method # fallback to bitcart default code, e.g. call add_request on the daemon
139+
return {
140+
"payment_address": "youraddress",
141+
"payment_url": "willbeinsideqrcode",
142+
"lookup_field": "for your reference",
143+
"metadata": {"somekey": "somevalue"},
144+
}
145+
```
146+
147+
#### Plugin Settings
148+
149+
To add custom settings for your plugin:
150+
151+
```python
152+
from pydantic import BaseModel
153+
from api.plugins import register_settings
154+
155+
class PluginSettings(BaseModel):
156+
setting1: str = ""
157+
setting2: bool = False
158+
159+
register_settings("your_plugin_name", PluginSettings)
160+
```
161+
162+
Settings are automatically available in the admin panel and can be accessed via:
163+
164+
```python
165+
from api.plugins import get_plugin_settings
166+
167+
settings = await get_plugin_settings("your_plugin_name")
168+
```
169+
170+
#### Dependencies
171+
172+
If you need to add backend dependencies, create a requirements.txt file in your plugin directory, it will be installed automatically.
173+
174+
```
175+
your_plugin_name/
176+
├── requirements.txt
177+
```
178+
179+
### Frontend Plugin Development
180+
181+
#### Admin Panel Extensions
182+
183+
Admin plugins can extend the UI using the our plugin system based on [vuems](https://github.com/ergonode/vuems). The basic structure is in e.g. `admin/your_plugin_name`:
184+
185+
```
186+
your_plugin_name/
187+
├── config/
188+
│ ├── extends.js # Component and dictionary registration
189+
│ ├── index.js # Plugin configuration
190+
│ └── routes.js # Custom routes
191+
├── pages/ # Custom pages
192+
└── components/ # Custom components
193+
└── package.json # dependencies
194+
```
195+
196+
#### Extending the UI
197+
198+
1. Register custom components in `extends.js`:
199+
200+
```javascript
201+
export default {
202+
extendComponents: {
203+
// Add components to specific UI slots
204+
'dashboard-widgets': [MyWidget],
205+
'invoice-details': [CustomInvoiceInfo]
206+
},
207+
dictionaries: {
208+
// Add custom data dictionaries
209+
'my-data': { /* ... */ }
210+
}
211+
}
212+
```
213+
214+
2. Add custom routes in `routes.js`:
215+
216+
```javascript
217+
import CustomPage from '../pages/CustomPage'
218+
219+
export default [
220+
{
221+
name: 'custom-page',
222+
path: '/custom',
223+
component: CustomPage
224+
}
225+
]
226+
```
227+
228+
3. Add dependencies to `package.json`
229+
230+
#### Store Extensions
231+
232+
Store plugins follow a similar structure to admin plugins but are specifically for the store frontend. They can:
233+
234+
* Add custom pages
235+
* Extend existing components
236+
* Add new payment methods
237+
* Customize the checkout process
238+
239+
### Plugin Packaging
240+
241+
To package your plugin:
242+
243+
```bash
244+
bitcart-cli plugin package .
245+
```
246+
247+
This creates a `.bitcart` file that can be installed on any Bitcart instance.
248+
249+
### Best Practices
250+
251+
1. Use descriptive names for hooks and filters
252+
2. Document your plugin's requirements and dependencies
253+
3. Handle errors gracefully
254+
4. Clean up resources in the shutdown method
255+
5. Follow the existing code style
256+
6. Test your plugin thoroughly before distribution
257+
258+
### Plugin Lifecycle
259+
260+
1. Plugin discovery and loading
261+
2. Database migrations
262+
3. Plugin initialization (startup)
263+
4. Hook/filter registration
264+
5. Settings registration
265+
6. UI component registration (if applicable)
266+
7. Background worker setup (if needed)
267+
8. Shutdown cleanup on server stop

0 commit comments

Comments
 (0)