diff --git a/controller/docker-compose.yaml b/controller/docker-compose.yaml index f371747..f5cbf0e 100644 --- a/controller/docker-compose.yaml +++ b/controller/docker-compose.yaml @@ -3,3 +3,5 @@ services: routes: image: ghcr.io/une-tasse-de-cafe/coffee-shop/controller:latest build: . + environment: + - "NATS_URL=192.168.128.51:4222" diff --git a/routes/docker-compose.yaml b/routes/docker-compose.yaml index d32a23f..b7ac5af 100644 --- a/routes/docker-compose.yaml +++ b/routes/docker-compose.yaml @@ -3,3 +3,5 @@ services: routes: image: ghcr.io/une-tasse-de-cafe/coffee-shop/routes:latest build: . + ports: + - 8080:8080 diff --git a/routes/go.mod b/routes/go.mod index 35681ee..73f37bb 100644 --- a/routes/go.mod +++ b/routes/go.mod @@ -1,3 +1,13 @@ module github.com/une-tasse-de-cafe/coffee-shop/routes go 1.21.6 + +require github.com/nats-io/nats.go v1.37.0 + +require ( + github.com/klauspost/compress v1.17.2 // indirect + github.com/nats-io/nkeys v0.4.7 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + golang.org/x/crypto v0.18.0 // indirect + golang.org/x/sys v0.16.0 // indirect +) diff --git a/routes/main.go b/routes/main.go index b7266a9..1ca7996 100644 --- a/routes/main.go +++ b/routes/main.go @@ -2,9 +2,18 @@ package main import ( "encoding/json" + "errors" "fmt" "log" "net/http" + "os" + + "github.com/nats-io/nats.go" +) + +const ( + streamName = "ORDERS-WEB" + subject = "coffee.web.requests" ) type CoffeeOrder struct { @@ -15,6 +24,37 @@ type CoffeeOrder struct { SugarCount string `json:"sugar_count"` } +func sendOrderToController(order CoffeeOrder) error { + url := os.Getenv("NATS_URL") + if url == "" { + return errors.New("Please provide nats url in NATS_URL env") + } + + nc, _ := nats.Connect(url) + defer nc.Drain() + + js, _ := nc.JetStream() + + js.AddStream(&nats.StreamConfig{ + Name: streamName, + Subjects: []string{subject}, + }) + + jsonData, err := json.Marshal(order) + if err != nil { + return errors.New("Error converting to JSON:" + err.Error()) + + } + ack, err := js.Publish(subject, jsonData) + + if err != nil { + return err + } + + fmt.Println(ack) + return nil +} + func handleCoffeeOrder(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) @@ -40,6 +80,12 @@ func handleCoffeeOrder(w http.ResponseWriter, r *http.Request) { response := fmt.Sprintf("Order received from %s : %s size coffee, %s beans, with %s and %s sugar(s).", order.Name, order.Size, order.BeanType, order.Milk, order.SugarCount) fmt.Println(response) + err = sendOrderToController(order) + + if err != nil { + fmt.Println("ERR") + } + w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]string{"title": "Thank you!", "message": response, "status": orderStatus}) } @@ -50,7 +96,12 @@ func handleHome(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/order-coffee", handleCoffeeOrder) - http.HandleFunc("/", handleHome) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/index", http.StatusMovedPermanently) + }) + + http.HandleFunc("/index", handleHome) fmt.Println("Starting server on port 8080...") if err := http.ListenAndServe(":8080", nil); err != nil {