forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
37 lines (30 loc) · 866 Bytes
/
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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"encoding/json"
"log"
"net/http"
)
// requestSub is the general subject for DB requests.
const requestSubject = "dbReq"
// GetUsers is a sample database requrest handler.
func GetUsers(w http.ResponseWriter, r *http.Request) {
log.Println("GetUsers: Started")
defer log.Println("GetUsers: Completed")
// Build the request.
req := Request{
URI: "/db/users",
}
// Send the request for processing.
resp, err := SendRequest(requestSubject, req)
if err != nil {
log.Println("GetUsers: ERROR:", err)
json.NewEncoder(w).Encode(struct{ Error error }{err})
return
}
log.Println("GetUsers: RESP:", resp.JSON)
// Return the response back to the caller.
json.NewEncoder(w).Encode(resp.JSON)
return
}