upgrades - plugins

pull/10/head
Hunter Long 2018-06-10 21:08:04 -07:00
parent dfcdbcffe1
commit 4df559efc7
4 changed files with 78 additions and 12 deletions

View File

@ -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
} }

1
plugins/html/slack.html Normal file
View File

@ -0,0 +1 @@
yooyyo

View File

@ -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
} }

View File

@ -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() {
}