-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
41 lines (35 loc) · 772 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
addr := ":" + os.Getenv("PORT")
http.HandleFunc("/", handle)
log.Fatal(http.ListenAndServe(addr, nil))
}
func handle(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Error parsing form.", http.StatusBadRequest)
return
}
url := r.Form.Get("text")
start := time.Now()
_, err := http.Get(url)
if err != nil {
http.Error(w, "Error requesting URL.", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
text := fmt.Sprintf("%s took %s", url, time.Since(start))
jsonString := fmt.Sprintf(`
{
"response_type": "in_channel",
"text": "%s"
}
`, text)
fmt.Fprintf(w, jsonString)
}