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.
 
 
 
 
 

72 lines
1.8 KiB

package main
import (
"net/http"
"log"
"io"
"sort"
)
var Hooks []Hook
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")
}
}
func runHook(w http.ResponseWriter, r *http.Request, hook Hook) {
io.WriteString(w, hook.Name + "\n")
}
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
}
hook := Hooks[index]
runHook(w, r, hook)
}
func showHooks(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.String())
for _, hook := range Hooks {
io.WriteString(w, hook.Name + "\n")
}
}
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/"):]
//Perfectly okay to not check to see if Hooks is nil
Hooks = append(Hooks, Hook{Name:name})
sort.Sort(ByName(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
}
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))
}