mirror of https://github.com/statping/statping
upgrades - plugins
parent
dfcdbcffe1
commit
4df559efc7
|
@ -6,12 +6,17 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pluginRoutes []*plugins.Routing
|
pluginRoutes []*plugins.Routing
|
||||||
|
allPlugins []*plugins.Plugin
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitPluginsDatabase() {
|
func InitPluginsDatabase() {
|
||||||
plugins.InitDB(db)
|
plugins.InitDB(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetAuthorized() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func Routes() []*plugins.Routing {
|
func Routes() []*plugins.Routing {
|
||||||
return plugins.PluginRoutes
|
return plugins.PluginRoutes
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
yooyyo
|
|
@ -3,24 +3,45 @@ package plugins
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
PluginRoutes []*Routing
|
PluginRoutes []*Routing
|
||||||
|
Plugins []*Plugin
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Plugin struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
type Routing struct {
|
type Routing struct {
|
||||||
URL string
|
URL string
|
||||||
Method string
|
Method string
|
||||||
Handler func(http.ResponseWriter, *http.Request)
|
Handler func(http.ResponseWriter, *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Add(name string) {
|
||||||
|
plugin := &Plugin{name}
|
||||||
|
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)) {
|
||||||
route := &Routing{url, method, handle}
|
route := &Routing{url, method, handle}
|
||||||
PluginRoutes = append(PluginRoutes, route)
|
PluginRoutes = append(PluginRoutes, route)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Authenticated(r *http.Request) bool {
|
||||||
|
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func log(msg... string) {
|
||||||
|
fmt.Println(" @plugins: ",msg)
|
||||||
|
}
|
||||||
|
|
||||||
func InitDB(database *sql.DB) {
|
func InitDB(database *sql.DB) {
|
||||||
db = database
|
db = database
|
||||||
}
|
}
|
|
@ -1,30 +1,69 @@
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SLACK_TABLE = "plugin_slack"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
AddRoute("save_slack", "POST", SaveIt)
|
Add("slack")
|
||||||
AddRoute("create_slack", "GET", Create)
|
AddRoute("install_slack", "GET", InstallSlack)
|
||||||
|
AddRoute("uninstall_slack", "GET", UninstallSlack)
|
||||||
|
AddRoute("save_slack", "POST", SaveSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveIt(w http.ResponseWriter, r *http.Request) {
|
type Slack struct {
|
||||||
|
Key string
|
||||||
|
Secret string
|
||||||
|
Enabled bool
|
||||||
|
Channel string
|
||||||
|
}
|
||||||
|
|
||||||
|
func InstallSlack(w http.ResponseWriter, r *http.Request) {
|
||||||
|
CreateTable()
|
||||||
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(w http.ResponseWriter, r *http.Request) {
|
func UninstallSlack(w http.ResponseWriter, r *http.Request) {
|
||||||
CreateTable()
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
slack.UpdateTable()
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/plugins", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func CreateTable() {
|
func CreateTable() {
|
||||||
sql := "CREATE TABLE slack (enabled BOOLEAN, api_key text, api_key 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);"
|
||||||
db.QueryRow(sql).Scan()
|
db.QueryRow(sql).Scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DropTable() {
|
func DropTable() {
|
||||||
sql := "DROP TABLE slack;"
|
sql := "DROP TABLE "+SLACK_TABLE+";"
|
||||||
db.QueryRow(sql).Scan()
|
db.QueryRow(sql).Scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabase() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue