Captain Hook - Web hook server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

171 lines
3.7 KiB

package main
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"net/http"
// "sort"
"strings"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
//Show the index
func index(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
if r.URL.String() == "/" {
http.ServeFile(w, r, "../html/index.html")
} else {
http.ServeFile(w, r, "../html/404.html")
}
}
//Run a particular hook (will probably come up with a better solution)
func runHook(w http.ResponseWriter, r *http.Request, hook Hook) {
fmt.Fprintln(w, hook.Name)
}
//Handles all of the hooks
func hookHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
name := r.URL.String()[len("/hook/"):]
row := db.QueryRow("SELECT EXISTS(SELECT id FROM HOOKS WHERE HOOKS.id = ?)", name)
var res int
row.Scan(&res)
if res == 0 {
http.ServeFile(w, r, "../html/404.html")
return
}
hook := Hook{
Name: name,
params: nil,
actions: nil,
}
runHook(w, r, hook)
}
//Shows all of the hooks
func showHooks(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
if r.URL.String() != "/hooks/" {
http.ServeFile(w, r, "../html/404.html")
return
}
rows, err := db.Query("SELECT ID FROM HOOKS")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
hookNames := ""
var name string
for rows.Next() {
err = rows.Scan(&name)
if err != nil {
log.Fatal(err)
}
hookNames += name + "\n"
}
fmt.Fprint(w, hookNames)
}
//Creates a new hook
func createHook(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
//Doesn't actually work yet
if r.URL.String() == "/hook/create/" {
http.ServeFile(w, r, "../html/create.html")
} else {
//Create a hook
name := r.URL.String()[len("/hook/create/"):]
statement, err := db.Prepare("INSERT IGNORE INTO HOOKS(id) VALUES(?)")
if err != nil {
log.Fatal(err)
fmt.Fprintln(w, err)
return
}
defer statement.Close()
statement.Exec(name)
fmt.Fprintln(w, name)
}
}
//Deletes all hooks
func deleteHook(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
name := r.URL.String()[len("/hook/delete/"):]
statement, err := db.Prepare("DELETE FROM HOOKS where ID=(?)")
if err != nil {
log.Println(err)
fmt.Fprintln(w, err)
return
}
defer statement.Close()
res, err := statement.Exec(name)
if err != nil {
log.Println(err)
fmt.Fprintln(w, err)
return
}
n, err := res.RowsAffected()
if err != nil {
log.Println(err)
fmt.Fprintln(w, err)
return
}
if n == 0 {
http.ServeFile(w, r, "../html/404.html")
return
}
fmt.Fprintln(w, name)
// log.Println(cap(Hooks))
// //Clear hooks (this is perfectly safe to do)
// Hooks = nil
}
func main() {
//Get user credentials
password, err := ioutil.ReadFile(".password")
if err != nil {
log.Fatal(err)
}
connectString := strings.TrimSpace(string(password))
//Open the db and initialize the table if it doesn't exist
db, err = sql.Open("mysql", connectString + "@tcp(localhost:3306)/captain")
if err != nil {
log.Fatal(err)
}
defer db.Close()
var statement *sql.Stmt
//// DEBUG: Making sure table is good
// statement, err = db.Prepare("DROP TABLE IF EXISTS HOOKS")
// if err != nil {
// log.Fatal(err)
// }
// statement.Exec()
statement, err = db.Prepare(`CREATE TABLE IF NOT EXISTS HOOKS(
ID varchar(64) PRIMARY KEY
)`)
if err != nil {
log.Fatal(err)
}
statement.Exec()
//Handle hooks
http.HandleFunc("/hook/delete/", deleteHook)
http.HandleFunc("/hook/create/", createHook)
http.HandleFunc("/hook/", hookHandler)
http.HandleFunc("/hooks/", showHooks)
http.HandleFunc("/", index)
//Start the server
log.Println("Starting server")
log.Fatal(http.ListenAndServe("127.0.0.1:8000", nil))
}