-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.go
64 lines (60 loc) · 1.45 KB
/
streaming.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
package main
import (
"database/sql"
"fmt"
"github.com/julienschmidt/sse"
"github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"math/rand"
"strconv"
"time"
)
func streamDataToWebPage(sseStreamer *sse.Streamer) {
_, err := sql.Open("postgres", databaseConnection)
if err != nil {
fmt.Println(err.Error())
return
}
reportProblem := func(ev pq.ListenerEventType, err error) {
if err != nil {
fmt.Println(err.Error())
}
}
listener := pq.NewListener(databaseConnection, 10*time.Second, time.Minute, reportProblem)
err = listener.Listen("events")
if err != nil {
fmt.Println(err.Error())
return
}
for {
waitForNotification(listener, sseStreamer)
}
}
func waitForNotification(listener *pq.Listener, sseStreamer *sse.Streamer) {
for {
select {
case n := <-listener.Notify:
sseStreamer.SendString("data", "data", n.Extra)
return
}
}
}
func insertRandomDataToDatabase() {
for {
productionDatabase, err := gorm.Open(postgres.Open(databaseConnection), &gorm.Config{})
productionDB, _ := productionDatabase.DB()
if err != nil {
fmt.Println("Problem opening database, looks like it does not exist")
_ = productionDB.Close()
time.Sleep(2 * time.Second)
continue
}
randomNumber := strconv.Itoa(rand.Intn(100-0) + 0)
fmt.Println("Inserting random data to database: " + randomNumber)
data := Data{Data: randomNumber}
productionDatabase.Save(&data)
_ = productionDB.Close()
time.Sleep(2 * time.Second)
}
}