Skip to content

Commit 4f107f3

Browse files
committed
Source for Go websockets app that is deployed with Terraform
1 parent c50485a commit 4f107f3

20 files changed

+8986
-0
lines changed

webapp/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rice-box.go
2+
dist/*

webapp/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Web app
2+
3+
A web app written in Go that uses websockets to log visits and push notifications to all visitors.
4+
5+
Defaults to running on port 80. Set `PORT` as ENV var to specify another port.
6+
7+
### Build
8+
9+
Build for Linux and Darwin:
10+
11+
./bin/build
12+
13+
Output can be found in `dist`.
14+
15+
A copy of the Linux executable will also be copied to `../assets` for deployment with Terraform.
16+
17+
### Run
18+
19+
PORT=8080 go run main.go
20+
21+
### View
22+
23+
http://localhost:8080
24+
25+
## Credits
26+
27+
Browser icons from https://github.com/alrra/browser-logos
28+
29+

webapp/assets/app.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var data = [{ date: new Date(), value: 1 }];
2+
var ids = {};
3+
4+
const windowWidth = $(window).width();
5+
function renderChart(data) {
6+
MG.data_graphic({
7+
title: "Visits",
8+
description: "This graphic shows a time series of page views.",
9+
data: data,
10+
width: windowWidth < 800 ? windowWidth - 40 : 700,
11+
height: windowWidth < 800 ? 150 : 250,
12+
chart_type: "histogram",
13+
bins: 30,
14+
bar_margin: 0,
15+
target: "#chart",
16+
x_accessor: "date",
17+
y_accessor: "value"
18+
});
19+
}
20+
21+
renderChart(data);
22+
23+
function addTableRow(record) {
24+
// {browser:"firefox", epochs:39929283829, ip:"10.0.0.1", agent:"Chrome 238.23"}
25+
$("#visits tbody").prepend(
26+
'<tr><td><img src="images/128/' +
27+
record.browser +
28+
'_128x128.png"></td><td>' +
29+
'<div class="metadata"><span class="time">' +
30+
moment(record.epochs).format("LT") +
31+
"</span></div>" +
32+
'<div class="agent">' +
33+
record.agent +
34+
"</div>" +
35+
"</td></tr>"
36+
);
37+
}
38+
39+
function logVisit(record) {
40+
if (ids[record.id] === undefined) {
41+
record.date = new Date(record.epochs);
42+
data.push(record);
43+
renderChart(data);
44+
addTableRow(record);
45+
ids[record.id] = 1;
46+
}
47+
}
48+
49+
var socket = io({ transports: ["websocket"] });
50+
51+
// Listen for messages
52+
socket.on("message", function(message) {
53+
logVisit(message);
54+
});
55+
56+
socket.on("connect", function() {
57+
// Broadcast a message
58+
var message = {};
59+
const epochs = new Date().getTime();
60+
61+
function broadcastMessage() {
62+
message.epochs = new Date().getTime();
63+
message.id = message.epochs;
64+
socket.emit("send", message, function(result) {
65+
// Silent success
66+
});
67+
}
68+
69+
message = {
70+
id: epochs,
71+
browser: navigator.appName.toLowerCase(),
72+
epochs: epochs,
73+
agent: navigator.userAgent,
74+
value: 1
75+
};
76+
broadcastMessage();
77+
78+
setInterval(broadcastMessage, 30 * 1000);
79+
});

webapp/assets/dependencies/d3.v4.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/assets/dependencies/jquery-3.2.1.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)