Skip to content

Commit

Permalink
routes send orders to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
qjoly committed Aug 26, 2024
1 parent 33e9946 commit 5b3ce40
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions controller/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions routes/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ services:
routes:
image: ghcr.io/une-tasse-de-cafe/coffee-shop/routes:latest
build: .
ports:
- 8080:8080
10 changes: 10 additions & 0 deletions routes/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
53 changes: 52 additions & 1 deletion routes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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})
}
Expand All @@ -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 {
Expand Down

0 comments on commit 5b3ce40

Please sign in to comment.