/
test.go
42 lines (36 loc) · 862 Bytes
/
test.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 (
"flag"
"fmt"
"http"
)
var addr = flag.String("addr", ":9999", "http service address")
func main() {
flag.Parse()
fmt.Println("Hello, world")
http.Handle("/", handlerWrapper(http.HandlerFunc(testpage)))
err := http.ListenAndServe(*addr, nil)
if err != nil {
fmt.Println("Oh noes.")
}
}
func handlerWrapper(baseHandler http.HandlerFunc) http.HandlerFunc {
f := func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
callback, ok := req.Form["callback"]
if ok {
w.Header()["Content-type"] = []string{"text/javascript"}
fmt.Fprintf(w, "%v(", callback[0])
} else {
w.Header()["Content-type"] = []string{"application/json"}
}
baseHandler(w, req)
if ok {
fmt.Fprintf(w, ");")
}
}
return f
}
func testpage(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, `{ some : "json" }`)
}