Added HTTP Server Reference

This commit is contained in:
Matt Dunleavy
2024-07-07 01:46:54 -04:00
parent fdc491362e
commit e7083816a3
2 changed files with 178 additions and 49 deletions

121
README.md
View File

@@ -1,6 +1,6 @@
# <img src="assets/img/plugin-manager-go-256.png" style="float:right"/>Plugin Manager for Go # <img src="assets/img/plugin-manager-go-256.png" style="float:right"/>Plugin Manager for Go
A robust plugin management library for Go applications. A robust and flexible plugin management library for Go applications.
##### Features ##### Features
@@ -56,9 +56,9 @@ manager, err := pm.NewManager("plugins.json", "./plugins", "public_key.pem")
**Parameters:** **Parameters:**
- `configPath` (string): Path to the JSON configuration file for managing enabled/disabled plugins. - `configPath` (string): Path to the JSON configuration file for managing enabled/disabled plugins. ("plugins.json")
- `pluginDir` (string): Directory where plugins are stored. - `pluginDir` (string): Directory where plugins are stored. ("./plugins")
- `publicKeyPath` (string): Path to the public key file used for verifying plugin signatures. - `publicKeyPath` (string): Path to the public key file used for verifying plugin signatures. ("public_key.pem")
**Returns:** **Returns:**
@@ -167,16 +167,19 @@ manager.SubscribeToEvent("PluginLoaded", func(e pm.Event) {
Plugins must implement the `Plugin` interface. Plugins must implement the `Plugin` interface.
#### Plugin (struct)
The `Plugin` struct defines the basic structure of your plugin. It can be empty if your plugin doesn't need to store any state, or you can add fields if your plugin needs to maintain state across method calls.
```go ```go
package main
import (
"fmt"
pm "github.com/matt-dunleavy/plugin-manager"
)
type MyPlugin struct{} type MyPlugin struct{}
```
#### Metadata()
The `Metadata()` method returns metadata about the plugin, including the `Name`, `Version`, and `Dependencies` (a map of other plugins that this plugin depends on, with the key being the plugin's name, and the value being the version constraint).
```go
func (p *MyPlugin) Metadata() pm.PluginMetadata { func (p *MyPlugin) Metadata() pm.PluginMetadata {
return pm.PluginMetadata{ return pm.PluginMetadata{
Name: "MyPlugin", Name: "MyPlugin",
@@ -184,37 +187,79 @@ func (p *MyPlugin) Metadata() pm.PluginMetadata {
Dependencies: map[string]string{}, Dependencies: map[string]string{},
} }
} }
```
#### Preload()
The `Preload()` method is called before the plugin is fully loaded. Use it for any setup that needs to happen before initialization.
```go
func (p *MyPlugin) PreLoad() error { func (p *MyPlugin) PreLoad() error {
fmt.Println("MyPlugin pre-load") fmt.Println("MyPlugin pre-load")
return nil return nil
} }
```
#### Init()
The `Init()` method is called to initialize the plugin. Use it to set up any resources or state the plugin needs.
```go
func (p *MyPlugin) Init() error { func (p *MyPlugin) Init() error {
fmt.Println("MyPlugin initialized") fmt.Println("MyPlugin initialized")
return nil return nil
} }
```
#### PostLoad()
The `PostLoad()` method is called after the plugin is fully loaded. Use it for any final setup steps.
```go
func (p *MyPlugin) PostLoad() error { func (p *MyPlugin) PostLoad() error {
fmt.Println("MyPlugin post-load") fmt.Println("MyPlugin post-load")
return nil return nil
} }
```
#### Execute()
`Execute()` is the main method of your plugin. It is called when the plugin manager executes your plugin.
```go
func (p *MyPlugin) Execute() error { func (p *MyPlugin) Execute() error {
fmt.Println("MyPlugin executed") fmt.Println("MyPlugin executed")
return nil return nil
} }
```
#### PreUnload()
The `PreUnload()` method is called before the plugin is unloaded. Use it to prepare for shutdown.
```Go
func (p *MyPlugin) PreUnload() error { func (p *MyPlugin) PreUnload() error {
fmt.Println("MyPlugin pre-unload") fmt.Println("MyPlugin pre-unload")
return nil return nil
} }
```
#### Shutdown()
The `Shutdown()` method is called when the plugin is being unloaded. Use it to clean up any resources the plugin has allocated.
```Go
func (p *MyPlugin) Shutdown() error { func (p *MyPlugin) Shutdown() error {
fmt.Println("MyPlugin shut down") fmt.Println("MyPlugin shut down")
return nil return nil
} }
```
#### Plugin Variable
This variable is how the plugin manager discovers your plugin. It must be named `Plugin` and be of the type that implements the plugin interface.
```go
var Plugin MyPlugin var Plugin MyPlugin
``` ```
@@ -226,18 +271,14 @@ var Plugin MyPlugin
> >
> When implementing your own plugin, you would replace the `fmt.Println` statements with your actual plugin logic. The `PreLoad`, `PostLoad`, `PreUnload`, and `Shutdown` methods allow you to manage the lifecycle of your plugin, while `Init` and `Execute` form the core functionality. > When implementing your own plugin, you would replace the `fmt.Println` statements with your actual plugin logic. The `PreLoad`, `PostLoad`, `PreUnload`, and `Shutdown` methods allow you to manage the lifecycle of your plugin, while `Init` and `Execute` form the core functionality.
## Compiling Plugins ## Compiling Plugins
Compile your plugin with the standard Go toolchain by setting the `-buildmode` flag to `plugin`: Compile a plugin using the standard Go compiler toolchain by setting the `-buildmode` flag to `plugin`:
```bash ```bash
go build -buildmode=plugin -o myplugin.so myplugin.go go build -buildmode=plugin -o myplugin.so myplugin.go
``` ```
## Configuration ## Configuration
The plugin manager uses a JSON configuration file to keep track of enabled plugins. Here's an example `plugins.json`: The plugin manager uses a JSON configuration file to keep track of enabled plugins. Here's an example `plugins.json`:
@@ -279,46 +320,28 @@ The plugin manager uses a JSON configuration file to keep track of enabled plugi
### Step-by-Step Guide to Implementing and Deploying Redbean ### Step-by-Step Guide to Implementing and Deploying Redbean
1. ##### **Initialize the Plugin Manager** #### **Setup a Remote Repository**
```go
manager, err := pm.NewManager("plugins.json", "./plugins", "public_key.pem")
if err != nil {
log.Fatalf("Failed to create plugin manager: %v", err)
}
```
2. ##### **Setup the Remote Repository**
```go ```go
repo, err := manager.SetupRemoteRepository("user@example.com:/path/to/repo", "/path/to/ssh/key") repo, err := manager.SetupRemoteRepository("user@example.com:/path/to/repo", "/path/to/ssh/key")
if err != nil {
log.Fatalf("Failed to setup remote repository: %v", err)
}
``` ```
3. ##### **Prepare Local Directory for Deployment**. #### Prepare Local Directory for Deployment
Create a local directory to store your plugins and repository structure: Create a local directory to store your plugins and repository structure:
```go ```go
localRepoPath := "./repository" localRepoPath := "./repository"
if err := os.MkdirAll(localRepoPath, 0755); err != nil {
log.Fatalf("Failed to create local repository directory: %v", err)
}
``` ```
4. ##### **Add Plugins to the Local Repository.** #### **Add Plugins to the Local Repository.**
Copy or move your plugin files to the local repository directory. Copy or move your plugin files to the local repository directory.
5. ##### **Deploy the Repository** #### **Deploy the Repository**
```go ```go
err = manager.DeployRepository(repo, localRepoPath) err = manager.DeployRepository(repo, localRepoPath)
if err != nil {
log.Fatalf("Failed to deploy repository: %v", err)
}
``` ```
This step will: This step will:
@@ -326,9 +349,9 @@ This step will:
- Package your plugins and repository structure into a Redbean executable - Package your plugins and repository structure into a Redbean executable
- Deploy the Redbean executable to your remote server (if a remote URL was provided) - Deploy the Redbean executable to your remote server (if a remote URL was provided)
6. ##### **Verify Deployment** #### **Verify Deployment**
If deployed remotely, SSH into your server and check that the Redbean executable is present and running: If deployed remotely, SSH into your server and check that the Redbean executable is present and running:
```bash ```bash
ssh user@example.com ssh user@example.com
@@ -336,17 +359,17 @@ This step will:
ps aux | grep redbean ps aux | grep redbean
``` ```
##### **Access Your Plugin Repository** #### **Access Your Plugin Repository**
Your plugins are now accessible via HTTP/HTTPS. If Redbean is running on the default port, you can access your plugins at: `http://your-server-address:8080/plugins/` Your plugins are now accessible via HTTP/HTTPS. If Redbean is running on the default port, you can access your plugins at: `http://your-server-address:8080/plugins/`
##### **Update Repository** #### **Update Repository**
To update your repository, simply repeat steps 4-5. The plugin manager will handle updating the Redbean executable and redeploying your changes. To update your repository, simply repeat steps 4-5. The plugin manager will handle updating the Redbean executable and redeploying your changes.
#### Advanced Configuration ### Advanced Configuration
Redbean offers various configuration options. You can create a `redbean.ini` file in your local repository directory to customize settings: When deploying your plugin repository via redbean, the plugin manager will include a `redbean.ini` file that you can use to customize your repository's server configuration.
```ini ```ini
[server] [server]
@@ -354,7 +377,7 @@ port = 9000
addr = 127.0.0.1 addr = 127.0.0.1
``` ```
The plugin manager will include this configuration when deploying your repository. Refer to the [redbean.ini reference](docs/redbean.md) for a comprehensive list of commands.
> [!IMPORTANT] > [!IMPORTANT]
> >

106
docs/redbean.md Normal file
View File

@@ -0,0 +1,106 @@
### Configuration Options for redbean.ini
[redbean](https://redbean.dev/) is an open source webserver in a zip executable that runs on six operating systems.
##### Server Settings:
- `port`: The port number the server listens on (default: 8080)
- `addr`: The IP address to bind to (default: 0.0.0.0)
- `daemon`: Run as a daemon (true/false)
- `log`: Path to the log file
- `access_log`: Path to the access log file
- `error_log`: Path to the error log file
- `pid`: Path to the PID file
- `user`: User to run the server as
- `group`: Group to run the server as
- `chroot`: Directory to chroot into
- `ssl`: Enable SSL (true/false)
- `ssl_cert`: Path to SSL certificate file
- `ssl_key`: Path to SSL key file
- `ssl_password`: Password for SSL key file
1. ##### MIME Types:
- `mime_type`: Set custom MIME types (e.g., `mime_type.xyz=application/x-xyz`)
2. ##### URL Rewriting:
- `rewrite`: URL rewrite rules
3. ##### Directory Listings:
- `dir_list`: Enable directory listings (true/false)
- `dir_index`: Default index file names (comma-separated)
4. ##### CGI Settings:
- `cgi_timeout`: Timeout for CGI scripts (in seconds)
- `cgi_dir`: Directory for CGI scripts
5. ##### Lua Settings:
- `lua_path`: Lua module search path
- `lua_cpath`: Lua C module search path
6. ##### Security Settings:
- `access_control_allow_origin`: Set CORS headers
- `strict_transport_security`: Set HSTS header
- `content_security_policy`: Set CSP header
7. ##### Performance Settings:
- `workers`: Number of worker threads
- `max_connections`: Maximum number of simultaneous connections
- `keep_alive_timeout`: Keep-alive timeout (in seconds)
- `gzip`: Enable gzip compression (true/false)
- `gzip_types`: MIME types to compress (comma-separated)
8. ##### Caching:
- `cache_control`: Set Cache-Control header
- `etag`: Enable ETag header (true/false)
9. ##### Custom Error Pages:
- `error_page`: Set custom error pages (e.g., `error_page.404=/custom_404.html`)
10. ##### Virtual Hosts:
- `vhost`: Configure virtual hosts
11. ##### Proxy Settings:
- `proxy_pass`: Configure reverse proxy settings
12. ##### WebSocket Settings:
- `websocket`: Enable WebSocket support (true/false)
13. ##### Basic Authentication:
- `auth_basic`: Enable basic authentication
- `auth_basic_user_file`: Path to the user file for basic authentication
14. ##### Rate Limiting:
- `limit_req`: Configure request rate limiting
15. ##### IP Filtering:
- `allow`: Allow specific IP addresses or ranges
- `deny`: Deny specific IP addresses or ranges
16. ##### File Serving:
- `alias`: Create aliases for directories
- `try_files`: Specify a list of files to try when serving requests
17. ##### Miscellaneous:
- `server_tokens`: Control the emission of the Server header (on/off)
- `client_max_body_size`: Maximum allowed size of the client request body
> [!IMPORTANT]
>
> Remember that the availability and syntax of these options may vary slightly depending on the version of redbean you're using. Always refer to the [official redbean documentation](https://redbean.dev/) for the most up-to-date and version-specific information.