From 3694a50f6b498b1078e84f3e2e0be460729a64b6 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Wed, 4 Jul 2018 14:14:20 -0700 Subject: [PATCH] Updated Statup Plugins (markdown) --- Statup-Plugins.md | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/Statup-Plugins.md b/Statup-Plugins.md index 9b5c4e6..2d2f357 100644 --- a/Statup-Plugins.md +++ b/Statup-Plugins.md @@ -13,6 +13,10 @@ Start off with the [Example Statup Plugin](https://github.com/hunterlong/statup_ ## Building Plugins Plugins don't need a push request and they can be private! You'll need to compile your plugin to the Golang `.so` binary format. Once you've built your plugin, insert it into the `plugins` folder in your Statup directory and reboot the application. Clone the [Example Statup Plugin](https://github.com/hunterlong/statup_plugin) repo and try to build it yourself! +#### Build Requirements +- You must have `main.go` +- You must create the Plugin variable on `init()` + ```bash git clone https://github.com/hunterlong/statup_plugin cd statup-plugin @@ -26,13 +30,13 @@ Statup includes a couple tools to help you on your Plugin journey, you can use ` statup test plugins ```

- +

Your plugin should be able to parse and receive events before distributing it. The test tools creates a temporary database (SQLite) that your plugin can interact with. Statup uses [upper.io/db.v3](https://upper.io/db.v3) for database interactions. The database is passed to your plugin `OnLoad(db sqlbuilder.Database)`, so you can use the `db` variable passed here. ## Statup Plugin Interface -Please remember Golang plugin's are very new and Statup plugin package may change and 'could' brake your plugin. +Please remember Golang plugin's are very new and Statup plugin package may change and 'could' brake your plugin. Checkout the [statup/plugin package](https://github.com/hunterlong/statup/blob/master/plugin/main.go) to see the most current interfaces. ```go type PluginActions interface { GetInfo() Info @@ -90,9 +94,41 @@ func (p pkg) OnLoad(db sqlbuilder.Database) { ``` ###### OnLoad is fired after plugin is loaded into the environment + +## Interacting with Database +The Example Statup Plugin includes a variable `Database` that will allow you to interact with the Statup database. Checkout [database.go](https://github.com/hunterlong/statup_plugin/blob/master/database.go) to see a full example of Create, Read, Update and then Deleting a custom Communication entry into the database. +```go +// Insert a new communication into database +// once inserted, return the Communication +func (c *Communication) Create() *Communication { + uuid, err := CommunicationTable().Insert(c) + if err != nil { + panic(err) + } + c.Id = uuid.(int64) + return c +} +``` + ## Custom HTTP Routes -Plugin's can include their own HTTP route to accept GET/POST requests. +Plugin's can include their own HTTP route to accept GET/POST requests. Route are loaded after Statup loads all of it's Routes. Checkout [routes.go](https://github.com/hunterlong/statup_plugin/blob/master/routes.go) on the example plugin to see a full example of how to use it. +```go +// You must have a Routes() method in your plugin +func (p *pkg) Routes() []plugin.Routing { + return []plugin.Routing{{ + URL: "hello", + Method: "GET", + Handler: CustomInfoHandler, + }} +} + +// This is the HTTP handler for the '/hello' URL created above +func CustomInfoHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "Oh Wow!!! This is cool...") +} +``` + ## Plugin To-Do List -- [ ] Ability to read/write to database - [ ] Ability to includes assets like jpg, json, etc \ No newline at end of file