-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.4 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"errors"
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
"os"
"strconv"
"time"
)
func getInterval() int{
interval_s, err := getEnv("MESSAGE_INTERVAL")
if err != nil {
return 5
}
interval, err := strconv.Atoi(interval_s)
if err != nil {
fmt.Printf("%s must be an integer", "MESSAGE_INTERVAL")
return 5
}
return interval
}
func getMockMessage() []byte{
mock_message, err := getEnv("MOCK_MESSAGE")
if err != nil {
return []byte("Default Message from Go Websocket Mock")
}
return []byte(mock_message)
}
func reader(conn *websocket.Conn) {
for {
if err:= conn.WriteMessage(1, getMockMessage() ); err != nil {
log.Println(err)
return
}
time.Sleep(time.Duration(getInterval()) * time.Second)
}
}
func wsEndpoint(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}
log.Println("Client successfully connected")
reader(ws)
}
func setupRoutes() {
http.HandleFunc("/ws", wsEndpoint)
}
func getEnv(envKey string) (string, error) {
val, ok := os.LookupEnv(envKey)
if !ok {
return "", errors.New(fmt.Sprintf("%s not set\n", envKey))
} else {
return val, nil
}
}
func main() {
setupRoutes()
panic(http.ListenAndServe(":8085",nil))
}