-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
44 lines (35 loc) · 1.01 KB
/
app.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
)
func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/", fs)
http.HandleFunc("/callback", callbackHandler)
log.Println("Listening...")
log.Fatal(http.ListenAndServe(":3000", nil))
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var params []string
for k, v := range r.PostForm {
params = append(params, fmt.Sprintf(`%v: %v`, strconv.Quote(k), strconv.Quote(v[0])))
}
paramsString := "{" + strings.Join(params, ",") + "}"
callback := r.FormValue("js")
if callback == "" {
callback = "parent.callback"
}
JSONCallback(w, callback, paramsString, http.StatusCreated)
}
func JSONCallback(w http.ResponseWriter, callback string, params string, code int) {
response := fmt.Sprintf("<html><head><script>%v(%v)</script></head><body>%v(%v)</body></html>", callback, params, callback, params)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(code)
io.WriteString(w, response)
}