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 (
|
||||
pluginRoutes []*plugins.Routing
|
||||
allPlugins []*plugins.Plugin
|
||||
allPlugins []*plugins.Plugin
|
||||
)
|
||||
|
||||
func InitPluginsDatabase() {
|
||||
|
@ -17,6 +17,22 @@ func SetAuthorized() {
|
|||
|
||||
}
|
||||
|
||||
func AfterInstall() {
|
||||
|
||||
}
|
||||
|
||||
func AfterUninstall() {
|
||||
|
||||
}
|
||||
|
||||
func AfterSave() {
|
||||
|
||||
}
|
||||
|
||||
func Routes() []*plugins.Routing {
|
||||
return plugins.PluginRoutes
|
||||
}
|
||||
|
||||
func AllPlugins() []*plugins.Plugin {
|
||||
return plugins.Plugins
|
||||
}
|
||||
|
|
|
@ -2,18 +2,22 @@ package plugins
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
db *sql.DB
|
||||
PluginRoutes []*Routing
|
||||
Plugins []*Plugin
|
||||
Plugins []*Plugin
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
Name string
|
||||
Name string
|
||||
InstallSQL string
|
||||
InstallFunc func()
|
||||
UninstallFunc func()
|
||||
SaveFunc func()
|
||||
}
|
||||
|
||||
type Routing struct {
|
||||
|
@ -22,9 +26,8 @@ type Routing struct {
|
|||
Handler func(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
func Add(name string) {
|
||||
plugin := &Plugin{name}
|
||||
Plugins = append(Plugins, plugin)
|
||||
func (p *Plugin) Add() {
|
||||
Plugins = append(Plugins, p)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func log(msg... string) {
|
||||
fmt.Println(" @plugins: ",msg)
|
||||
func log(msg ...string) {
|
||||
fmt.Println(" @plugins: ", msg)
|
||||
}
|
||||
|
||||
func InitDB(database *sql.DB) {
|
||||
|
|
|
@ -2,68 +2,66 @@ package plugins
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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 {
|
||||
Key string
|
||||
Secret string
|
||||
Enabled bool
|
||||
Channel string
|
||||
Key string
|
||||
Secret string
|
||||
Enabled bool
|
||||
Channel string
|
||||
InstallFunc func()
|
||||
UninstallFunc func()
|
||||
SaveFunc func(*http.Request)
|
||||
}
|
||||
|
||||
func InstallSlack(w http.ResponseWriter, r *http.Request) {
|
||||
CreateTable()
|
||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||
}
|
||||
func init() {
|
||||
|
||||
func UninstallSlack(w http.ResponseWriter, r *http.Request) {
|
||||
DropTable()
|
||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func SaveSettings(w http.ResponseWriter, r *http.Request) {
|
||||
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,
|
||||
plugin := &Plugin{
|
||||
"slack",
|
||||
SLACK_INSTALL,
|
||||
InstallSlack,
|
||||
UninstallSlack,
|
||||
SaveSlack,
|
||||
}
|
||||
|
||||
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() {
|
||||
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()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func DropTable() {
|
||||
sql := "DROP TABLE "+SLACK_TABLE+";"
|
||||
sql := "DROP TABLE " + SLACK_TABLE + ";"
|
||||
db.QueryRow(sql).Scan()
|
||||
}
|
||||
|
|
8
web.go
8
web.go
|
@ -39,9 +39,11 @@ func RunHTTPServer() {
|
|||
r.Handle("/plugins", http.HandlerFunc(PluginsHandler))
|
||||
r.Handle("/help", http.HandlerFunc(HelpHandler))
|
||||
|
||||
for _, route := range Routes() {
|
||||
fmt.Printf("Adding plugin route: /plugins/%v\n", route.URL)
|
||||
r.Handle("/plugins/"+route.URL, http.HandlerFunc(route.Handler)).Methods(route.Method)
|
||||
for _, plugin := range AllPlugins() {
|
||||
fmt.Printf("Adding plugin: %v\n", plugin.Name)
|
||||
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{
|
||||
|
|
Loading…
Reference in New Issue