mirror of https://github.com/statping/statping
upgrades - plugins
parent
4df559efc7
commit
a95f725348
18
plugins.go
18
plugins.go
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pluginRoutes []*plugins.Routing
|
pluginRoutes []*plugins.Routing
|
||||||
allPlugins []*plugins.Plugin
|
allPlugins []*plugins.Plugin
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitPluginsDatabase() {
|
func InitPluginsDatabase() {
|
||||||
|
@ -17,6 +17,22 @@ func SetAuthorized() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AfterInstall() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func AfterUninstall() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func AfterSave() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func Routes() []*plugins.Routing {
|
func Routes() []*plugins.Routing {
|
||||||
return plugins.PluginRoutes
|
return plugins.PluginRoutes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AllPlugins() []*plugins.Plugin {
|
||||||
|
return plugins.Plugins
|
||||||
|
}
|
||||||
|
|
|
@ -2,18 +2,22 @@ package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"net/http"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
PluginRoutes []*Routing
|
PluginRoutes []*Routing
|
||||||
Plugins []*Plugin
|
Plugins []*Plugin
|
||||||
)
|
)
|
||||||
|
|
||||||
type Plugin struct {
|
type Plugin struct {
|
||||||
Name string
|
Name string
|
||||||
|
InstallSQL string
|
||||||
|
InstallFunc func()
|
||||||
|
UninstallFunc func()
|
||||||
|
SaveFunc func()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Routing struct {
|
type Routing struct {
|
||||||
|
@ -22,9 +26,8 @@ type Routing struct {
|
||||||
Handler func(http.ResponseWriter, *http.Request)
|
Handler func(http.ResponseWriter, *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Add(name string) {
|
func (p *Plugin) Add() {
|
||||||
plugin := &Plugin{name}
|
Plugins = append(Plugins, p)
|
||||||
Plugins = append(Plugins, plugin)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddRoute(url string, method string, handle func(http.ResponseWriter, *http.Request)) {
|
func AddRoute(url string, method string, handle func(http.ResponseWriter, *http.Request)) {
|
||||||
|
@ -32,14 +35,28 @@ func AddRoute(url string, method string, handle func(http.ResponseWriter, *http.
|
||||||
PluginRoutes = append(PluginRoutes, route)
|
PluginRoutes = append(PluginRoutes, route)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Authenticated(r *http.Request) bool {
|
func (p *Plugin) InstallPlugin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p.InstallFunc()
|
||||||
|
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Plugin) UninstallPlugin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p.UninstallFunc()
|
||||||
|
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Plugin) SavePlugin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p.SaveFunc()
|
||||||
|
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Authenticated(r *http.Request) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func log(msg... string) {
|
func log(msg ...string) {
|
||||||
fmt.Println(" @plugins: ",msg)
|
fmt.Println(" @plugins: ", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDB(database *sql.DB) {
|
func InitDB(database *sql.DB) {
|
||||||
|
|
|
@ -2,68 +2,66 @@ package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SLACK_TABLE = "plugin_slack"
|
SLACK_TABLE = "plugin_slack"
|
||||||
|
SLACK_INSTALL = "CREATE TABLE " + SLACK_TABLE + " (enabled BOOLEAN, api_key text, api_secret text, channel text);"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
Add("slack")
|
|
||||||
AddRoute("install_slack", "GET", InstallSlack)
|
|
||||||
AddRoute("uninstall_slack", "GET", UninstallSlack)
|
|
||||||
AddRoute("save_slack", "POST", SaveSettings)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Slack struct {
|
type Slack struct {
|
||||||
Key string
|
Key string
|
||||||
Secret string
|
Secret string
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Channel string
|
Channel string
|
||||||
|
InstallFunc func()
|
||||||
|
UninstallFunc func()
|
||||||
|
SaveFunc func(*http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstallSlack(w http.ResponseWriter, r *http.Request) {
|
func init() {
|
||||||
CreateTable()
|
|
||||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
|
||||||
}
|
|
||||||
|
|
||||||
func UninstallSlack(w http.ResponseWriter, r *http.Request) {
|
plugin := &Plugin{
|
||||||
DropTable()
|
"slack",
|
||||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
SLACK_INSTALL,
|
||||||
}
|
InstallSlack,
|
||||||
|
UninstallSlack,
|
||||||
func SaveSettings(w http.ResponseWriter, r *http.Request) {
|
SaveSlack,
|
||||||
r.ParseForm()
|
|
||||||
key := r.PostForm.Get("key")
|
|
||||||
secret := r.PostForm.Get("secret")
|
|
||||||
enabled, _ := strconv.ParseBool(r.PostForm.Get("enabled"))
|
|
||||||
channel := r.PostForm.Get("channel")
|
|
||||||
|
|
||||||
slack := &Slack {
|
|
||||||
key,
|
|
||||||
secret,
|
|
||||||
enabled,
|
|
||||||
channel,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slack.UpdateTable()
|
plugin.Add()
|
||||||
|
|
||||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InstallSlack() {
|
||||||
|
CreateTable()
|
||||||
|
}
|
||||||
|
|
||||||
|
func UninstallSlack() {
|
||||||
|
DropTable()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveSlack() {
|
||||||
|
//key := r.PostForm.Get("key")
|
||||||
|
//secret := r.PostForm.Get("secret")
|
||||||
|
//enabled, _ := strconv.ParseBool(r.PostForm.Get("enabled"))
|
||||||
|
//channel := r.PostForm.Get("channel")
|
||||||
|
|
||||||
|
//slack.UpdateTable()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func CreateTable() {
|
func CreateTable() {
|
||||||
sql := "CREATE TABLE "+SLACK_TABLE+" (enabled BOOLEAN, api_key text, api_secret text, channel text);"
|
sql := "CREATE TABLE " + SLACK_TABLE + " (enabled BOOLEAN, api_key text, api_secret text, channel text);"
|
||||||
db.QueryRow(sql).Scan()
|
db.QueryRow(sql).Scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Slack) UpdateTable() {
|
func (s *Slack) UpdateTable() {
|
||||||
sql := "CREATE TABLE "+SLACK_TABLE+" (enabled BOOLEAN, api_key text, api_key text, channel text);"
|
sql := "CREATE TABLE " + SLACK_TABLE + " (enabled BOOLEAN, api_key text, api_key text, channel text);"
|
||||||
db.QueryRow(sql).Scan()
|
db.QueryRow(sql).Scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DropTable() {
|
func DropTable() {
|
||||||
sql := "DROP TABLE "+SLACK_TABLE+";"
|
sql := "DROP TABLE " + SLACK_TABLE + ";"
|
||||||
db.QueryRow(sql).Scan()
|
db.QueryRow(sql).Scan()
|
||||||
}
|
}
|
||||||
|
|
8
web.go
8
web.go
|
@ -39,9 +39,11 @@ func RunHTTPServer() {
|
||||||
r.Handle("/plugins", http.HandlerFunc(PluginsHandler))
|
r.Handle("/plugins", http.HandlerFunc(PluginsHandler))
|
||||||
r.Handle("/help", http.HandlerFunc(HelpHandler))
|
r.Handle("/help", http.HandlerFunc(HelpHandler))
|
||||||
|
|
||||||
for _, route := range Routes() {
|
for _, plugin := range AllPlugins() {
|
||||||
fmt.Printf("Adding plugin route: /plugins/%v\n", route.URL)
|
fmt.Printf("Adding plugin: %v\n", plugin.Name)
|
||||||
r.Handle("/plugins/"+route.URL, http.HandlerFunc(route.Handler)).Methods(route.Method)
|
r.Handle("/plugins/install_"+plugin.Name, http.HandlerFunc(plugin.InstallPlugin)).Methods("GET")
|
||||||
|
r.Handle("/plugins/uninstall_"+plugin.Name, http.HandlerFunc(plugin.UninstallPlugin)).Methods("GET")
|
||||||
|
r.Handle("/plugins/save_"+plugin.Name, http.HandlerFunc(plugin.SavePlugin)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|
Loading…
Reference in New Issue