-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.go
62 lines (53 loc) · 1.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/apex/httpstat"
"github.com/dustin/go-humanize"
)
func main() {
addr := ":" + os.Getenv("PORT")
http.HandleFunc("/", handle)
log.Fatal(http.ListenAndServe(addr, nil))
}
func handle(w http.ResponseWriter, r *http.Request) {
// parse form body
if err := r.ParseForm(); err != nil {
log.Printf("error parsing form %s", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// grab values from form
team := r.Form.Get("team_domain")
user := r.Form.Get("user_name")
cmd := r.Form.Get("command")
args := strings.Fields(r.Form.Get("text"))
if len(args) == 0 {
fmt.Fprintln(w, "url is required")
return
}
// log stuff
fmt.Printf("%s %v from user %s (%s)\n", cmd, args, user, team)
// perform the request
res, err := httpstat.Request("GET", args[0], nil, nil)
if err != nil {
log.Printf("url required")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// write timing information
fmt.Fprintf(w, "Status %d\n", res.Status())
fmt.Fprintf(w, "Size %s\n", humanize.Bytes(uint64(res.BodySize())))
fmt.Fprintf(w, "Timing:")
fmt.Fprintf(w, " - DNS %s\n", res.TimeDNS())
fmt.Fprintf(w, " - Connect %s\n", res.TimeConnect())
fmt.Fprintf(w, " - TLS %s\n", res.TimeTLS())
fmt.Fprintf(w, " - Wait %s\n", res.TimeWait())
fmt.Fprintf(w, " - Response %s\n", res.TimeResponse(time.Now()))
fmt.Fprintf(w, " - Download %s\n", res.TimeDownload(time.Now()))
fmt.Fprintf(w, " - Total %s\n", res.TimeTotalWithRedirects(time.Now()))
}