forked from micro/micro
/
server.go
134 lines (112 loc) · 2.69 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package api
import (
"encoding/json"
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
log "github.com/golang/glog"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/errors"
"golang.org/x/net/context"
)
type ApiServer struct {
mtx sync.RWMutex
address string
exit chan chan error
}
func newApiServer(address string) API {
return &ApiServer{
address: address,
exit: make(chan chan error),
}
}
func (s *ApiServer) Address() string {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.address
}
func (s *ApiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
var service, method string
var request interface{}
// response content type
w.Header().Set("Content-Type", "application/json")
switch r.Header.Get("Content-Type") {
case "application/json":
b, err := ioutil.ReadAll(r.Body)
if err != nil {
e := errors.BadRequest("go.micro.api", err.Error())
w.WriteHeader(400)
w.Write([]byte(e.Error()))
return
}
var body map[string]interface{}
err = json.Unmarshal(b, &body)
if err != nil {
e := errors.BadRequest("go.micro.api", err.Error())
w.WriteHeader(400)
w.Write([]byte(e.Error()))
return
}
service = body["service"].(string)
method = body["method"].(string)
request = body["request"]
default:
r.ParseForm()
service = r.Form.Get("service")
method = r.Form.Get("method")
json.Unmarshal([]byte(r.Form.Get("request")), &request)
}
log.Infof("API Request: /rpc service: %s, method: %s", service, method)
var response map[string]interface{}
req := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request)
err := (*cmd.DefaultOptions().Client).Call(context.Background(), req, &response)
if err != nil {
log.Errorf("Error calling %s.%s: %v", service, method, err)
ce := errors.Parse(err.Error())
switch ce.Code {
case 0:
w.WriteHeader(500)
default:
w.WriteHeader(int(ce.Code))
}
w.Write([]byte(ce.Error()))
return
}
b, _ := json.Marshal(response)
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
}
func (s *ApiServer) Init() error {
log.Infof("API RPC handler %s", RpcPath)
http.Handle(RpcPath, s)
http.HandleFunc(HttpPath, restHandler)
return nil
}
func (s *ApiServer) Start() error {
l, err := net.Listen("tcp", s.address)
if err != nil {
return err
}
log.Infof("Listening on %s", l.Addr().String())
s.mtx.Lock()
s.address = l.Addr().String()
s.mtx.Unlock()
go http.Serve(l, nil)
go func() {
ch := <-s.exit
ch <- l.Close()
}()
return nil
}
func (s *ApiServer) Stop() error {
ch := make(chan error)
s.exit <- ch
return <-ch
}