forked from datatogether/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_handlers.go
71 lines (66 loc) · 1.58 KB
/
user_handlers.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
package main
import (
"github.com/datatogether/api/apiutil"
"github.com/datatogether/identity/user"
"net"
"net/http"
"net/rpc"
)
func UserHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
GetUserHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func UsersHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
ListUsersHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func ListUsersHandler(w http.ResponseWriter, r *http.Request) {
conn, err := net.Dial("tcp", cfg.IdentityServiceUrl)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
cli := rpc.NewClient(conn)
page := apiutil.PageFromRequest(r)
p := user.UsersListParams{
Limit: page.Size,
Offset: page.Offset(),
}
reply := []*user.User{}
if err := cli.Call("UserRequests.List", p, &reply); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, reply)
}
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
conn, err := net.Dial("tcp", cfg.IdentityServiceUrl)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
cli := rpc.NewClient(conn)
p := user.UsersGetParams{
Subject: &user.User{
Id: r.FormValue("id"),
},
}
reply := &user.User{}
if err := cli.Call("UserRequests.Get", p, reply); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, reply)
}