-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
42 lines (36 loc) · 939 Bytes
/
api.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/numine777/OffByOne/core"
"reflect"
)
func handlePost(handler *reflect.Value, request *http.Request, writer http.ResponseWriter) {
if request.Body == nil {
http.Error(writer, "Please send a request body", 400)
return
}
sig, err := core.GetSignature(handler)
if err != nil {
panic(err)
}
decoder := json.NewDecoder(request.Body)
parsed := reflect.New(sig.Params[0])
elem := parsed.Interface()
err = decoder.Decode(&elem)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", parsed.Elem())
params := make([]reflect.Value, len(sig.Params))
params[0] = parsed.Elem()
body := (*handler).Call(params)
fmt.Printf(body[0].String())
json.NewEncoder(writer).Encode(body)
}
func superSmartPost(handler *reflect.Value) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
handlePost(handler, r, w)
}
}