Skip to content

Commit 4fadb5b

Browse files
committed
GET api for shipment
1 parent 711eb06 commit 4fadb5b

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

warehouse-server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/aws/aws-sdk-go v1.36.15 // indirect
77
github.com/joho/godotenv v1.3.0
88
github.com/klauspost/compress v1.11.4 // indirect
9+
github.com/mtanzim/event-driven-bookstore/bookstore-server v0.0.0-20201224211355-711eb0609826
910
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96
1011
go.mongodb.org/mongo-driver v1.4.4
1112
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect

warehouse-server/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
4646
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4747
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
4848
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
49+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
50+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
4951
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
5052
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
5153
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
@@ -74,6 +76,9 @@ github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kN
7476
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
7577
github.com/mtanzim/event-driven-bookstore v0.0.0-20201224201644-9daa352f5789 h1:/6HfK/QSCR/84iWDNSwP5j+yXkR1rlIRlCA4nqtYF1M=
7678
github.com/mtanzim/event-driven-bookstore v0.0.0-20201224203702-6e1bc5c24f96 h1:8ktrSYjtDygmGChdHSqMlSabaa5kHo2Iy22Yy14PUak=
79+
github.com/mtanzim/event-driven-bookstore v0.0.0-20201224211355-711eb0609826 h1:1yzcKP2ogrouigRnAYfg6HepM2Aug3vwp4iKvEgNHlE=
80+
github.com/mtanzim/event-driven-bookstore/bookstore-server v0.0.0-20201224211355-711eb0609826 h1:gjhlBQkSaPnS/04/mVnfolcOWcXlRXDLrwd4XFRdJSA=
81+
github.com/mtanzim/event-driven-bookstore/bookstore-server v0.0.0-20201224211355-711eb0609826/go.mod h1:gQtM1vLq1U8hL15dQ4051d/OzHDMvaaunzmPB0yVDro=
7782
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218123655-8698acd0eeba h1:iw7OiWZMjCpE7qFdYs7BzrXsVl+rXqQM3pE0UP6fdaQ=
7883
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218123655-8698acd0eeba/go.mod h1:oyIArPUvcShGDKt2HibHNtknD+W1PwIPHNnBp/8vsL8=
7984
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218130814-89d0f3ce984a h1:9WSsk+hlc2SaznxeI2bE27w6IlDnsJgJ5hNnekV81KM=
@@ -98,6 +103,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
98103
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
99104
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
100105
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
106+
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
107+
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
101108
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
102109
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
103110
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=

warehouse-server/handler/warehouse.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package handler
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
8+
service "github.com/mtanzim/event-driven-bookstore/warehouse-server/service"
9+
)
10+
11+
type ShipmentHandler struct {
12+
service *service.ShipmentService
13+
}
14+
15+
func NewShipmentHandler(s *service.ShipmentService) *ShipmentHandler {
16+
return &ShipmentHandler{s}
17+
}
18+
19+
func (h ShipmentHandler) GetPendingShipments(w http.ResponseWriter, r *http.Request) {
20+
w.Header().Set("Content-Type", "application/json")
21+
pendingShipments, err := h.service.GetPendingShipments()
22+
if err != nil {
23+
log.Println(err)
24+
http.Error(w, "Cannot get data", http.StatusInternalServerError)
25+
return
26+
}
27+
json.NewEncoder(w).Encode(pendingShipments)
28+
}

warehouse-server/main.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package main
22

33
import (
4+
"log"
5+
"net/http"
46
"os"
57

8+
"github.com/gorilla/mux"
69
_ "github.com/joho/godotenv/autoload"
710
consumer "github.com/mtanzim/event-driven-bookstore/common-server/consumer"
811
persister "github.com/mtanzim/event-driven-bookstore/common-server/persister"
12+
handler "github.com/mtanzim/event-driven-bookstore/warehouse-server/handler"
913
service "github.com/mtanzim/event-driven-bookstore/warehouse-server/service"
14+
"github.com/rs/cors"
1015
)
1116

17+
func loggingMiddleware(next http.Handler) http.Handler {
18+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19+
log.Println(r.RequestURI)
20+
next.ServeHTTP(w, r)
21+
})
22+
}
23+
1224
func main() {
1325
kafkaServer := os.Getenv("KAFKA_SERVER_ADDR")
1426
groupID := os.Getenv("KAFKA_GROUP_ID")
@@ -34,9 +46,18 @@ func main() {
3446

3547
dqlMonitorService := service.NewDLQMonitorService(warehousePaymentDLQColl, warehouseCollection)
3648

37-
done := make(chan bool)
3849
go dqlMonitorService.Monitor()
3950
go shipmentService.ConsumeMessages()
4051
go paymentStatusService.ConsumeMessages()
41-
<-done
52+
53+
shipmentHandler := handler.NewShipmentHandler(shipmentService)
54+
r := mux.NewRouter()
55+
port := os.Getenv("REST_PORT")
56+
r.HandleFunc("/api/shipment", shipmentHandler.GetPendingShipments).Methods(http.MethodGet, http.MethodOptions)
57+
r.Use(loggingMiddleware)
58+
// TODO: fix this
59+
rWithCORS := cors.Default().Handler(r)
60+
log.Println("Starting HTTP Server on port", port)
61+
log.Fatal(http.ListenAndServe(":"+port, rWithCORS))
62+
4263
}

warehouse-server/service/shipment.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ func NewShipmentService(c *kafka.Consumer, topic string, coll *mongo.Collection)
2121
return &ShipmentService{&WarehouseService{c, topic}, coll}
2222
}
2323

24+
func (s ShipmentService) GetPendingShipments() ([]dto.CartWarehouse, error) {
25+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
26+
defer cancel()
27+
28+
var data []dto.CartWarehouse
29+
cursor, err := s.collection.Find(ctx, bson.M{}, nil)
30+
if err != nil {
31+
return nil, err
32+
33+
}
34+
if err = cursor.All(ctx, &data); err != nil {
35+
return nil, err
36+
}
37+
return data, nil
38+
39+
}
40+
2441
func (s ShipmentService) ConsumeMessages() {
2542
// TODO: is this idiomatic :/
2643
s.warehouseService.ConsumeMessages(s.processShipmentRequest)

0 commit comments

Comments
 (0)