Skip to content

Commit

Permalink
Source for Go websockets app that is deployed with Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
topfunky committed Dec 19, 2017
1 parent c50485a commit 4f107f3
Show file tree
Hide file tree
Showing 20 changed files with 8,986 additions and 0 deletions.
2 changes: 2 additions & 0 deletions webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rice-box.go
dist/*
29 changes: 29 additions & 0 deletions webapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Web app

A web app written in Go that uses websockets to log visits and push notifications to all visitors.

Defaults to running on port 80. Set `PORT` as ENV var to specify another port.

### Build

Build for Linux and Darwin:

./bin/build

Output can be found in `dist`.

A copy of the Linux executable will also be copied to `../assets` for deployment with Terraform.

### Run

PORT=8080 go run main.go

### View

http://localhost:8080

## Credits

Browser icons from https://github.com/alrra/browser-logos


79 changes: 79 additions & 0 deletions webapp/assets/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var data = [{ date: new Date(), value: 1 }];
var ids = {};

const windowWidth = $(window).width();
function renderChart(data) {
MG.data_graphic({
title: "Visits",
description: "This graphic shows a time series of page views.",
data: data,
width: windowWidth < 800 ? windowWidth - 40 : 700,
height: windowWidth < 800 ? 150 : 250,
chart_type: "histogram",
bins: 30,
bar_margin: 0,
target: "#chart",
x_accessor: "date",
y_accessor: "value"
});
}

renderChart(data);

function addTableRow(record) {
// {browser:"firefox", epochs:39929283829, ip:"10.0.0.1", agent:"Chrome 238.23"}
$("#visits tbody").prepend(
'<tr><td><img src="images/128/' +
record.browser +
'_128x128.png"></td><td>' +
'<div class="metadata"><span class="time">' +
moment(record.epochs).format("LT") +
"</span></div>" +
'<div class="agent">' +
record.agent +
"</div>" +
"</td></tr>"
);
}

function logVisit(record) {
if (ids[record.id] === undefined) {
record.date = new Date(record.epochs);
data.push(record);
renderChart(data);
addTableRow(record);
ids[record.id] = 1;
}
}

var socket = io({ transports: ["websocket"] });

// Listen for messages
socket.on("message", function(message) {
logVisit(message);
});

socket.on("connect", function() {
// Broadcast a message
var message = {};
const epochs = new Date().getTime();

function broadcastMessage() {
message.epochs = new Date().getTime();
message.id = message.epochs;
socket.emit("send", message, function(result) {
// Silent success
});
}

message = {
id: epochs,
browser: navigator.appName.toLowerCase(),
epochs: epochs,
agent: navigator.userAgent,
value: 1
};
broadcastMessage();

setInterval(broadcastMessage, 30 * 1000);
});
2 changes: 2 additions & 0 deletions webapp/assets/dependencies/d3.v4.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions webapp/assets/dependencies/jquery-3.2.1.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4f107f3

Please sign in to comment.