forked from hashicorp/demo-terraform-101
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Source for Go websockets app that is deployed with Terraform
- Loading branch information
Showing
20 changed files
with
8,986 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rice-box.go | ||
dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.