diff --git a/README.md b/README.md index 8da60df..e469385 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# captain +# Captain -Captain Hook - Web hook server \ No newline at end of file +Captain Hook - Web hook server diff --git a/html/404.html b/html/404.html new file mode 100644 index 0000000..f216209 --- /dev/null +++ b/html/404.html @@ -0,0 +1,10 @@ + + + + + 404 - Page Not Found + + +

404 - Page Not Found

+ + diff --git a/html/create.html b/html/create.html new file mode 100644 index 0000000..6640f3a --- /dev/null +++ b/html/create.html @@ -0,0 +1,14 @@ + + + + + Create Hook + + +
+

This page does not actually work

+ + +
+ + diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..cf19783 --- /dev/null +++ b/html/index.html @@ -0,0 +1,10 @@ + + + + + + + +

Test Web Hook Server

+ + diff --git a/src/hook.go b/src/hook.go new file mode 100644 index 0000000..8d0aa0b --- /dev/null +++ b/src/hook.go @@ -0,0 +1,46 @@ +package main + +import ( + "net/http" + "io" + "log" +) + +type HookHandler interface { + ServeHTTP(http.ResponseWriter, *http.Request) +} + +type Condition struct { + selector string + tester string + value string +} + +type Hook struct { + Name string + params []Condition + actions []string +} + +type ByName []Hook + +func (h ByName) Len() int { + return len(h) +} + +func (h ByName) Less(i, j int) bool { + return h[i].Name < h[j].Name +} + +func (h ByName) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} + +func (h Hook) String() string { + return h.Name +} + +func (h Hook) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Println(r.URL.String()) + io.WriteString(w, h.Name + "\n") +} diff --git a/src/server.go b/src/server.go new file mode 100644 index 0000000..6b2ba81 --- /dev/null +++ b/src/server.go @@ -0,0 +1,72 @@ +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)) +}