-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhelper.templ.go
88 lines (73 loc) · 1.74 KB
/
helper.templ.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package appenginetesting
import (
"text/template"
)
const helperTemplString = `
package helper
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"appengine"
)
func init() {
http.HandleFunc("/info", info)
http.HandleFunc("/call", call)
}
func info(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
found := false
for i, a := range os.Args {
if a == "-addr_api" {
found = true
log.Printf("FAKE_APP_API_SOCKET:%s", os.Args[i+1])
}
}
if !found {
http.Error(w, "socket not found", 404)
}
}
type fakeProto struct {
data []byte
}
func (p *fakeProto) Reset() {
*p = fakeProto{}
}
func (p *fakeProto) String() string {
return string(p.data)
}
func (*fakeProto) ProtoMessage() {
}
func (p *fakeProto) Marshal() ([]byte, error) {
return p.data, nil
}
func (p *fakeProto) Unmarshal(data []byte) error {
p.data = make([]byte, len(data))
copy(p.data, data)
return nil
}
func call(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
body, err := ioutil.ReadAll(r.Body)
service, method := r.FormValue("s"), r.FormValue("m")
log.Printf("making API call for %q.%q ; body len = %d (cl=%d), %v", service, method, len(body), r.ContentLength, err)
if err != nil {
http.Error(w, "failed to read body", 500)
return
}
in := &fakeProto{body}
out := &fakeProto{}
err = c.Call(service, method, in, out, nil)
log.Printf("API call %q.%q = %v", service, method, err)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/x-proto")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(out.data)))
w.Write(out.data)
}
`
var helperTempl = template.Must(template.New("helper.go").Parse(helperTemplString))