@ -2,10 +2,13 @@ package main
import (
"database/sql"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
)
@ -15,7 +18,7 @@ var db *sql.DB
// Used in writing out errors when they occur
type errorMessage struct {
StatusCode int
Msg string
Msg string
}
// Writes the given error message in the template. If not, just writes the message raw
@ -34,64 +37,60 @@ func writeError(w http.ResponseWriter, e errorMessage) {
// Get all the currently available hooks
func getHooks ( w http . ResponseWriter ) [ ] Hook {
// Get all hooks from db
rows , err := db . Query ( "SELECT ID FROM HOOKS " )
rows , err := db . Query ( "SELECT ID FROM Hooks " )
if err != nil {
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
}
defer rows . Close ( )
// Look through hooks
var hooks [ ] Hook
hooks := make ( [ ] Hook , 0 )
for rows . Next ( ) {
var hook Hook
err = rows . Scan ( & hook . Name )
// Hook doesn't have a name (This shouldn't really happen)
if err != nil {
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
}
hooks = append ( hooks , hook )
}
return hooks
}
// Run a particular hook (will probably come up with a better solution)
func runHook ( w http . ResponseWriter , r * http . Request , hook Hook ) {
b , err := json . Marshal ( hook )
if err != nil {
logError ( w , err )
} else {
fmt . Fprint ( w , string ( b ) )
}
}
// Show the index
func index ( w http . ResponseWriter , r * http . Request ) {
// Log request to this url
log . Println ( r . URL . String ( ) )
log . Println ( r . URL . String ( ) , r . Method )
if r . URL . String ( ) == "/" {
// Serve index template
t := template . Must ( template . New ( "index.html" ) . ParseFiles ( "html/index.html" ) )
err := t . Execute ( w , getHooks ( w ) )
if err != nil {
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
}
// Can't find the page
// Can't find the page
} else {
writeError ( w , errorMessage {
writeError ( w , errorMessage {
StatusCode : http . StatusNotFound ,
Msg : "Not f ound",
Msg : "Not F ound" ,
} )
}
}
// 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 requests to /hook/
func hookHandler ( w http . ResponseWriter , r * http . Request ) {
// Log request to this url
log . Println ( r . URL . String ( ) )
log . Println ( r . URL . String ( ) , r . Method )
if r . URL . String ( ) == "/hook/" {
switch r . Method {
// Display all hooks
@ -100,9 +99,9 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
// Method unsupported
default :
log . Println ( "y tho" )
writeError ( w , errorMessage {
writeError ( w , errorMessage {
StatusCode : http . StatusMethodNotAllowed ,
Msg : "Method Unsupported" ,
Msg : "Method Unsupported" ,
} )
}
return
@ -115,23 +114,22 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
// Handle the hook
case http . MethodGet , http . MethodPost :
// See if the hook exists
row := db . QueryRow ( "SELECT EXISTS(SELECT id FROM HOOKS WHERE HOOKS .id = ?)" , name )
row := db . QueryRow ( "SELECT EXISTS(SELECT id FROM Hooks WHERE Hooks .id = ?)" , name )
var res int
row . Scan ( & res )
// Hook doesn't exist
if res == 0 {
writeError ( w , errorMessage {
writeError ( w , errorMessage {
StatusCode : http . StatusNotFound ,
Msg : "Not Found" ,
Msg : "Not Found" ,
} )
return
}
// Construct the hook from the results
hook := Hook {
Name : name ,
params : nil ,
actions : nil ,
hook := Hook {
Name : name ,
conditions : nil ,
}
// Run the user's hook
@ -147,9 +145,9 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
// Method unsupported
default :
writeError ( w , errorMessage {
writeError ( w , errorMessage {
StatusCode : http . StatusMethodNotAllowed ,
Msg : "Method Not Allowed" ,
Msg : "Method Not Allowed" ,
} )
}
}
@ -157,27 +155,21 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
// Shows all of the hooks
func showHooks ( w http . ResponseWriter ) {
hooks := getHooks ( w )
allHooks := ""
if hooks != nil {
for _ , h := range hooks {
allHooks += h . Name + "\n"
}
b , err := json . Marshal ( hooks )
if err != nil {
logError ( w , err )
} else {
allHooks = "Nothing Found\n"
fmt . Fprint ( w , string ( b ) )
}
fmt . Fprint ( w , allHooks )
}
// Creates a new hook
func createHook ( w http . ResponseWriter , name string ) {
statement , err := db . Prepare ( "INSERT IGNORE INTO HOOKS (id) VALUES(?)" )
statement , err := db . Prepare ( "INSERT IGNORE INTO Hooks (id) VALUES(?)" )
// Shouldn't really happen, but incase the insert goes wrong
if err != nil {
log . Fatal ( err )
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
os . Exit ( 1 )
return
}
defer statement . Close ( )
@ -187,14 +179,10 @@ func createHook(w http.ResponseWriter, name string) {
// Delete a hook
func deleteHook ( w http . ResponseWriter , name string ) {
statement , err := db . Prepare ( "DELETE FROM HOOKS where ID=(?)" )
statement , err := db . Prepare ( "DELETE FROM Hooks WHERE ID=(?)" )
//Shouldn't ever error
if err != nil {
log . Println ( err )
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
return
}
defer statement . Close ( )
@ -203,30 +191,16 @@ func deleteHook(w http.ResponseWriter, name string) {
res , err := statement . Exec ( name )
// Shouldn't ever error
if err != nil {
log . Println ( err )
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : err . Error ( ) ,
} )
logError ( w , err )
return
}
// Probably also shouldn't error
n , err : = res . RowsAffected ( )
_ , err = res . RowsAffected ( )
if err != nil {
log . Println ( err )
fmt . Fprintln ( w , err )
return
}
// Nothing was deleted
if n == 0 {
writeError ( w , errorMessage {
StatusCode : http . StatusInternalServerError ,
Msg : "Hook was not deleted" ,
} )
logError ( w , err )
return
}
fmt . Fprintln ( w , name )
}
}