Browse Source

Add very basic (and not race condition proof) POC

master
stew3254 2 years ago
parent
commit
d9ce49e1db
  1. 4
      README.md
  2. 10
      html/404.html
  3. 14
      html/create.html
  4. 10
      html/index.html
  5. 46
      src/hook.go
  6. 72
      src/server.go

4
README.md

@ -1,3 +1,3 @@
# captain
# Captain
Captain Hook - Web hook server
Captain Hook - Web hook server

10
html/404.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 - Page Not Found</title>
</head>
<body>
<h1>404 - Page Not Found</h1>
</body>
</html>

14
html/create.html

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Hook</title>
</head>
<body>
<form>
<h1>This page does not actually work</h1>
<input type="text">
<input type="submit" value="submit" action="">
</form>
</body>
</html>

10
html/index.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Test Web Hook Server</h1>
</body>
</html>

46
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")
}

72
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))
}
Loading…
Cancel
Save