Skip to content

Commit

Permalink
implement websocket to view the dashboard of top ten product
Browse files Browse the repository at this point in the history
  • Loading branch information
Le Minh Tan committed Jul 30, 2021
1 parent c048aac commit 02ea940
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/dutroctu/go-webservice

go 1.16

require github.com/go-sql-driver/mysql v1.6.0 // indirect
require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
28 changes: 28 additions & 0 deletions product/product.data.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,31 @@ func updateProduct(product Product) error {
}
return nil
}

func GetTopTenProducts() ([]Product, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

results, err := database.DbConn.QueryContext(ctx, `SELECT productId,
manufacturer,
sku,
upc,
pricePerUnit,
quantityOnHand,
productName
FROM products ORDER BY quantityOnHand DESC LIMIT 10`)
if err != nil {
log.Println(err.Error())
return nil, err
}
defer results.Close()
products := make([]Product, 0)
for results.Next() {
var product Product
results.Scan(&product.ProductID, &product.Manufacturer,
&product.Sku, &product.Upc, &product.PricePerUnit,
&product.QuantityOnHand, &product.ProductName)
products = append(products, product)
}
return products, nil
}
2 changes: 2 additions & 0 deletions product/product.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"time"

"github.com/dutroctu/go-webservice/cors"
"golang.org/x/net/websocket"
)

const productsBasePath = "products"

func SetupRoutes(apiBasePath string) {
handleProducts := http.HandlerFunc(productsHandler)
handleProduct := http.HandlerFunc(productHandler)
http.Handle("/websocket", websocket.Handler(productSocket))
http.Handle(fmt.Sprintf("%s/%s", apiBasePath, productsBasePath), cors.Middleware(handleProducts))
http.Handle(fmt.Sprintf("%s/%s/", apiBasePath, productsBasePath), cors.Middleware(handleProduct))
}
Expand Down
53 changes: 53 additions & 0 deletions product/product.websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package product

import (
"fmt"
"log"
"time"

"golang.org/x/net/websocket"
)

type message struct {
Data string `json:"data"`
Type string `json:"type"`
}

func productSocket(ws *websocket.Conn) {
done := make(chan struct{})
fmt.Println("new websocket connection established")
go func(c *websocket.Conn) {

for {
var msg message
if err := websocket.JSON.Receive(ws, &msg); err != nil {
log.Println(err)
break
}
fmt.Println("received message ", msg.Data)
}
close(done)
}(ws)

loop:
for {
select {
case <-done:
fmt.Println("connection was closed, lets break out of here")
break loop
default:
products, err := GetTopTenProducts()
if err != nil {
log.Println(err)
break
}
if err := websocket.JSON.Send(ws, products); err != nil {
log.Println(err)
break
}
time.Sleep(10 * time.Second)
}
}
fmt.Println("close websocket")
defer ws.Close()
}

0 comments on commit 02ea940

Please sign in to comment.