-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (114 loc) · 3.21 KB
/
main.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 main
import (
"context"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/realkinetic/cloud-native-meetup-2019/car-service/service"
"github.com/realkinetic/cloud-native-meetup-2019/util"
)
const port = ":8082"
var notrace = flag.Bool("notrace", false, "disable tracing")
type server struct {
service service.CarRentalService
}
func main() {
flag.Parse()
if err := util.Init("car-service", *notrace); err != nil {
panic(err)
}
carService, err := service.NewCarRentalService()
if err != nil {
panic(err)
}
s := &server{service: carService}
http.HandleFunc("/cars/booking", s.bookingHandler)
handler := util.NewContextHandler(http.DefaultServeMux)
log.Printf("Car rental service listening on %s...", port)
if err := http.ListenAndServe(port, handler); err != nil {
panic(err)
}
}
func (s *server) bookingHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
switch r.Method {
case "GET":
s.getBooking(ctx, w, r)
case "POST":
s.bookCarRental(ctx, w, r)
default:
log.WithContext(ctx).WithFields(log.Fields{
"error": errors.New("invalid HTTP method"),
}).Error("Invalid HTTP method for endpoint")
http.Error(w, "Invalid HTTP method", http.StatusBadRequest)
}
}
func (s *server) getBooking(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ref := r.URL.Query().Get("ref")
ctx = util.WithRef(ctx, ref)
confirmation, err := s.service.GetBooking(ctx, ref)
if err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Error("Failed to fetch booking")
if err == service.ErrNoSuchBooking {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
resp, err := json.Marshal(confirmation)
if err != nil {
panic(err)
}
log.WithContext(ctx).Info("Fetched booking")
w.Write(resp)
}
func (s *server) bookCarRental(ctx context.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Error("Failed to read request body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req service.BookCarRentalRequest
if err := json.Unmarshal(data, &req); err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Error("Failed to unmarshal request body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := req.Validate(); err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Error("Invalid booking request")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
confirmation, err := s.service.BookCarRental(ctx, &req)
if err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Error("Failed to book car")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx = util.WithRef(ctx, confirmation.Ref)
resp, err := json.Marshal(confirmation)
if err != nil {
log.WithContext(ctx).WithFields(log.Fields{
"error": err,
}).Fatal("Failed to marshal response")
}
log.WithContext(ctx).Info("Booked car")
w.WriteHeader(http.StatusCreated)
w.Write(resp)
}