3 changed files with 158 additions and 58 deletions
-
1.gitignore
-
32src/hook.go
-
183src/server.go
@ -0,0 +1 @@ |
|||
.password |
@ -1,46 +1,46 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"net/http" |
|||
"io" |
|||
"log" |
|||
"io" |
|||
"log" |
|||
"net/http" |
|||
) |
|||
|
|||
type HookHandler interface { |
|||
ServeHTTP(http.ResponseWriter, *http.Request) |
|||
ServeHTTP(http.ResponseWriter, *http.Request) |
|||
} |
|||
|
|||
type Condition struct { |
|||
selector string |
|||
tester string |
|||
value string |
|||
selector string |
|||
tester string |
|||
value string |
|||
} |
|||
|
|||
type Hook struct { |
|||
Name string |
|||
params []Condition |
|||
actions []string |
|||
Name string |
|||
params []Condition |
|||
actions []string |
|||
} |
|||
|
|||
type ByName []Hook |
|||
|
|||
func (h ByName) Len() int { |
|||
return len(h) |
|||
return len(h) |
|||
} |
|||
|
|||
func (h ByName) Less(i, j int) bool { |
|||
return h[i].Name < h[j].Name |
|||
return h[i].Name < h[j].Name |
|||
} |
|||
|
|||
func (h ByName) Swap(i, j int) { |
|||
h[i], h[j] = h[j], h[i] |
|||
h[i], h[j] = h[j], h[i] |
|||
} |
|||
|
|||
func (h Hook) String() string { |
|||
return h.Name |
|||
return h.Name |
|||
} |
|||
|
|||
func (h Hook) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|||
log.Println(r.URL.String()) |
|||
io.WriteString(w, h.Name + "\n") |
|||
log.Println(r.URL.String()) |
|||
io.WriteString(w, h.Name+"\n") |
|||
} |
@ -1,72 +1,171 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"net/http" |
|||
"log" |
|||
"io" |
|||
"sort" |
|||
"database/sql" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"log" |
|||
"net/http" |
|||
// "sort"
|
|||
"strings" |
|||
_ "github.com/go-sql-driver/mysql" |
|||
) |
|||
|
|||
var Hooks []Hook |
|||
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") |
|||
} |
|||
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) { |
|||
io.WriteString(w, hook.Name + "\n") |
|||
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/"):] |
|||
index := sort.Search(len(Hooks), func(i int) bool { return name == Hooks[i].Name }) |
|||
if index == len(Hooks) { |
|||
http.ServeFile(w, r, "../html/404.html") |
|||
return |
|||
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, |
|||
} |
|||
hook := Hooks[index] |
|||
runHook(w, r, hook) |
|||
runHook(w, r, hook) |
|||
} |
|||
|
|||
//Shows all of the hooks
|
|||
func showHooks(w http.ResponseWriter, r *http.Request) { |
|||
log.Println(r.URL.String()) |
|||
for _, hook := range Hooks { |
|||
io.WriteString(w, hook.Name + "\n") |
|||
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 { |
|||
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/"):] |
|||
//Perfectly okay to not check to see if Hooks is nil
|
|||
Hooks = append(Hooks, Hook{Name:name}) |
|||
sort.Sort(ByName(Hooks)) |
|||
} |
|||
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()) |
|||
log.Println(cap(Hooks)) |
|||
//Clear hooks (this is perfectly safe to do)
|
|||
Hooks = nil |
|||
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() { |
|||
http.HandleFunc("/hook/delete/", deleteHook) |
|||
http.HandleFunc("/hook/create/", createHook) |
|||
http.HandleFunc("/hook/", hookHandler) |
|||
http.HandleFunc("/hooks/", showHooks) |
|||
http.HandleFunc("/", index) |
|||
log.Fatal(http.ListenAndServe("127.0.0.1:80", nil)) |
|||
//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)) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue