forked from ppai-plivo/ut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
60 lines (48 loc) · 1.24 KB
/
service.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
package main
import (
"context"
"encoding/json"
"net/http"
"path"
"strconv"
"github.com/ppai-plivo/ut/pkg/db"
"github.com/plivo/pkg/log"
)
type Store interface {
GetUserByID(ctx context.Context, id int) (*db.User, error)
}
type Service struct {
store Store
logger log.Logger
}
func New(store Store, logger log.Logger) *Service {
return &Service{
store: store,
logger: logger,
}
}
func (s *Service) HandleRequest(w http.ResponseWriter, r *http.Request) {
userID, err := strconv.Atoi(path.Base(r.URL.Path))
if err != nil {
http.Error(w, "invalid user id", http.StatusBadRequest)
return
}
user, err := s.store.GetUserByID(context.TODO(), userID)
if err != nil {
s.logger.Errorw("store.GetUserByID() failed", "error", err.Error())
http.Error(w, "user not found", http.StatusNotFound)
return
}
b, err := json.Marshal(user)
if err != nil {
s.logger.Errorw("json.Marshal() failed", "error", err.Error())
http.Error(w, "server malfunctioned", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(b); err != nil {
s.logger.Errorw("w.Write() failed", "error", err.Error())
}
}